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.
Related
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
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
I'm trying to convert the first page of a PHP generated PDF to an image, and have done so with the following code:
exec("convert http://####.com/tcpdf/examples/example_009.php[0] -resize 100 sample.jpeg");
However I don't want to save the image, I'm looking for a way of including the command in a PHP script in place of an image, e.g: <img src="display_image_script.php?pdf=dynamic_pdf.php">
Is there a way to get ImageMagick to return the image within the PHP page using header('Content-Type: image/jpeg')?
Untested, but try:
header('Content-type: image/jpeg');
passthru("convert somePdfFile.pdf jpeg:-");
You need the passthru to stream the binary back to the browser and the jpeg:- in the command string converts pdf to jpeg and returns the jpeg binary on stdout.
Can I base64 encode an image that I created on the fly, without first saving it to disk? As far as I know, base64_encode() only accepts strings, and I couldn't find a way to retrive image source object as string without first saving it, and load it with file_get_contents()
GD doesn't provide a method to return an output image as text, but you can fake it with the output buffering functions:
ob_start();
imagejpeg($handle); // no second parameter, will do output instead of writing to file
$img = ob_get_clean();
echo base64_encode($img);
I have a gd image reference (I've already manipulated the image how I want to), and now I want to store the image. Instead of saving it directly to a file, is there a way I can get the binary data, that way I can convert it to base64 then save the base64 string. The only option I've been able to find is imagejpeg($image), but that either saves it as a file or prints it directly to the browser.
You can output to the buffer, and then capture it in this way.
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();