Hello,
I am describing here the way to
1) Encode image to generate a string
2) Decode image from a given encoded string
1) Here you need to specifiy the path of your image in variable ‘$image_path’
<?php $image_path = 'test_image.gif'; //this will be the physical path of your image $img_binary = fread(fopen($image_path, "r"), filesize($image_path)); $img_str = base64_encode($img_binary); // will produce the encoded string echo '<img src="data:image/gif;base64,'.$img_str.'" />'; //you can display the image on browser screen using image tag, if jpeg or jpg image than use 'image/jpg' ?>
2) Here I am using the string produced from the executing the code seen above, it can be any encoded string which is generated from image
<?php $decoded_str = base64_decode($img_str); //pass the encoded string here $im = imagecreatefromstring($decoded_str); //below code will display the image on browser if ($im !== false) { header('Content-Type: image/gif'); imagegif($im); imagedestroy($im); } else { echo 'An error occurred.'; } ?>
Hope this is helpful.
This is not a secure way of encryption. If someone know the base64 encryption, They can simply decode code the image and use.
But I this code has help me in one place. Anyway Thank you..
Hello Jizo, thanks for your feedback, but this post doesn’t mention about encryption, may be you can post here any other way to do it.
Thanks you saved my day. I was looking for this
i wasen’t abe to display encoded image from last 5 days. echo ” this helped me alot Thank you so much
Not working
May I know the details ?
Thanks for this script
Hey i am unable to see the image! it displays nothing ! i triple checked the image path and also that it is readable as i am trying to run it on my localhost! please help !
the following is the code :
<?php
$image_path = 'images/new.gif'; //this will be the physical path of your image
$img_binary = fread(fopen($image_path, "r"), filesize($image_path));
$img_str = base64_encode($img_binary); // will produce the encoded string
echo '’; //you can display the image on browser screen using image tag, if jpeg or jpg image than use ‘image/jpg’
$decoded_str = base64_decode($img_str); //pass the encoded string here
$im = imagecreatefromstring($decoded_str);
//below code will display the image on browser
if ($im !== false) {
header(‘Content-Type: image/gif’);
imagepng($im);
imagedestroy($im);
}
else {
echo ‘An error occurred.’;
}
?>
Issue seems to be the physical path, please check using fread function.
Hi ,
Encoded image not display in the browser.
Plz help me any one
What error is displayed ?, please check whether image path is correct and image is readable.
Hello,
do You know how to encode image not from file, but from PHP resource? (i mean object that is creating e.g. by the imagecreatefrompng/imagecreatefromjpg/imagecreatefromstring function?
Thanks to look at this.
Eve
If you are creating an image, why you want to encode it ?, you can directly use the resource object for passing, and can display image as and when necssary
i want to save encoded image with some name. How i can do this.
Encoding an image gives a string as an output, if you wan to store it in database, you can add new column with your custom name to be given to image
Awesome, great script. Was looking for this since a long time. Whatever I used to find wouldn’t work. The decoding worked perfectly. Thank you!!!!!
@praz – Glad, it was useful to you. Thanks for your comments
not helpful. >.<
@krogs – Did you face any issue ?