How to Encrypt and Decrypt Image Path in PHP

To Encrypt and Decrypt Image Path in PHP, we use 3 main PHP inbuilt functions,

How to Encrypt and Decrypt Image URL in PHP
How to Encrypt and Decrypt Image URL in PHP
  • file_get_contents() Function:
    • The file_get_contents() function is PHP inbuilt function that can help to read files into a string. This function uses the memory mapping technique to doing this which is also supported by the server.
  • file_put_contents() Function:
    • This function can create a new file if not available with the same name if the file is available then it overwrites that. The file can be an image or any type of file.
  • base64_encode() Function:
    • The base64_encode() is also a PHP built-in function, which used to encode the data with MIME base64. MIME (Multipurpose Internet Mail Extensions). It encodes the string in base64 which takes 33% more space than the original data.
  • base64_decode() Function:
    • This function is vise versa of the base64_encode() function. It decodes the data which encode with MIME base64 type.

Here we use these PHP built-in function to encrypt and decrypt the image path in PHP. You can check the below example and source code.

Encrypt Image Path in PHP

<?php

$image=file_get_contents("testImage.png");
$encrypted=base64_encode($image);
echo '<img src="data:image/gif;base64,'.$encrypted.'" />';

?>

Code Explanations:

  • Here we use a normal PNG image, to read we use file_get_contents() function.
  • Than we encrypt the image path using base64_encode() function.
  • On the last line we show the image which generated from encryption URL.

Show Decrypt Image and Path in PHP

<?php

$encrypted = ''; //encrypted image from above code or any base64 encoded text
$decrypted=base64_decode($encrypted);

//file_put_content put decoded image with that name you can change it according to you.
file_put_contents("testImage.png",$decrypted);
echo '<img src="testImage.png" />';

?>

Here is the output image of both, Encrypt Image Path in PHP and as well as Show Decrypt Image and Path in PHP.

How to Encrypt and Decrypt Image Path in PHP
Example of Encrypt Image Path in PHP and Show Decrypt Image and Path in PHP

Copy and paste above code and check it out, if you facing any issue with these than please feel free to comment.

Want to check more about file_put_content and other function, https://www.php.net/manual/en/function.file-put-contents.php

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 163

3 thoughts on “How to Encrypt and Decrypt Image Path in PHP

    1. Thanks for the clarification.
      But if you want to use other methods like openssl_encrypt or PHP Blob at the end you have to use base64_encode() for decryption.

      1. It’s not clear to me what you mean by “use `base64_encode()` for decryption”. The main purpose of Base64 is encoding binary and other non-ASCII data as ASCII, and yes, that’s very often used in conjunction with `openssl_encrypt` and such.

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

We are now accepting Guest Posting and creation of Backlinks
This is default text for notification bar