Source for file ImageEditor.php
Documentation is available at ImageEditor.php
* Image Editor. Editing tools, crop, rotate, scale and save.
* @author $Author: Wei Zhuo $
* @author $Author: Paul Moers <mail@saulmade.nl> $ - watermarking and replace code + several small enhancements <http://fckplugins.saulmade.nl>
* @version $Id: ImageEditor.php 27 2004-04-01 08:31:57Z Wei Zhuo $
require_once('Transform.php');
* Handles the basic image editing capbabilities.
* @author $Author: Wei Zhuo $
* @version $Id: ImageEditor.php 27 2004-04-01 08:31:57Z Wei Zhuo $
* user based on IP address
* Create a new ImageEditor instance. Editing requires a
* tmp file, which is saved in the current directory where the
* image is edited. The tmp file is assigned by md5 hash of the
* user IP address. This hashed is used as an ID for cleaning up
* the tmp files. In addition, any tmp files older than the
* the specified period will be deleted.
* @param ImageManager $manager the image manager, we need this
* for some file and path handling functions.
$this->_uid = md5($_SERVER['REMOTE_ADDR']);
* @return int 1 if the file was saved sucessfully,
* 0 no save operation, -1 file save error.
* Process the image, if not action, just display the image.
* @return array with image information, empty array if not an image.
* <code>array('src'=>'url of the image', 'dimensions'=>'width="xx" height="yy"',
* 'file'=>'image file, relative', 'fullpath'=>'full path to the image');</code>
if (isset ($uploadedRelative) && $uploadedRelative != "")
$relative = $uploadedRelative;
elseif(isset ($_GET['img']))
$imgURL = $this->manager->getFileURL($relative);
$fullpath = $this->manager->getFullPath($relative);
$imgInfo = @getImageSize($fullpath);
$image['dimensions'] = $imgInfo[3];
$image['width'] = $imgInfo[0];
$image['height'] = $imgInfo[1];
$image['file'] = $relative;
$image['fullpath'] = $fullpath;
* Process the actions, crop, scale(resize), rotate, flip, and save.
* When ever an action is performed, the result is save into a
* temporary image file, see createUnique on the filename specs.
* It does not return the saved file, alway returning the tmp file.
* @param string $action, should be 'crop', 'scale', 'rotate','flip', or 'save'
* @param string $relative the relative image filename
* @param string $fullpath the fullpath to the image file
* @return array with image information
* <code>array('src'=>'url of the image', 'dimensions'=>'width="xx" height="yy"',
* 'file'=>'image file, relative', 'fullpath'=>'full path to the image');</code>
if(isset ($_GET['params']))
$params = $_GET['params'];
// 'ImageManager.php' handled the uploaded file, it's now on the server.
// If maximum size is specified, constrain image to it.
if ($this->manager->config['maxWidth'] > 0 && $this->manager->config['maxHeight'] > 0 && ($img->img_x > $this->manager->config['maxWidth'] || $img->img_y > $this->manager->config['maxHeight']))
$percentage = min($this->manager->config['maxWidth']/ $img->img_x, $this->manager->config['maxHeight']/ $img->img_y);
$img->scale($percentage);
$functionName = 'ImageCreateFrom' . $img->type;
$imageResource = $functionName($fullpath);
echo "<script>alert(\"Error when loading '" . basename($fullpath) . "' - Loading '" . $img->type . "' files not supported\");</script>";
$watermarkFullPath = $_GET['watermarkFullPath'];
if ($watermarkImageType == "jpg") { $watermarkImageType = "jpeg"; }
if ($watermarkImageType == "tif") { $watermarkImageType = "tiff"; }
$functionName = 'ImageCreateFrom' . $watermarkImageType;
$watermarkResource = $functionName($watermarkFullPath);
echo "<script>alert(\"Error when loading '" . basename($watermarkFullPath) . "' - Loading '" . $img->type . "' files not supported\");</script>";
$watermarkX = isset ($_GET['watermarkX']) ? $_GET['watermarkX'] : - 1;
$watermarkY = isset ($_GET['watermarkY']) ? $_GET['watermarkY'] : - 1;
$opacity = $_GET['opacity'];
// PNG24 watermark on GIF target needs special handling
// PNG24 watermark with alpha transparency on other targets need also this handling
if ($watermarkImageType == "png" && $numberOfColors == 0 && ($img->type == "gif" || $opacity < 100))
require_once('Classes/api.watermark.php');
$imageResource = $watermarkAPI->create_watermark($imageResource, $watermarkResource, $opacity, $watermarkX, $watermarkY);
// PNG24 watermark without alpha transparency on other targets than GIF can use 'imagecopy'
elseif ($watermarkImageType == "png" && $numberOfColors == 0 && $opacity == 100)
$watermark_width = imagesx($watermarkResource);
$watermark_height = imagesy($watermarkResource);
imagecopy($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height);
// Other watermarks can be appllied no swet on all targets
$watermark_width = imagesx($watermarkResource);
$watermark_height = imagesy($watermarkResource);
imagecopymerge($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height, $opacity);
else if($values[0] == 'ver')
$quality = intval($values[1]);
if($quality < 0) $quality = 85;
$oldSaveFile = $newSaveFile;
if ($this->manager->config['allow_newFileName'] && $this->manager->config['allow_overwrite'] == false)
// check whether a file already exist and if there is, create a variant of the filename
//get unique filename just returns the filename, so
//we need to make the relative path again.
if ($oldSaveFile != $newSaveFile)
$this->forcedNewName = $newName;
$this->forcedNewName = false;
$newSaveFullpath = $this->manager->getFullPath($newSaveFile);
$img->save($newSaveFullpath, $values[0], $quality);
//create the tmp image file
$newFullpath = $this->manager->getFullPath($newRelative);
$newURL = $this->manager->getFileURL($newRelative);
// when uploaded and not resized, rename and don't save
if ($action == "replace" && $percentage <= 0)
rename($fullpath, $newFullpath);
// when watermarked, save to new filename
elseif ($action == "watermark")
$functionName = 'image' . $img->type;
$functionName($imageResource, $newFullpath, 100);
$functionName($imageResource, $newFullpath);
echo "<script>alert(\"Error when saving '" . basename($newFullpath) . "' - Saving '" . $img->type . "' files not supported\");</script>";
$img->save($newFullpath);
// when uploaded was resized and saved, remove original
if ($action == "replace" && $percentage > 0)
//get the image information
$image['dimensions'] = $imgInfo[3];
$image['width'] = $imgInfo[0];
$image['height'] = $imgInfo[1];
$image['file'] = $newRelative;
$image['fullpath'] = $newFullpath;
* Get the file name base on the save name
* @param string $type image type, 'jpeg', 'png', or 'gif'
* @return string the filename according to save type
if(!isset ($_GET['file']))
$base = substr($filename,0,$index);
if($type == 'jpeg' && !($ext== 'jpeg' || $ext== 'jpg'))
if($type== 'png' && $ext != 'png')
if($type== 'gif' && $ext != 'gif')
* Get the default save file name, used by editor.php.
* @return string a suggestive filename, this should be unique
* Get a unique filename. If the file exists, the filename
* base is appended with an increasing integer.
* @param string $relative the relative filename to the base_dir
* @return string a unique filename in the current path
$fullpath = $this->manager->getFullPath($relative);
$ext = substr($file, $dotIndex);
$base = substr($file, 0, $dotIndex);
$filename = $base. '_'. $counter. $ext;
* Specifiy the original relative path, a new filename
* and return the new filename with relative path.
* i.e. $pathA (-filename) + $file
* @param string $pathA the relative file
* @param string $file the new filename
* @return string relative path with the new filename
$path = substr($pathA, 0, $index);
* Get the action GET parameter
* @return string action parameter
if(isset ($_GET['action']))
$action = $_GET['action'];
* Generate a unique string based on md5(microtime()).
* Well not so uniqe, as it is limited to 6 characters
* @return string unique string.
* Create unique tmp image file name.
* The filename is based on the tmp file prefix
* specified in config.inc.php plus
* the UID (basically a md5 of the remote IP)
* and some random 6 character string.
* This function also calls to clean up the tmp files.
* @param string $file the fullpath to a file
* @return string a unique filename for that path
* NOTE: it only returns the filename, path no included.
//make sure the the unique temp file does not exists
$this->cleanUp($path,$pathinfo['basename']);
* Delete any tmp image files.
* @param string $path the full path
* where the clean should take place.
$tmp = $this->manager->getTmpPrefix();
$prefix = $tmp. $this->_uid;
while (false !== ($entry = $d->read()))
if(substr($entry,0,$len)== $prefix && $entry != $file)
else if(substr($entry,0,$tmpLen)== $tmp && $entry != $file)
* Get the image type base on an image file.
* @param string $file the full path to the image file.
* @return string of either 'gif', 'jpeg', 'png' or 'bmp'
* otherwise it will return null.
$imageInfo = @getImageSize($file);
* Check if the specified image can be edit by GD
* mainly to check that GD can read and save GIFs
* @return int 0 if it is not a GIF file, 1 is GIF is editable, -1 if not editable.
$fullpath = $this->manager->getFullPath($relative);
* Check if GIF can be edit by GD.
* @return int 0 if it is not using the GD library, 1 is GIF is editable, -1 if not editable.
|