Encode Decode image using PHP

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.

19 thoughts on “Encode Decode image using PHP

  1. This is not a secure way of encryption. If someone know the base64 encryption, They can simply decode code the image and use.

    1. 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.

  2. 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

  3. 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.’;
    }
    ?>

  4. 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

    1. 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

    1. 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

  5. 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!!!!!

Leave a Reply to praz Cancel reply

Your email address will not be published. Required fields are marked *

Scroll to top