Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 233
stamas Avatar asked Aug 14 '12 17:08

stamas


2 Answers

Figured out.

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

like image 136
stamas Avatar answered Nov 03 '22 00:11

stamas


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;
like image 5
Tars Avatar answered Nov 03 '22 02:11

Tars