bmz_image_handler.class.php
来自「Zen Cart是真正的电子商务艺术」· PHP 代码 · 共 708 行 · 第 1/3 页
PHP
708 行
<?php/** * bmz_image_handler.class.php * IH2 class for image manipulation * * @author Tim Kroeger <tim@breakmyzencart.com> * @copyright Copyright 2005-2006 breakmyzencart.com * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0 * @version $Id: bmz_image_handler.class.php,v 1.8 2006/05/01 12:19:30 tim Exp $ */require_once('bmz_gif_info.class.php');class ih_image{ /** * $orig is the original image source passed to the constructor * $src = is the reference to an actual physical image * $local is the cached image reference */ var $orig = null; var $src = null; var $local = null; var $filename; var $extension; var $width; var $height; var $sizetype; var $canvas; var $zoom; var $watermark; var $force_canvas;/** * ih_image class constructor * @author Tim Kroeger (tim@breakmyzencart.com) * @version 1.99 * @param string $src Image source (e.g. - images/productimage.jpg) * @param string $width The image's width * @param string $height The image's height */ function ih_image($src, $width, $height){ global $ihConf; $this->orig = $src; $this->src = $src; $this->width = $width; $this->height = $height; $this->determine_image_sizetype(); if ((($this->sizetype == 'large') || ($this->sizetype == 'medium')) && $this->file_not_found()) { // large or medium image specified but not found. strip superflous suffix. // now we can actually access the default image referenced in the database. $this->src = $this->strip_sizetype_suffix($this->src); } $this->filename = $ihConf['dir']['docroot'] . $this->src; $this->extension = substr($this->src, strrpos($this->src, '.')); list($newwidth, $newheight, $resize) = $this->calculate_size($this->width, $this->height); // set canvas dimensions if (($newwidth > 0) && ($newheight > 0)) { $this->canvas['width'] = $newwidth; $this->canvas['height'] = $newheight; } // initialize overlays (watermark, zoom overlay) $this->initialize_overlays($this->sizetype); } # end class constructor function file_not_found() { global $ihConf; // try to find file by using different file extensions if initial // source doesn't succeed if (is_file($ihConf['dir']['docroot'] . $this->src)) { return false; } else { // do a quick search for files with common extensions $extensions = array('.png', '.PNG', '.jpg', '.JPG', '.jpeg', '.JPEG', '.gif', '.GIF'); $base = substr($this->src, 0, strrpos($this->src, '.')); for ($i=0; $i<count($extensions); $i++) { if (is_file($ihConf['dir']['docroot'] . $base . $extensions[$i])) { $this->src = $base . $extensions[$i]; return false; } } // not found? maybe mixed case file extension? if ($ihConf['allow_mixed_case_ext']) { // this can cost some time for every displayed image so default is // to not do this search $directory = dirname($ihConf['dir']['docroot'] . $this->src); $dir = @dir($directory); while ($file = $dir->read()) { if (!is_dir($directory . $file)) { if(preg_match("/^" . $ihConf['dir']['docroot'] . $base . "/i", $file) == '1') { $file_ext = substr($file, strrpos($file, '.')); if (is_file($ihConf['dir']['docroot'] . $base . $file_ext)) { $this->src = $base . $file_ext; return false; } } } } } } // still here? no file found... return true; } function is_real() { // return true if the source images are really present and medium // or large are not just a descendant from the default image. // small default images always return true. // strip file extensions, they don't matter $orig = substr($this->orig, 0, strrpos($this->orig, '.')); $src = substr($this->src, 0, strrpos($this->src, '.')); return ($orig == $src); } function determine_image_sizetype() { global $ihConf; if (strstr($this->src, $ihConf['large']['suffix'])) { $this->sizetype = 'large'; } elseif (strstr($this->src, $ihConf['medium']['suffix'])) { $this->sizetype = 'medium'; } elseif ((intval($this->width) == intval($ihConf['small']['width'])) && (intval($this->height) == intval($ihConf['small']['height']))) { $this->sizetype = 'small'; } else $this->sizetype = 'generic'; } function strip_sizetype_suffix($src) { global $ihConf; $src = preg_replace('/' . $ihConf['large']['suffix'] . '\./', '.', $src); $src = preg_replace('/' . $ihConf['medium']['suffix'] . '\./', '.', $src); $src = str_replace($ihConf['medium']['prefix'] . '/', '/', $src); $src = str_replace($ihConf['large']['prefix'] . '/', '/', $src); return $src; } function initialize_overlays($sizetype) { global $ihConf; switch ($sizetype) { case 'large': $this->watermark['file'] = ($ihConf['large']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'large/watermark' . $ihConf['large']['suffix'] . '.png' : ''; $this->zoom['file'] = ($ihConf['large']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'large/zoom' . $ihConf['large']['suffix'] . '.png' : ''; break; case 'medium': $this->watermark['file'] = ($ihConf['medium']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'medium/watermark' . $ihConf['medium']['suffix'] . '.png': ''; $this->zoom['file'] = ($ihConf['medium']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'medium/zoom' . $ihConf['medium']['suffix'] . '.png' : ''; break; case 'small': $this->watermark['file'] = ($ihConf['small']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'watermark.png' : ''; $this->zoom['file'] = ($ihConf['small']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'zoom.png' : ''; break; default: $this->watermark['file'] = ''; $this->zoom['file'] = ''; break; } if (($this->watermark['file'] != '') && is_file($this->watermark['file'])) { // set watermark parameters list($this->watermark['width'], $this->watermark['height']) = @getimagesize($this->watermark['file']); list($this->watermark['startx'], $this->watermark['starty']) = $this->calculate_gravity($this->canvas['width'], $this->canvas['height'], $this->watermark['width'], $this->watermark['height'], $ihConf['watermark']['gravity']); //echo '(' . $this->watermark['startx'] . ', ' . $this->watermark['starty'] . ') ' . $this->watermark['width'] . 'x' . $this->watermark['height'] . '<br />'; } else { $this->watermark['file'] = ''; } if (($this->zoom['file'] != '') && is_file($this->zoom['file'])) { // set zoom parameters list($this->zoom['width'], $this->zoom['height']) = @getimagesize($this->zoom['file']); list($this->zoom['startx'], $this->zoom['starty']) = $this->calculate_gravity($this->canvas['width'], $this->canvas['height'], $this->zoom['width'], $this->zoom['height'], $ihConf['zoom']['gravity']); //echo '(' . $this->zoom['startx'] . ', ' . $this->zoom['starty'] . ') ' . $this->zoom['width'] . 'x' . $this->zoom['height'] . '<br />'; } else { $this->zoom['file'] = ''; } } function get_local() { if ($this->local) return $this->local; // check if image handler is available and if we should resize at all if ($this->resizing_allowed()) { $this->local = $this->get_resized_image($this->width, $this->height); } else { $this->local = $this->src; } return $this->local; } function resizing_allowed() { global $bmzConf; global $ihConf; // only resize if resizing is turned on // don't resize template images so test for the configured images directory. // if $ihConf['noresize_key'] is found within the string, don't resize either. $allowed = false; if ($ihConf['resize'] && ((strpos($this->src, $ihConf['dir']['images']) === 0) || ((strpos($this->src, substr($bmzConf['cachedir'], strlen($ihConf['dir']['docroot']))) === 0))) && (strpos($this->src, $ihConf['noresize_key']) === false)) { $allowed = true; for ($i=0; $i++; $i<count($ihConf)) { $allowed &= (strpos($this->src, $ihConf['dir']['images'] . $ihConf['noresize_dirs'][$i] . '/') !== 0); } } return $allowed; } function get_resized_image($width, $height, $override_sizetype = '', $filetype = '') { global $ihConf; $sizetype = ($override_sizetype == '') ? $this->sizetype : $override_sizetype; switch ($sizetype) { case 'large': $file_extension = (($ihConf['large']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['large']['filetype']); $background = $ihConf['large']['bg']; $quality = $ihConf['large']['quality']; $width = $ihConf['large']['width']; $height = $ihConf['large']['height']; break; case 'medium': $file_extension = (($ihConf['medium']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['medium']['filetype']); $background = $ihConf['medium']['bg']; $quality = $ihConf['medium']['quality']; break; case 'small': $file_extension = (($ihConf['small']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['small']['filetype']); $background = $ihConf['small']['bg']; $quality = $ihConf['small']['quality'];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?