25

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.

Prisoner
  • 27,391
  • 11
  • 73
  • 102
netzaffin
  • 1,602
  • 3
  • 13
  • 14
  • @mishu of course... i tried base64encode($imageres) and even ob_contents etc. but it doesnt work... – netzaffin Dec 14 '11 at 10:08
  • You can use [resample](http://stackoverflow.com/a/29250930/4058484) to get the base64 encoded string after resizing – eQ19 May 10 '16 at 16:11

1 Answers1

54

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);
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130