using base64 data
using base64 data
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?
"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?
Re: using base64 data
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.
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.
Re: using base64 data
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... .
Re: using base64 data
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:
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">
Re: using base64 data
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');
Re: using base64 data
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.
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.
Re: using base64 data
I have been thinking as I am using php I can use the built in function to decode the base64 back into image data?
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.
Code: Select all
$data = base64_decode($src);
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.
Re: using base64 data
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: using base64 data
Could it be that image? Can you find any others that are commonly used to be sure that they all fail?
Re: using base64 data
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: using base64 data
This works for me:
I just removed all his indents.
If that does not work, change your line endings for windows.
Code: Select all
convert 'inline:data:,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//U
b//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ek
yky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguW
w6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7' b64_folder.gif
If that does not work, change your line endings for windows.
Re: using base64 data
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.
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.
Re: using base64 data
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.
Re: using base64 data
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.
I will have another go at it tomorrow.