<?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); } }
GD庫封裝的處理圖片工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.