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;
}
}
?>
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
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- January 19, 2009
- article by Gabriel C.
- 2 comments
Related Posts
-
PHP: Cropping a Rectangle Image to Square using GDat October 1, 2008 with 5 comments
-
PHP: How to Add Transparency to an Image using GDat October 13, 2008 with 6 comments
-
Resize an Image (on the fly) & Keep its Aspect Ratio using PHP and GDat September 28, 2008 with 32 comments
-
PHP: How to Download a Remote Image using GD or cURLat January 5, 2009 with 30 comments
-
How to extract images from an URL in PHPat August 30, 2008 with 8 comments

2 Replies to "PHP: How to Create Mirror Images using GD"
October 17, 2011 at 4:31 AM
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();
?>
October 17, 2011 at 4:38 AM
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);
?>