PHP: How to Create Mirror Images using GD

Posted on January 19, 2009, under PHP,  Bookmark it

This class is useful to create mirror images using the GD Library. You just need to set the source path of the image (URL or local path) and the folder where the new image should be saved. If the latter is not specified the image will output without being saved. This is useful if you need to generate mirror images ‘on the fly’.

Here’s the source code (I will explain you how it works below):

<?php
/*
--------------------------------------------------------------------------------------------
Credits: Bit Repository
Source URL: http://www.bitrepository.com/
--------------------------------------------------------------------------------------------
*/

/* Image Mirror Class */

class Image_Mirror {

var $source_image;
var $new_image_name;
var $save_to_folder;

function make_mirror_image($flip = 1)
{
$info = GetImageSize($this->source_image);

if(empty($info)) {
	exit("The file from the requested path doesn't see to be an image");
}

$width = $info[0];
$height = $info[1];

$mime = $info['mime'];

// What sort of image?

$type = substr(strrchr($mime, '/'), 1);

switch ($type)
{
case 'jpeg':
    $image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
	$quality = 100; // best quality
    break;

case 'png':
    $image_create_func = 'ImageCreateFromPNG';
    $image_save_func = 'ImagePNG';
	$new_image_ext = 'png';
	$quality = 0; // no compression
    break;

case 'bmp':
    $image_create_func = 'ImageCreateFromBMP';
    $image_save_func = 'ImageBMP';
	$new_image_ext = 'bmp';
    break;

case 'gif':
    $image_create_func = 'ImageCreateFromGIF';
    $image_save_func = 'ImageGIF';
	$new_image_ext = 'gif';
    break;

case 'vnd.wap.wbmp':
    $image_create_func = 'ImageCreateFromWBMP';
    $image_save_func = 'ImageWBMP';
	$new_image_ext = 'bmp';
    break;

case 'xbm':
    $image_create_func = 'ImageCreateFromXBM';
    $image_save_func = 'ImageXBM';
	$new_image_ext = 'xbm';
    break;

default:
	$image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
}

// Source Image
$image = $image_create_func($this->source_image);

$new_image = ImageCreateTrueColor($width, $height);

// Set a White & Transparent Background Color (PHP 4 >= 4.3.2, PHP 5)
$bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127);
ImageFill($new_image, 0, 0 , $bg);

if($flip == 1)
{
   $dst_y = 0;
   $src_y = 0;

   $coordinate = ($width - 1);

   foreach(range($width, 0) as $range)
   {
   $src_x = $range;
   $dst_x = $coordinate - $range;

   ImageCopy($new_image, $image, $dst_x, $dst_y, $src_x, $src_y, 1, $height);
   }
}
elseif($flip == 2)
{
   $dst_x = 0;
   $src_x = 0;

   $coordinate = ($height - 1);

   foreach(range($height, 0) as $range)
   {
   $src_y = $range;
   $dst_y = $coordinate - $range;

   ImageCopy($new_image, $image, $dst_x, $dst_y, $src_x, $src_y, $width, 1);
   }
}

if(isSet($this->save_to_folder))
		{
	       if($this->new_image_name)
	       {
	       $new_name = $this->new_image_name.'.'.$new_image_ext;
	       }
	       else
	       {
$basename = basename($this->source_image);
$new_name = $this->new_image_name($basename).'_mirror.'.$new_image_ext;
	       }

		$save_path = $this->save_to_folder.$new_name;
		}
		else
		{
		/* Set the right header for the image */
		   header("Content-Type: ".$mime);

		   $save_path = '';
		}

// Show/Save image 

if(isSet($quality))
	{
	$process = $image_save_func($new_image, $save_path, $quality);
	}
	else
	{
	$process = $save_path ? $image_save_func($new_image, $save_path) : $image_save_func($new_image);
	}

return array('result' => $process, 'new_file_path' => $save_path);
	}

function new_image_name($filename)
{
		$ext = strrchr($filename, ".");

	if($ext)
	{
	$strlen = strlen($ext);
	$filename = basename(substr($filename, 0, -$strlen));
	}

	$string = trim($filename);
	$string = strtolower($string);
	$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
	$string = ereg_replace("[ \t\n\r]+", "_", $string);
	$string = str_replace(" ", '_', $string);
	$string = ereg_replace("[ _]+", "_", $string);

	return $string;
}

}
?>
Variables Info

var $source_image – The URL or local path to the image (ex: http://www.domain.com/images/logo.jpg, /home/site/public_html/images/logo.jpg).
var $new_image_name (optional) – Sets the new name for the mirrored image. If NULL or not set, a name will be automatically added for the new image.
var $save_to_folder – This is the folder where the new image will be saved (ex: /home/site/public_html/mirror_images/). If it is not set, the script will output the image without saving it.
var $quality – This is the 3rd argument that is passed to the image saver function. It is used for JPEG (from 0 to 100) or PNG (from 0 to 9) images.

How it works?

First, we need to get the width, height & mime type of the source image by using GetImageSize(). The latter is used to determine the function that creates a new image from our file/URL. For effectiveness, we will use the concept of variable functions. For example, if the image is a PNG, $image_create_func will be equal with

Share the Love
Get Free Updates

Related Posts

2 Replies to "PHP: How to Create Mirror Images using GD"

  1. Hi,
    nice mirror effect :) but how it works vertical ?

    PS: a simple Example for using this class:
    1. copy and paste this class code in new php file
    2. create new php file and copy/paste this code:

    source_image = 'img.jpg';
    $mirror->new_image_name = 'mirror.jpg';
    $mirror->make_mirror_image();
    ?>

  2. ok, i checked this :)

    vertical:

    <?php
    include_once(‘mirror-class.php’);
    $mirror = new Image_Mirror;
    $mirror->source_image = ‘img.jpg’;
    $mirror->new_image_name = ‘mirror.jpg’;
    $mirror->make_mirror_image($flip = 2);
    ?>

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)