PHP ImageMagick - Securing The Images



In this chapter, you will be learning to secure the images so that only the sender and the intended receiver get to see the images on the web pages.

PHP Imagemagick provides image processing and manipulation, allowing you to protect your images from theft or unauthorized use. Now, we will discuss the features of PHP Imagemagick and how they can help you keep your images safe.

Enciphering an Image

The inbuilt function named ‘encipherImage()’ in Imagemagick, helps in enciphering the images. Converting the plain pixels image to the enciphered pixels is the process that happens in this function. The enciphered image can be viewed only by the viewer who can decipher the image using the key is given (‘passphrase’).

Syntax

public Imagick::encipherImage(string $passphrase): bool

This function takes one parameter ‘passphrase’ which acts as a key to encrypt and decrypt images. It takes an image as input and enciphers the image using the passphrase and produces the enciphered image as output.

Example

In the below example, the implementation of ‘encipherImage()’ function is shown. Firstly, a new imagick object must be created and an image is taken as input. ‘Passphrase’ is defined which is a string that is passed as a parameter. Then, ‘encipherImage()’ function is applied with the help of the passphrase and the enciphered image is obtained as output.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image.jpeg");
   $passphrase="Tutorials Point";
   $image->encipherImage($passphrase);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/encipherImage.png");
?>

Assume that the following is the input image (image.jpeg) in the program −

Enciphering Image

Output

Enciphering Image

Deciphering the Image

Imagemagick has provided an inbuilt function ‘decipherImage()’ which helps to decipher the image. The process of converting the encrypted image to a plain image is called deciphering an image. This function takes the enciphered image as input, converts that image to a plain image using the passphrase, and produces the plain image as output.

Syntax

public Imagick::decipherImage(string $passphrase): bool

This function takes the ‘passphrase’ as a parameter. It helps to decipher the image.

Example

The following example shows how to implement the 'decipherImage()' function. To begin, create a new Imagick object and pass an image as input. You will also need to define a passphrase string which is passed as a parameter. Finally, use the 'decipherImage()' function with your passphrase to obtain a deciphered image as output.

<?php
   $image=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/encipherImage.png");
   $passphrase="Tutorials Point";
   $image->decipherImage($passphrase);
   $image->writeImage($_SERVER['DOCUMENT_ROOT']."/test/decipherImage.png");
?>

Assume that the following is the input image (encipherImage.png) in the program −

Deciphering Image

Output

Deciphering Image
Advertisements