9951 explained code solutions for 126 technologies


predisHow can I use PHP and Redis to store and manipulate bitmap images?


Using PHP and Redis, bitmap images can be stored and manipulated in a variety of ways. PHP can be used to read in the image file and then convert it into a binary string. Redis can then be used to store the binary string in a key-value pair.

Example code

$imageFile = file_get_contents('image.bmp');
$binaryString = bin2hex($imageFile);
$redis->set('image', $binaryString);

The binary string can then be retrieved from Redis and used to manipulate the image. For example, the image can be resized using PHP's imagecopyresampled function.

Example code

$binaryString = $redis->get('image');
$imageFile = hex2bin($binaryString);
$image = imagecreatefromstring($imageFile);
$resizedImage = imagecreatetruecolor(100, 100);
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, 100, 100, imagesx($image), imagesy($image));
header('Content-Type: image/bmp');
imagebmp($resizedImage);

Output example

Resized image in BMP format.

Code explanation

  • file_get_contents('image.bmp'): reads the image file into a string.
  • bin2hex($imageFile): converts the image file string into a binary string.
  • $redis->set('image', $binaryString): stores the binary string in a key-value pair in Redis.
  • $binaryString = $redis->get('image'): retrieves the binary string from Redis.
  • hex2bin($binaryString): converts the binary string back into a string.
  • imagecreatefromstring($imageFile): creates an image resource from the string.
  • imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, 100, 100, imagesx($image), imagesy($image)): resizes the image.
  • header('Content-Type: image/bmp'): sets the content type of the response.
  • imagebmp($resizedImage): outputs the resized image in BMP format.

Helpful links

Edit this code on GitHub