php圖片水印添加、壓縮、剪切的封裝類實(shí)現(xiàn)
php對圖片文件的操作主要是利用GD庫擴(kuò)展。當(dāng)我們頻繁利用php對圖片進(jìn)行操作時(shí),會(huì)自然封裝很多函數(shù),否則會(huì)寫太多重復(fù)的代碼。當(dāng)有很多對圖片的相關(guān)函數(shù)的時(shí)候,我們可以考慮將這些函數(shù)也整理一下,因而就有了封裝成類的想法。
操作圖片主要?dú)v經(jīng)四個(gè)步驟:
1、打開圖片
2、操作圖片
3、輸出圖片
4、銷毀圖片
1,3,4三個(gè)步驟每次都要寫,每次又都差不多。真正需要變通的只有操作圖片的這一步驟了。操作圖片又往往通過1或多個(gè)主要的GD函數(shù)來完成。
<?php class Image { private $info; private $image; public $type; public function __construct($src) { $this->info=getimagesize($src); $this->type=image_type_to_extension($this->info['2'],false); $fun="imagecreatefrom{$this->type}"; $this->image=$fun($src); } /** * 文字水印 * @param [type] $font 字體 * @param [type] $content 內(nèi)容 * @param [type] $size 文字大小 * @param [type] $col 文字顏色(四元數(shù)組) * @param array $location 位置 * @param integer $angle 傾斜角度 * @return [type] */ public function fontMark($font,$content,$size,$col,$location,$angle=0){ $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']); imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content); } /** * 圖片水印 * @param [type] $imageMark 水印圖片地址 * @param [type] $dst 水印圖片在原圖片中的位置 * @param [type] $pct 透明度 * @return [type] */ public function imageMark($imageMark,$dst,$pct){ $info2=getimagesize($imageMark); $type=image_type_to_extension($info2['2'],false); $func2="imagecreatefrom".$type; $water=$func2($imageMark); imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct); imagedestroy($water); } /** * 壓縮圖片 * @param [type] $thumbSize 壓縮圖片大小 * @return [type] [description] */ public function thumb($thumbSize){ $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]); imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']); imagedestroy($this->image); $this->image=$imageThumb; } /** * 裁剪圖片 * @param [type] $cutSize 裁剪大小 * @param [type] $location 裁剪位置 * @return [type] [description] */ public function cut($cutSize,$location){ $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]); imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]); imagedestroy($this->image); $this->image=$imageCut; } /** * 展現(xiàn)圖片 * @return [type] [description] */ public function show(){ header("content-type:".$this->info['mime']); $funn="image".$this->type; $funn($this->image); } /** * 保存圖片 * @param [type] $newname 新圖片名 * @return [type] [description] */ public function save($newname){ header("content-type:".$this->info['mime']); $funn="image".$this->type; $funn($this->image,$newname.'.'.$this->type); } public function __destruct(){ imagedestroy($this->image); } } ?>
是不是這樣操作就很簡單了,如果還需要其他操作,只需要再往這個(gè)類里面添加就好啦~~希望大家可以熟練掌握。
相關(guān)文章
php實(shí)現(xiàn)批量上傳數(shù)據(jù)到數(shù)據(jù)庫(.csv格式)的案例
下面小編就為大家?guī)硪黄猵hp實(shí)現(xiàn)批量上傳數(shù)據(jù)到數(shù)據(jù)庫(.csv格式)的案例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06php 使用mpdf實(shí)現(xiàn)指定字段配置字體樣式的方法
前兩天在做一個(gè)pdf導(dǎo)出功能,使用的插件是kartik-v/yii2-mpdf,此插件使用的是mpdf。接下來通過本文給大家介紹php 使用mpdf實(shí)現(xiàn)指定字段配置字體樣式的方法,需要的朋友可以參考下2019-07-07簡單的php中文轉(zhuǎn)拼音的實(shí)現(xiàn)代碼
這篇文章主要介紹了簡單的php中文轉(zhuǎn)拼音的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02在Mac OS上搭建PHP的Yii框架及相關(guān)測試環(huán)境
這篇文章主要介紹了在Mac OS上搭建PHP的Yii框架及相關(guān)測試環(huán)境的方法,Mac自帶Apache與PHP等軟件,因而搭建開發(fā)環(huán)境時(shí)非常舒適,需要的朋友可以參考下2016-02-02php桌面中心(一) 創(chuàng)建數(shù)據(jù)庫
php桌面中心(一) 創(chuàng)建數(shù)據(jù)庫...2007-03-03docker-compose部署php項(xiàng)目實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于docker-compose部署php項(xiàng)目的相關(guān)實(shí)例以及代碼內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2019-07-07ThinkPHP自動(dòng)填充實(shí)現(xiàn)無限級分類的方法
這篇文章主要介紹了ThinkPHP自動(dòng)填充實(shí)現(xiàn)無限級分類的方法,是ThinkPHP項(xiàng)目開發(fā)中非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-08-08迪菲-赫爾曼密鑰交換(Diffie–Hellman)算法原理和PHP實(shí)現(xiàn)版
這篇文章主要介紹了迪菲-赫爾曼密鑰交換(Diffie–Hellman)算法原理和PHP實(shí)現(xiàn)版,需要的朋友可以參考下2015-05-05