How to encrypt and decrypt an image file in PHP?

How can I encrypt a downloaded image file (for example www.yahoo.com/images/image1.jpg ) using PHP and decrypt when it is displayed in a browser? I want to save it in a folder, not in a database.

+4
source share
1 answer

All downloaded files are stored in a temporary PHP folder.

This temporary file path is accessible via the variable $ _FILES ["file"] ["tmp_name"] set by PHP after submitting the form.

You can then encode image data using an encoding algorithm (not encryption) such as base64_encode (), and then decode it for display using base64_decode ().

<?php $image_binary = fread(fopen($_FILES["file"]["tmp_name"], "r"), filesize($_FILES["file"]["tmp_name"])); $encoded_image = base64_encode($image_binary); //Save the image file with file data set as $encoded_image ?> 

Another thing to keep in mind is that your PHP scripts are visible to anyone with admin access (like your host). Therefore, they will know which encryption method you used, and can learn about how to decrypt encrypted files.

Best encryption strategies: Best way to use PHP to encrypt and decrypt passwords?

-1
source

Source: https://habr.com/ru/post/1412931/


All Articles