stamas
stamas

Reputation: 397

php imagick - read image from base64

I am manipulating images with js, and I'd like to save these transformed images. I'm posting this data with ajax:

image : canvas.toDataURL('image/jpeg')

This way, I get the base64 code for the image, but I can't find a way to read it with Imagick.

This is my process:

$img = new Imagick();
$decoded = base64_decode($_POST['image']);
$img->readimageblob($decoded);

But this fails:

Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' @ error/blob.c/BlobToImage/360' in /Library/WebServer/Documents/test/save.php:7 Stack trace:

#0 /Library/WebServer/Documents/test/save.php(7): Imagick->readimageblob('u?Z?f?{??z?????...')

Any ideas why?

Upvotes: 8

Views: 19792

Answers (3)

Tars
Tars

Reputation: 99

readImageBlob — Reads image from a binary string : http://php.net/manual/en/imagick.readimageblob.php

$base64 = "iVBORw0KGgoAAAAN....";

$imageBlob = base64_decode($base64);

$imagick = new Imagick();
$imagick->readImageBlob($imageBlob);

header("Content-Type: image/png");
echo $imagick;

Upvotes: 5

stamas
stamas

Reputation: 397

Figured out.

I had to remove the data:image/png;base64, part from the posted string, and then imagick could interpret it as blob.

Upvotes: 14

Abid Hussain
Abid Hussain

Reputation: 7762

Read this url;--

PHP-Imagemagick image display

or try it:-

$thumbnail = $img->getImageBlob();
$contents =  ob_get_contents();
ob_end_clean();

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

Upvotes: 3

Related Questions