【opencart3源码分析】图像类image.php

<?php
/**
 * @package		OpenCart
 * @author		Daniel Kerr
 * @copyright	Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
 * @license		https://opensource.org/licenses/GPL-3.0
 * @link		https://www.opencart.com
*/

/**
* Image class
*/
class Image {
    // 文件
	private $file;
	// 图像
	private $image;
	// 宽度
	private $width;
	// 高度
	private $height;
	// 字节
	private $bits;
	// mime
	private $mime;

	/**
	 * Constructor
	 * 构造方法
	 * @param	string	$file
	 *
 	*/
	public function __construct($file) {
	    // 如果没有加载gd库
		if (!extension_loaded('gd')) {
			exit('Error: PHP GD is not installed!');
		}

		// 如果文件存在
		if (file_exists($file)) {
			$this->file = $file;
            // 获取图像大小
			$info = getimagesize($file);
            // 宽
			$this->width  = $info[0];
			// 高
			$this->height = $info[1];
			$this->bits = isset($info['bits']) ? $info['bits'] : '';
			$this->mime = isset($info['mime']) ? $info['mime'] : '';

			if ($this->mime == 'image/gif') {
			    // 创建gif格式图像
				$this->image = imagecreatefromgif($file);
			} elseif ($this->mime == 'image/png') {
			    // 创建png格式图像
				$this->image = imagecreatefrompng($file);
			} elseif ($this->mime == 'image/jpeg') {
			    //创建jpeg格式图像
				$this->image = imagecreatefromjpeg($file);
			}
		} else {
			exit('Error: Could not load image ' . $file . '!');
		}
	}
	
	/**
     * 
	 *  获取文件
	 * @return	string
     */
	public function getFile() {
		return $this->file;
	}

	/**
     * 
	 * 获取图像
	 * @return	array
     */
	public function getImage() {
		return $this->image;
	}
	
	/**
     * 
	 * 获取宽度
	 * @return	string
     */
	public function getWidth() {
		return $this->width;
	}
	
	/**
     * 
	 * 获取高度
	 * @return	string
     */
	public function getHeight() {
		return $this->height;
	}
	
	/**
     * 
	 * 获取字节
	 * @return	string
     */
	public function getBits() {
		return $this->bits;
	}
	
	/**
     * 
	 * 获取mime
	 * @return	string
     */
	public function getMime() {
		return $this->mime;
	}
	
	/**
     * 
     * 保存
     * @param	string	$file
	 * @param	int		$quality
     */
	public function save($file, $quality = 90) {
		$info = pathinfo($file);

		$extension = strtolower($info['extension']);
        // 如果是资源类型
		if (is_resource($this->image)) {
			if ($extension == 'jpeg' || $extension == 'jpg') {
                // 以jpeg格式将图像输出
			    imagejpeg($this->image, $file, $quality);
			} elseif ($extension == 'png') {
			    // 以png格式将图像输出
				imagepng($this->image, $file);
			} elseif ($extension == 'gif') {
			    //以gif格式将图像输出
				imagegif($this->image, $file);
			}
            // 销毁图像
			imagedestroy($this->image);
		}
	}
	
	/**
     * 
     * 重置大小
     * @param	int	$width
	 * @param	int	$height
	 * @param	string	$default
     */
	public function resize($width = 0, $height = 0, $default = '') {
		if (!$this->width || !$this->height) {
			return;
		}

		$xpos = 0;
		$ypos = 0;
		$scale = 1;

		$scale_w = $width / $this->width;
		$scale_h = $height / $this->height;

		if ($default == 'w') {
			$scale = $scale_w;
		} elseif ($default == 'h') {
			$scale = $scale_h;
		} else {
			$scale = min($scale_w, $scale_h);
		}

		if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') {
			return;
		}

		$new_width = (int)($this->width * $scale);
		$new_height = (int)($this->height * $scale);
		$xpos = (int)(($width - $new_width) / 2);
		$ypos = (int)(($height - $new_height) / 2);

		$image_old = $this->image;
		// 建立画布
		$this->image = imagecreatetruecolor($width, $height);

		if ($this->mime == 'image/png') {
		    // 设定图像的混色模式
			imagealphablending($this->image, false);
			// 设置保存PNG时保留透明通道信息
			imagesavealpha($this->image, true);
			// 为图像分配颜色和透明度
			$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
            // 将背景定义为透明色
			imagecolortransparent($this->image, $background);
		} else {
		    //分配颜色
			$background = imagecolorallocate($this->image, 255, 255, 255);
		}
        // 画一椭圆并填充
		imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
        // 重采样拷贝部分图像并调整大小
		imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);
        // 销毁图像
		imagedestroy($image_old);

		$this->width = $width;
		$this->height = $height;
	}
	
	/**
     * 
     * 添加水印
     * @param	string	$watermark
	 * @param	string	$position
     */
	public function watermark($watermark, $position = 'bottomright') {
		switch($position) {
			case 'topleft':
				$watermark_pos_x = 0;
				$watermark_pos_y = 0;
				break;
			case 'topcenter':
				$watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
				$watermark_pos_y = 0;
				break;
			case 'topright':
				$watermark_pos_x = $this->width - $watermark->getWidth();
				$watermark_pos_y = 0;
				break;
			case 'middleleft':
				$watermark_pos_x = 0;
				$watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
				break;
			case 'middlecenter':
				$watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
				$watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
				break;
			case 'middleright':
				$watermark_pos_x = $this->width - $watermark->getWidth();
				$watermark_pos_y = intval(($this->height - $watermark->getHeight()) / 2);
				break;
			case 'bottomleft':
				$watermark_pos_x = 0;
				$watermark_pos_y = $this->height - $watermark->getHeight();
				break;
			case 'bottomcenter':
				$watermark_pos_x = intval(($this->width - $watermark->getWidth()) / 2);
				$watermark_pos_y = $this->height - $watermark->getHeight();
				break;
			case 'bottomright':
				$watermark_pos_x = $this->width - $watermark->getWidth();
				$watermark_pos_y = $this->height - $watermark->getHeight();
				break;
		}
		
		imagealphablending( $this->image, true );
		imagesavealpha( $this->image, true );
		imagecopy($this->image, $watermark->getImage(), $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark->getWidth(), $watermark->getHeight());

		imagedestroy($watermark->getImage());
	}
	
	/**
     * 
     * 裁剪图像
     * @param	int		$top_x
	 * @param	int		$top_y
	 * @param	int		$bottom_x
	 * @param	int		$bottom_y
     */
	public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
		$image_old = $this->image;
		$this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
        // 拷贝图像
		imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height);
		imagedestroy($image_old);

		$this->width = $bottom_x - $top_x;
		$this->height = $bottom_y - $top_y;
	}
	
	/**
     * 
     * 旋转图像
     * @param	int		$degree
	 * @param	string	$color
     */
	public function rotate($degree, $color = 'FFFFFF') {
		$rgb = $this->html2rgb($color);

		$this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
        // 获取图像宽度
		$this->width = imagesx($this->image);
		// 获取图像高度
		$this->height = imagesy($this->image);
	}
	
	/**
     * 
     * 过滤
     */
	private function filter() {
	    // 函数参数列表
        $args = func_get_args();
        // 调用回调函数
        call_user_func_array('imagefilter', $args);
	}
	
	/**
     * 
     * 给图像添加文本
     * @param	string	$text
	 * @param	int		$x
	 * @param	int		$y 
	 * @param	int		$size
	 * @param	string	$color
     */
	private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
		$rgb = $this->html2rgb($color);
        // 水平地画一行字符串
		imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
	}
	
	/**
     * 
     * 合并图像
     * @param	object	$merge
	 * @param	object	$x
	 * @param	object	$y
	 * @param	object	$opacity
     */
	private function merge($merge, $x = 0, $y = 0, $opacity = 100) {
		imagecopymerge($this->image, $merge->getImage(), $x, $y, 0, 0, $merge->getWidth(), $merge->getHeight(), $opacity);
	}
	
	/**
     * 
     *
     * @param	string	$color
	 * 
	 * @return	array
     */
	private function html2rgb($color) {
		if ($color[0] == '#') {
			$color = substr($color, 1);
		}

		if (strlen($color) == 6) {
			list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
		} elseif (strlen($color) == 3) {
			list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
		} else {
			return false;
		}
        // 十六进制转为十进制
		$r = hexdec($r);
		$g = hexdec($g);
		$b = hexdec($b);

		return array($r, $g, $b);
	}
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/81431596