search
top

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.

15 Responses to “Encode Decode image using PHP”

  1. Pratik says:

    Not working

  2. Anne Maria says:

    Thanks for this script

  3. Azim Saiyed says:

    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. sibhu says:

    Hi ,

    Encoded image not display in the browser.

    Plz help me any one

  5. Eve says:

    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

    • Pragnesh says:

      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

  6. brij kishor says:

    i want to save encoded image with some name. How i can do this.

    • Pragnesh says:

      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

  7. praz says:

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

  8. krogs says:

    not helpful. >.<

Leave a Reply

top