Get base64 encoded png image from url in php - php

I have picture url from Facebook in jpg format. But I can manage it in another code only if it is in base64 encoded png format. I don't want to rewrite entire module.
I have tried everything I found on Internet and nothing works.
The closest I solve this is
base64_encode(imagepng(imagecreatefromstring(file_get_contents($url))));
If you have some ideas I would like to hear them.

get image, file_get_contents('image.jpg');
convert to png: Convert JPG/GIF image to PNG in PHP?
do base 64: http://php.net/manual/en/function.base64-encode.php

Related

convert base64 string to .tiff image

Case : I have images in BLOB format in database and from database getting the images in base64 string. My code was working fine when images was .jpeg.
Code:
<?php $img = $image['PRODUCT_IMAGE']->load(); ?>
<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />
Now suddenly my db administrator converted all images in .tiff format and i have to change my code to support .tiff format. I have been trying for solution but didn't find anything in this case.
In most scenarios they have .tiff file to convert example.
please have me achieve one of the following solution
Convert base64 to .tiff that some browser don't support.
Ask our DB administrator to change the format of all images.
As you guess i don't have the solution to 1 and 2. 3 is not likely to happen.

Convert base64 PDF to base64 image, without saving it to any file

I do not know if it's a valid question to ask here but I'm tired of finding the solutions/libraries, so I had to ask for help from you guys.
Basically I'm generating a PDF using TCPDF and by using the parameter 'E' in the output method I get the the document as base64 mime (base64 String).
So as I'm building a API I don't want to save the PDF (or any file) on the server but just want to convert the output of base64 PDF to a base64 image (jpg) and throw the base64 output as json encoded.
So how do I convert the base64 PDF to base64 image (jpg).
You can use imagemagick to do that.
$imagick = new Imagick();
$imagick->readImageBlob($pdfBlob);
$imagick->setImageFormat("jpeg");
$imageBlob = $imagick->getImageBlob();
see http://php.net/manual/book.imagick.php

create image from png, convert to base64

I want to open a image url, convert it to an image, so I can use imagecopymerge() on it, then convert that to base64.
I was able to view the image data using file_get_contents, but I'm not sure how to combine that with imagecreatefromstring, imagecopymerge, and base64_encode.
The way I finally did it was to use
$img=imagecreatefrompng('url');
ob_start();
imagepng($img);
$imgString=ob_get_clean();
$imgBase64=base64_encode($imgString);
I found out that I can't convert an image to a string, and have to use the buffer.

php reverse imagecreatefromstring

Is there a way to turn a jpg to string, reverse of imagecreatefromstring?
I have to communicate to a server which needs binary of image, i saw plenty of jpg to binary but not the other way around.
Just a shot in the dark here... No real experience with this, just my thoughts after looking through some documentation...
I see in the documentation of imagecreatefromstring() an example is given where a base64 encoded string is converted into an image. Taking that example and flipping it around might just be what you are looking for.
$image = file_get_contents('image_file.jpg');
$imageString = base64_encode($image);
imagecreatefromstring takes a string which contains the binary data of an image and turns it into a gd image resource so you can manipulate it with the gd image library. Literally the "reverse" of that would be imagejpeg, which saves a gd image resource to a jpeg image.
I guess what you really want though is simply the initial string, which contains the binary data of the image to begin with. I.e.:
$imageString = file_get_contents('image.jpg');
$gd = imagecreatefromstring($imageString);
Just skip step 2.

image conversion from .pdf to .jpg not showing complete image

I am using imagemagick on a pdf file on my server, the conversion works correctly however when I look at the jpb that was converted only shows some of the items in the first page of the pdf. However it is missing the complete image in the back.
/usr/local/bin/convert -size 250x350 testimage.pdf[0] testimage.jpg
this is my php code I am using to convert the pdf to jpg.
This is what it should look like
However it looks like this
It looks like a layer try:
/usr/local/bin/convert testimage.pdf[0] -flatten testimage.jpg
Why did you have the -size 250x350 in the code?

Categories