📄 imageeditor.php
字号:
<?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 $ * @package ImageManager */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 $ * @package ImageManager * @subpackage Editor */class ImageEditor { /** * ImageManager instance. */ var $manager; /** * user based on IP address */ var $_uid; /** * tmp file storage time. */ var $lapse_time =900; //15 mins var $filesaved = 0; /** * 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. */ function ImageEditor($manager) { $this->manager = $manager; $this->_uid = md5($_SERVER['REMOTE_ADDR']); } /** * Did we save a file? * @return int 1 if the file was saved sucessfully, * 0 no save operation, -1 file save error. */ function isFileSaved() { Return $this->filesaved; } /** * 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> */ function processImage($uploadedRelative) { if (isset($uploadedRelative) && $uploadedRelative != "") { $relative = $uploadedRelative; } elseif(isset($_GET['img'])) { $relative = rawurldecode($_GET['img']); } else { Return array(); } $imgURL = $this->manager->getFileURL($relative); $fullpath = $this->manager->getFullPath($relative); $imgInfo = @getImageSize($fullpath); if(!is_array($imgInfo)) Return array(); $action = $this->getAction(); if(!is_null($action)) { $image = $this->processAction($action, $relative, $fullpath); } else { $image['src'] = $imgURL; $image['dimensions'] = $imgInfo[3]; $image['width'] = $imgInfo[0]; $image['height'] = $imgInfo[1]; $image['file'] = $relative; $image['fullpath'] = $fullpath; } Return $image; } /** * 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> */ function processAction($action, $relative, $fullpath) { $params = ''; if(isset($_GET['params'])) $params = $_GET['params']; $values = explode(',',$params,4); $saveFile = $this->getSaveFileName($values[0]); $img = Image_Transform::factory(IMAGE_CLASS); $img->load($fullpath); switch ($action) { case 'replace': // '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); } break; case 'watermark': // loading target image $functionName = 'ImageCreateFrom' . $img->type; if(function_exists($functionName)) { $imageResource = $functionName($fullpath); } else { echo "<script>alert(\"Error when loading '" . basename($fullpath) . "' - Loading '" . $img->type . "' files not supported\");</script>"; return false; } // loading watermark $watermarkFullPath = $_GET['watermarkFullPath']; $watermarkImageType = strtolower(substr($watermarkFullPath, strrpos($watermarkFullPath, ".") + 1)); if ($watermarkImageType == "jpg") { $watermarkImageType = "jpeg"; } if ($watermarkImageType == "tif") { $watermarkImageType = "tiff"; } $functionName = 'ImageCreateFrom' . $watermarkImageType; if(function_exists($functionName)) { $watermarkResource = $functionName($watermarkFullPath); } else { echo "<script>alert(\"Error when loading '" . basename($watermarkFullPath) . "' - Loading '" . $img->type . "' files not supported\");</script>"; return false; } $numberOfColors = imagecolorstotal($watermarkResource); $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'); $watermarkAPI = new watermark(); $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 else { $watermark_width = imagesx($watermarkResource); $watermark_height = imagesy($watermarkResource); imagecopymerge($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height, $opacity); } break; case 'crop': $img->crop(intval($values[0]),intval($values[1]), intval($values[2]),intval($values[3])); break; case 'scale': $img->resize(intval($values[0]),intval($values[1])); break; case 'rotate': $img->rotate(floatval($values[0])); break; case 'flip': if ($values[0] == 'hoz') $img->flip(true); else if($values[0] == 'ver') $img->flip(false); break; case 'save': if(!is_null($saveFile)) { $quality = intval($values[1]); if($quality <0) $quality = 85; $newSaveFile = $this->makeRelative($relative, $saveFile); $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 $newName = $this->getUniqueFilename($newSaveFile); //get unique filename just returns the filename, so //we need to make the relative path again. $newSaveFile = $this->makeRelative($relative, $newName); // forced new name? if ($oldSaveFile != $newSaveFile) { $this->forcedNewName = $newName; } else { $this->forcedNewName = false; } } $newSaveFullpath = $this->manager->getFullPath($newSaveFile); $img->save($newSaveFullpath, $values[0], $quality); if(is_file($newSaveFullpath)) $this->filesaved = 1; else $this->filesaved = -1; } break; } //create the tmp image file $filename = $this->createUnique($fullpath); $newRelative = $this->makeRelative($relative, $filename); $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") { // save image $functionName = 'image' . $img->type; if(function_exists($functionName)) { if($type=='jpeg') $functionName($imageResource, $newFullpath, 100); else $functionName($imageResource, $newFullpath); } else { echo "<script>alert(\"Error when saving '" . basename($newFullpath) . "' - Saving '" . $img->type . "' files not supported\");</script>";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -