how to create a base64encoded string from image resource
Asked Answered
U

1

24

I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.

Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.

Uwton answered 14/12, 2011 at 10:1 Comment(2)
@mishu of course... i tried base64encode($imageres) and even ob_contents etc. but it doesnt work...Uwton
You can use resample to get the base64 encoded string after resizingTroublemaker
I
53

Taken from http://www.php.net/manual/en/book.image.php#93393

$image = imagecreatefromstring($file);

// start buffering
ob_start();
imagepng($image);
$contents =  ob_get_clean();

echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";

imagedestroy($image);
Infliction answered 14/12, 2011 at 10:5 Comment(3)
Hmm, I'm sure i tried this before! Now it works. Thank you very much.Uwton
@netzafin: I know that feeling ;)Infliction
According to the manual $contents = ob_get_clean(); is a one-liner for what ob_get_contents and ob_end_clean does.Messenia

© 2022 - 2024 — McMap. All rights reserved.