Laravel - How to get the image content size? - php

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);

Related

Restore content file_get_content() function

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' />";

file_put_content function change the size of my picture

I'm trying to upload pictures to a server using file_put_content function but it seems that the file size is not the same. Any idea about how to conserve the same file size ?
Here is my code :
list($type, $img) = explode(';', $img);
list(, $img) = explode(',', $img);
$image = str_replace(' ','+',$img);
$data = base64_decode($image,true);
$filename = $dir.$hash_user_id.$typeImage.'.'.$extension;
file_put_contents($filename, $data);

Base 64 Image String Convert and Upload To the PHP server

I Have Image String Like
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAE7CAYAAADXQrC8AAANvUlEQVR4nO3dLXoqWRuGUebDGBgBA0BjsZFxuDhcVFQMKiYmBoPBIDCImAgEAoGoCexPpM9PkirgSYo06W+JZbr7UPu8u0XdV8GuTlVVBQAAgNN1/u0FAAAA/DRCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKA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hFTra9gtmmf6z2l1+/2+7PdVqapNY3T1J/VPeVrd/yMza9JrfBfW1+YopADaI6QAAj8ipD757qrhSUeC//tr2D8/lO6JM62qqsxv6oOjO5qW/bn3/8jMmvyeZctzFFIA7RFSAIH/bkid9tukS1jDdn7T8Od75fHl4++eFpOGJze9m/Jy7v0/MrPzhFTzHIUUQHuEFEDg0I3orO5GdDtvfN9RElKbqiqHfhv0PmLSdyxNTv690L+/hsWk6YW09TGwaQyvhj1rc/+PzKxJ76+QanOOn1s/AHWEFECg+Ua04Tjt3bKMGm5475a70z578Ov3LrtyP6w/XKA/efsOp+3zvNyOR6V37IZ9cF1m62Mnx/3t31/DvDGkRmW5T/asX542Z97/f2Y2HTUcCtHgaro+yxw/t34A6ggpgP+0fdk8r8ti9lQeHx7KdDotD49PZbZYlpdtfvz3z13Df4E5AlwSIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQ+h9OaCdW3kXmdwAAAABJRU5ErkJggg==';
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // Decode image using base64_decode
$file = uniqid() . '.jpg';
//Now you can put this image data to your desired file using file_put_contents function like below:
$success = file_put_contents($file, $data);
I wanted To convert Into Image and Upload To the Server
The Upload Folder Like
www.abc.com/uploads
And My File is
Www.abc.com/webservices/index.php (In this file I will write the code)
Thanks In advance
One way of doing it,
function writePng($data, $filename){
$bin = substr($data, strlen('data:image/png;base64,'));
$bin=base64_decode($bin);
$fp = fopen($filename,'w+');
fwrite($fp, $bin);
fclose($fp);
}
writePng($data, '1.png');

Resize Image in PHP Using Croppie

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.

string to file stream using php or jquery

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);

Categories