Hey Guys,
I am trying to create an jpg image from base64 code. Here's what I've got, in the first forum is a canvas drawing and it is captured by a JavaScript script which then saves it as a base64, in either img/jpeg or img/png. I was then able to take the base64 and attach it to an hidden text value in the forum to be able to grab it by PHP in my next page where I process the information. When I processes the information I want to save the base64 information as a jpg image in a folder, I have been able to save a file as .jpg into the folder but when I try and open it in Photoshop I get this error "Could not complete your request because a JPEG marker segment length is to short.".
This is the code I am using to get the saved .jpg file:
$imgSrc = $_POST['i1'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
//print $imgSrc;
file_put_contents('./images/signatures/' . $firstName . '-' . $lastName . '.jpg', $_POST['i1']);
My guess is that the line of base64 data is being "stored" into the .jpg like a text file and that's why it isn't being read. I was wondering if there is anyway of being able to take that base64 code and creating a image with it?
Thanks!
Creating Images from base64
Moderator: General Moderators
Re: Creating Images from base64
Hey Guys,
I have solved my problem for anyone wondering how here is what I did, I had the base64 code and I needed to strip the beginning part to actually get to the code behind it so I ran this
$imgSrc = substr($imgSrc, 22);
That strips the beginning part to be able to get to the actual data. I then use this to decode that data
$imgSrc = base64_decode($imgSrc);
From there I was able to save it out to a file with their first and last name as the file name by using this code
file_put_contents("./images/signatures/$firstName-$lastName.png",$imgSrc);
I have solved my problem for anyone wondering how here is what I did, I had the base64 code and I needed to strip the beginning part to actually get to the code behind it so I ran this
$imgSrc = substr($imgSrc, 22);
That strips the beginning part to be able to get to the actual data. I then use this to decode that data
$imgSrc = base64_decode($imgSrc);
From there I was able to save it out to a file with their first and last name as the file name by using this code
file_put_contents("./images/signatures/$firstName-$lastName.png",$imgSrc);