I used data-uri data:image/jpeg;base64,BASE64_HERE to pass the image. Can I pass this value imagecreatefromjpeg() to save as an image file.
How does this function work?
imagecreatefromjpeg() function creates image variable from file, but not backwards. While you have image source in base64 you can use simple file_put_contents() function to save file. Eg.:
$data = 'data:image/jpeg;base64,BASE64_HERE';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.jpg', $data);
You can also use imagecreatefromjpeg() and then imagejpeg() method if it's more preferable to you. But best method is to use imagecreatefromstring(), because it will automatically detects whenever image is jpg,gif,png or etc. Eg.:
$data = 'data:image/jpeg;base64,BASE64_HERE';
$image = imagecreatefromstring(base64_decode($data));
imagejpeg($image, 'image.jpg');
Related
I gave an image as a parameter to the file_get_content() function and received it as the output of a string. Now I want to do the opposite of this operation. What should I do?
You can write it back to an image file using file_put_contents
Example:
$pathToImage = '/images/test.png';
$newFile = '/images/test-new.png';
// Get the contents of the image file
$imgString = file_get_contents($pathToImage);
// Write it back to an image
file_put_contents($newFile, $imgString);
If you then look into your folder "images/", there should be a new file called "test-new.png"
Deal with image using base64
with file_put_contents
Online base64 image converte as string encode image
<?php
// Image as base64 truncated
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY8AAAFwCAYA...';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$image = 'image.png';
file_put_contents($image, $data);
echo "<img src='$file' alt='image' />";
I had base64 encoded data.
Look my code, please.
First of all, see my code.
$data = "data:image/png;base64,iVBORw0KGgoAAAA..........";
$image_array_1 = explode(";", $data);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$imageName = uniqid().time().".png";
I want to set an extension .png to complete my file so that I can count this file extension by laravel method $image->getClientOriginalExtension() and others laravel file methods.
sorry for miss spell of language.
Hope I make you understand.
This works, although I cannot say if it's the best way to go about it. It's a full working example with a 1x1 black pixel png image. This assumes you already removed the data:image/png;base64, portion from the image data.
$data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR
42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=');
// Create a temp file and write the decoded image.
$temp = tmpfile();
fwrite($temp, $data);
// Get the path of the temp file.
$tempPath = stream_get_meta_data($temp)['uri'];
// Initialize the UploadedFile.
$imageName = uniqid().time().".png";
$file = new \Illuminate\Http\UploadedFile($tempPath, $imageName, null, null, true);
// Test if the UploadedFile works normally.
echo $file->getClientOriginalExtension(); // Shows 'png'
$file->storeAs('images', 'test.png'); // Creates image in '\storage\app\images'.
// Delete the temp file.
fclose($temp);
I am trying to resize an image which is taken from a cropping tool used on my form. The code below is being used with the library Croppie.
if($_POST['imagebase64'] != '') {
$data = $_POST['imagebase64'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents(APPPATH.'../assets/image64.png', $data);
}
I would like to know how to resize the image before pushing it to the server. Preferably, I would like to use stock php, something like imagecopyresampled but I'm not sure how to go about it.
Any help would be much appreciated.
i have image that was in base64 format as follows:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAE........
I am trying to convert the above string to input file stream. I have checked for this but i didn't get anything, please let me know the process.
You have to use the file put contents.
Try this:
// Your image data
$data = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pV....
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
//Image name
$filename = "myname.png";
$data = base64_decode($data);
$file = 'uploads/' . $filename;
// decode the image data and save it to file
file_put_contents($file, $data);
I'm working on generating dinamic PDF with FPDF.
Now I'm stuck in this point, I send from Javascript a base64 image containing a Graph.
This is my code inside the FPDF class for insert an image.
function Grafici ($image)
{
list($type, $image) = explode(';', $image);
list(, $image) = explode(',', $image);
$data = base64_decode($image);
file_put_contents('/tmp/image.png', $data);
$this->Image('/tmp/image.png', 10, 63, 100);
}
As you can see, i'm saving the image on the server disk.
Is there a way to avoid the saving? If i remove the file_put_contents command and I put directly $data on $this-> Image($data, 10, 63, 100); FPDF return the error:
FPDF error: Image file has no extension and no type was specified: