Convert base64 PDF to base64 image, without saving it to any file - 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

Related

Convert FPDF output to base64 string

I am working with the PHP API where I need to create the PDF file using the FPDF and then I wanted to save it to the database by converting it to the base64 string. When I have tried the default $pdf->Output() method then I got the PDF file in my postman response.
Then I have tried to use this line of code to encode the pdf to base64 format. It worked but I am getting the broken pdf when I decode the base64 string.
$pdfFile = $pdf->Output("","S");
$base64String = chunk_split(base64_encode($pdfFile));
Am i doing anything wrong with this? Any small help would be appriciated.

Get base64 encoded png image from url in 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

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.

How to save a base64 decoded image in the filesystem using php?

I am getting a base64 encoded JPEG string via a POST request to my web service.
I want to decode it and save it in the filesystem.
How can I achieve this using PHP 5.3.
I am able to successfully decode the data using the base64_decode function.
How can I save this decoded string as a JPEG image in the server?
Thanks in advance.
If you are sure the image will always be jpg then you can simply use: file_put_contents();
<?php
$decoded=base64_decode($encodedString);
file_put_contents('newImage.JPG',$decoded);
//leave it to you to randomize the filename.
?>
Replacing the blank spaces with + signs is required if the data is derived from canvas.toDataURL() function.
$encodedString = str_replace(' ','+',$encodedString);
See this question
It helped a lot in my case.

base64 encode an image without saving

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);

Categories