How-to-Encrypt-and-Decrypt-Image-Path-in-PHP

To Encode and Decode 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

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

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

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

3 thoughts on “How to Encode and Decode 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 *