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.
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' />";
The data the user is sending is not an image file but its content. How can I get its size without making it a file and use filesize and something similar?
The content of the image as an example:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......
Is it possible to create a formula to calculate the size of the image content? something like strlen($imageContent).
You need to use the 8bit encoding on mb_strlen:
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$size = mb_strlen($data, '8bit');
You can make a file and then rapidly deleted.
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAASwCAYAA......';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.png', $data);
$size = getimagesize('/tmp/image.png');
unlink(/tmp/image.png);
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');
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: