using base64 data

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

using base64 data

Post by Bonzo »

I am receiving some data into php from a JavaScript file and it will be in a base64 format and the first part of the string? is:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4X0KRXhpZgAATU0AKgAAAAgACQEPAAIAAAASAAAAegEQAAIAAAAKAAAA

What is the best way of dealing with this as I want to recreate the original image and crop sections out of it with convert.

I assume but do not know that I could save the data into the original image in some way via php but is it possible to use the data directly so saving one step?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

I should have checked out the documentation first as it looks like I can use inline although I will probably have to write to a file first as it will be over 5000 characters.
I might use GD as it has a function http://php.net/manual/en/function.image ... string.php which looks like it does not have any size limitation.
If anyone has any useful tips about the best way to go about this I would be interested.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: using base64 data

Post by magick »

ImageMagick should be able to read base64 data directly: convert 'data:image/jpeg;base64,/9j/...' image.jpg. If that fails, prefix data: with inline:, e.g. inline:data:image/jpeg... .
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

Thanks for the info magick but there are to many options and I do not know what I am doing!

I am running the code in php and on my localhost I get a "Unable to fork error" and on my server it runs but nothing happens; I get no result and no errors.

Using the GD function I can get the output I want but I need to do further work in Imagemagick so would rather do all the work without saving a temporary image. I have also tried saving the base64 code as a file still without success.

I changed my image to base64 on a website and have been using that and I have also removed the data:image/jpeg;base64, from the front of the file as it looks like it is not needed and will probably cause problems.

Out of interest the GD code I am using is:

Code: Select all

<?php
$src="/9j/4SFjRXhpZgAASUkqAAgAAAAOAAABAwABAAAAwSYAAAEBAwABAAAAzzYAAAIBAwADAAAAtgAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAABIBAwABAAAAAQAAABUBAwABAAAAAwAAABoBBQABAAAAvAAAABsBBQABAAAAxAAAABwBAwABAAAAAQAAACgBAwABAAAAA the rest of the string is to long to post";

$data = base64_decode($src);

$formImage = imagecreatefromstring($data);

imagejpeg( $formImage, "output.jpg" );
// Clear the memory of the tempory image 
imagedestroy( $formImage );
?> 
<img src="output.jpg">
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

I have found a post on stackoverflow that works using Imagick but I would still prefer to use Imagemagick with exec() :

Code: Select all

$img = new Imagick();
$decoded = base64_decode($src);
$img->readimageblob($decoded);
$img->writeImage('output6.jpg');
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

Out of interest can anyone get a base64 image to convert?

I have tried in php, command line, using inline: not using inline:, different file, saving as a text file, reading from a string direct from the JavaScript, plus other options.

I am using 6.9.0 on Windows and will eventually be running the code on a Linux type server.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

I have been thinking as I am using php I can use the built in function to decode the base64 back into image data?

Code: Select all

$data = base64_decode($src);
Although I am not sure how to feed this into Imagemagick but I will try it out later.

I wonder if this is how Imagick is doing it ( it is how GD is doing it ); I need to find a copy of the class to see what it is doing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using base64 data

Post by fmw42 »

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

Thanks for the link fmw42 and I have tried the suggestions there. I can not get anything to work and I am starting to wonder if it works at all.
I assume it must work as Imagick can do it; but I can not find how it is done. I downloaded the Imagick files but can not find any mention of base64 in them.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using base64 data

Post by fmw42 »

Could it be that image? Can you find any others that are commonly used to be sure that they all fail?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

I tried Anthony's small gif image in his example and could not get that to work either. The problem is I am not getting any errors so I have no idea what the problem could be.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using base64 data

Post by fmw42 »

This works for me:

Code: Select all

convert 'inline:data:,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//U
b//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ek
yky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguW
w6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7' b64_folder.gif
I just removed all his indents.

If that does not work, change your line endings for windows.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

Thank you for that and it works in the command line on Windows when changing ' to " and in php on my localhost and server. So I now know it will work and can not believe with all the variations of code I tried I did not try that one.
I still can not get it to work on large files; I can get some output in one variation of the code but it is the text file written onto a page saved as a jpg!

Anyway enough for today and I will have another go tomorrow night.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: using base64 data

Post by magick »

Command-lines can only accept so much data. For a large file you need to put your inline data in a file and reference it, something like inline:@mydata.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: using base64 data

Post by Bonzo »

Thanks magick but I can not get that to work; I get an error "No such file or directory @ error/blob.c/OpenBlob/2658". I have tried using the name of the file as it is in the same folder as well as the absolute path on the server.

I will have another go at it tomorrow.
Post Reply