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?
Figured out.
I had to remove the data:image/png;base64, part from the posted string, and then imagick could interpret it as blob.
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;
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)."' />";
Related
I'm using the Google Charts API to generate some charts and am using an ajax request to send that data to an endpoint that will save the image to disk for use in a PDF. However, I'm not sure how to get the image string into a PHP image resource.
It looks something like this (I'll snip a bunch of the code to save some room):
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAEsCAYAAABwoFP+AAAgAElEQVR4XuzdCZcDuXXe/XGcfU/sLI41Glv5/l9JGmskZ99XJ7H9nl/bf71QqUgWyeKOOqdPd5NVKODiAvfBXX/rz//8z//8m3lNCkwKTArsTAFby//7f//v6+c//If/8M3f+Bt/45v//b//9zd/+qd/+s3f//t//5vf+q3f+nrjn/3Zn33d8z/+x//45vd+7/e+Pv+bf/Nv7tyb2dykwKTApMCkwC0o8FsTSN6CrLPNSYHPpsD//b//9TJwxYw7z0NGWIDRtXeSQyxAOi1trkg3pCSmx2MUn75CgHbRTcKMlTnqf+PAhEDRO8Xu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EEiki+kDBrKK+FgBrbAm5oHnMggRKUSzye49OnT7MDl2OSBpKGU+WbMiXNQlYnFAKFQCFQCHQgUESyA6Q6pRB4FgLqbTNFC7iJRpJW0d9+0lR+/Pixu3sCdMZSCHU3UCcWAoVAIVAIFAINAkUkazoUAgdGgOZRmcQcSKD/SU4u6IZ5W37J8nc8sBCra4VAIVAIvDACRSRfWLg1tNdAgK/kv//97//WomXeRiSZvZm5aSY/fPjwGoOtURQChUAhUAicCoEikqcSV3X2igggi3wcmaV9HMijgBkJyQXcIJvv378vs/UVJ0iNuRAoBAqBJyJQRPKJ4NetC4GlCPzwww//vQSZRDJFX/sZM7eoblHaf/rTn5Y2X+cXAoVAIbAJAqpy9ea53eSG1cjTECgi+TTo68aFwHIE/vOf//zmX//61xtxTACOVqQF4j/p57t3795M4Xwn6ygECoFCoBAoBPZEoIjknuhW24XATggIuGHORiwRxt/97ndv+SHrKAQKgUKgECgEHolAEclHol33KgQ2RoBWkp8kc3YdhUAhUAgUAoXAoxH4P1ANKGa/fh2tAAAAAElFTkSuQmCC"}';
$decoded = json_decode($data, true);
$resource = imagecreatefromstring($decoded['image_data']);
var_dump($resource);
This throws a warning at the imagecreatefromstring step: Warning: imagecreatefromstring(): Data is not in a recognized format.
What's the right handler for that image format?
This is because your need to get rid of leading data:image/png;base64,
Also your base64 string looks corrupted . I did the following
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAQhUlEQVR4Xu2dTWxcVxWAz3kzEwKqiCtoG0eCWiCx6IK6Euwq6u5aqB0HCVGJnzoggWji1lmAxApbLGCBVAc7QnQBjlS1YlXHToVAIBwJEAsEk+5QF7ggOa666ARQ0mRm3kH3zZvxeDJjz3jsmTn2N1IWyby577zvnHy697777lPhAwEIQMAJAXUSJ2FCAAIQEIRFEUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykik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TlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAv8H5S4HFJE4mRUAAAAASUVORK5CYII="}';
$decoded = json_decode($data, true);
$final = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $decoded['image_data']));
$im = imagecreatefromstring($final);
if ($im !== false) {
ob_clean();
header('Content-type: image/png');
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
}
?>
working fiddle
http://phpfiddle.org/main/code/7u1t-yfbe
I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.
Here's my code
$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';
$im = imagecreatefromwebp($filename);
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
Please help.
i am using this code, it works fine for me. Here $data contains the base64encoded data
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php
I have a script that downloads favicons and turns them in to PNGs.
To handle the conversion, I am using ImageMagick. My current approach involves downloading the data, writing it to a file, converting the file, then deleting the originally downloaded file. Here's what I mean:
$source = 'http://google.com/favicon.ico';
$image = file_get_contents($source);
// I'd like to skip these lines
$favicon = fopen('favicon.ico', 'w');
fwrite($favicon, $image);
fclose($favicon);
$im = new Imagick();
$im->readimage('favicon.ico');
$im = $im->flattenImages();
$im->setImageFormat('png');
$im->writeImage('favicon.png');
unlink('favicon.ico');
This works, but ideally I could do it without writing the $image variable to the file favicon.ico and instead I might just convert the data within the $image variable and then $im->writeImage('favicon.png') on that.
I checked out the method getImageBlob but when I tried that I got this error:
PHP Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `' # error/blob.c/BlobToImage/364' in /home/vagrant/test/test_image.php:73
Stack trace:
#0 /home/vagrant/test/test_image.php(73): Imagick->readimageblob('\x00\x00\x01\x00\x02\x00\x10\x10\x00\x00\x01\x00 \x00h...')
#1 {main}
thrown in /home/vagrant/test/test_image.php on line 73
Any ideas?
You'll need to invoke Imagick::setFormat before reading blob.
<?php
$source = 'http://google.com/favicon.ico';
$image = file_get_contents($source);
$im = new Imagick();
$im->setFormat('ICO');
$im->readImageBlob($image);
$im = $im->flattenImages();
$im->setImageFormat('PNG');
$im->writeImage('favicon.png');
Update
Flattening the .ico image may have negative effects on files containing more than one image. Simplest solution would be to iterate over all the images, and determine which sub-image to use.
$im = new Imagick();
$im->setFormat('ICO');
$im->readImageBlob($image);
for( $idx = 0, $len = $im->getNumberImages(); $idx < $len; $idx++ ) {
// If this is the sub-image you want, do the following, else skip
$im->setImageFormat('png');
$im->setImageIndex($idx);
$im->writeImage(sprintf('favicon_%d.png', $idx));
}
I want to validate whether the image raw data is valid or not.
Below is my code :
private function __blobToImage($imagerawdata)
{
$imagedata = base64_decode($imagerawdata);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
$path = WWW_ROOT . "commapp_images".'/';
$file = mktime().".png";
$filepath = $path.$file;
// Output the image
$image = imagecreatefromstring($imagedata);
ob_start();
imagejpeg($image, $filepath, 80);
ob_get_contents();
ob_end_clean();
return $file;
}
While using my code I'm getting the below error
"Notice (8): imagecreatefromstring() [function.imagecreatefromstring]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file"
Any one please help me as because I strucked here with out any solution.
imagecreatefromstring already does that, it returns false on failure. So you could suppress it's message and check for the return value:
$image = #imagecreatefromstring($imagedata);
if ($image !== false)
{
...
}
Edit: You don't need the header and output buffering if you are saving to a file and you are saving a jpeg with the png extension.
I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.
I have stored the file in the DB using Base64 encoding which makes the result look like:
/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...
(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)
Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:
How do I achieve this through PHP - I have tried using:
<?php
$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver
$data = base64_decode($data);
$fileTmp = imagecreatefromstring($data);
$newImage = imagecreatefromjpeg($fileTmp);
if (!$newImage) {
echo("<img src=".$newImage."/>");
}
?>
I'm just getting a blank page!
Your problem is that imagecreatefromstring() doesn't return a file, but rather an image in memory that should be output with the correct headers.
$data = base64_decode($data);
// Create image resource from your data string
$imgdata = imagecreatefromstring($data);
if ($imgdata) {
// Send JPEG headers
header("Content-type: image/jpeg");
// Output the image data
imagejpeg($imgdata);
// Clean up the resource
imagedestroy($imgdata);
exit();
}