<?php namespace vendor\gd\xx; /** * GDImg is the model behind the image. * * //设定图片尺寸 * $width = 300; * $height = 150; * //图片一 * $src = ‘001.jpg‘; * //$content = ‘hello‘; * //$font_url = ‘arial.ttf‘; * //$size = 20; * //$color = [255,255,255,20]; * //$local = [‘x‘ => 20, ‘y‘ => 30]; * //$angle = 10; * //图片二 * $source = "002.jpg"; * $local = [‘x‘ => 0, ‘y‘ => $height]; //图片位置 * $alpha = 100; //图片透明度 * * $image = new Image($src); * // TODO:以下宽高为配置的主面板显示宽高和图片一宽高 * $image->thumb($width, $height*2, $width, $height); //压缩图片 * //$image->fontMark($content, $font_url, $size, $color, $local, $angle); //合成文字水印 * $image->imageMark($source, $local, $alpha, $width, $height); //合成图片水印并压缩 * $image->show(); //打印在浏览器 * //$image->save(‘abc‘); //保存在硬盘中 * */ class GDImg extends Model{ /** * 内存中的图片 */ private $image; /** * 图片基本信息 */ public $info; /** * 打开一张图片,读取到内存中 * @ $src为图片本地路径 */ public function __construct($src){ $info = getimagesize($src); $this->info = [ ‘width‘ => $info[0], ‘height‘ => $info[1], ‘type‘ => image_type_to_extension($info[‘2‘], false), ‘mime‘ => $info[‘mime‘], ]; $fun = "imagecreatefrom{$this->info[‘type‘]}"; $this->image = $fun($src); } /** * 操作图片(压缩) * @ $width为面板宽度 * @ $height为面板高度 * @ $width2为图片一宽度 * @ $height2为图片一高度 */ public function thumb($width, $height, $width2, $height2){ $image_thumb = imagecreatetruecolor($width, $height); imagecopyresampled($image_thumb, $this->image, 0, 0, 0, 0, $width2, $height2, $this->info[‘width‘], $this->info[‘height‘]); imagedestroy($this->image); $this->image = $image_thumb; } /** * 操作图片(添加文字水印) * @ $content为文字内容 * @ $font_url为字体路径 * @ $size为字体大小 * @ $color为字体颜色 * @ $local为字体位置 */ public function fontMark($content, $font_url, $size, $color, $local, $angle){ $col = imagecolorallocatealpha($this->image, $color[0], $color[1], $color[2], $color[3]); imagettftext($this->image, $size, $angle, $local[‘x‘], $local[‘y‘], $col, $font_url, $content); } /** * 操作图片(添加图片水印) * @ $source为本地图片路径 * @ $local为图片位置 * @ $alpha为图片透明度 */ public function imageMark($source, $local, $alpha, $width, $height){ $info2 = getimagesize($source); $type2 = image_type_to_extension($info2[2], false); $fun2 = "imagecreatefrom{$type2}"; $water = $fun2($source); //压解水印 $image_thumb2 = imagecreatetruecolor($width, $height); imagecopyresampled($image_thumb2, $water, 0, 0, 0, 0, $width, $height, $this->info[‘width‘], $this->info[‘height‘]); imagecopymerge($this->image, $image_thumb2, $local[‘x‘], $local[‘y‘], 0, 0, $info2[0], $info2[1], $alpha); imagedestroy($water); } /** * 在浏览器中显示图片 */ public function show(){ header("Content-type:". $this->info[‘mime‘]); $fun = "image{$this->info[‘type‘]}"; $fun($this->image); } /** * 把图片保存在硬盘 * @ $newname为保存图片的名称 */ public function save($newname){ $fun = "image{$this->info[‘type‘]}"; $fun($this->image, $newname.‘.‘.$this->info[‘type‘]); } /** * 销毁图片 */ public function __destruct(){ imagedestroy($this->image); } }
本文出自 “Shows technology” 博客,请务必保留此出处http://wangzhijun.blog.51cto.com/9660708/1702036
原文:http://wangzhijun.blog.51cto.com/9660708/1702036