PHP開發(fā)的文字水印,縮略圖,圖片水印實(shí)現(xiàn)類與用法示例
本文實(shí)例講述了PHP開發(fā)的文字水印,縮略圖,圖片水印實(shí)現(xiàn)類與用法。分享給大家供大家參考,具體如下:
1.實(shí)現(xiàn)類ImageToTest.class.php參考代碼
class ImageToTest { /** * 圖片的基本信息 */ private $info; private $image; public function __construct($src){ $info = getimagesize($src); $this->info = array( '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); } /** * 操作圖片 (壓縮) */ public function thumb($width,$height){ $image_thumb = imagecreatetruecolor($width,$height); imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height, $this->info['width'],$this->info['height']); imagedestroy($this->image); $this->image = $image_thumb; } /** * 操作圖片(添加文字水?。? */ 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); } /** * 操作圖片(添加水印圖片) */ public function imageMark($source,$local,$alpha){ //1.獲取水印圖片的基本信息 $info2 = getimagesize($source); //2.通過水印的圖片編號(hào)來獲取水印的圖片類型 $type2 = image_type_to_extension($info2[2],false); //3.在內(nèi)存中創(chuàng)建一個(gè)和我們的水印圖像一致的圖像類型 $func2 = "imagecreatefrom{$type2}"; //4.把水印圖片復(fù)制到內(nèi)存中 $water = $func2($source); //5.合并圖片 imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha); //6.銷毀水印圖片 imagedestroy($water); } /** * 在瀏覽器中輸出圖片 */ public function show(){ header("Content-type:".$this->info['mime']); $funs = "image{$this->info['type']}"; $funs($this->image); } /** * 把圖片保存到硬盤里 */ public function save($newName){ $funs = "image{$this->info['type']}"; $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']); } /** * 銷毀圖片 使用析構(gòu)函數(shù) */ public function __destruct() { imagedestroy($this->image); } }
2.測(cè)試參考代碼
require_once('ImageToTest.class.php'); /*$src = './image/wbg.jpg'; $image = new ImageToTest($src); $image->thumb(700,550); $image->show();*/ /*$src2 = './image/wbg.jpg'; $content = 'SGC'; $font_url = './image/YGYcuhei.ttf'; $size = 33; $color = array( 0=>2, 1=>222, 2=>222, 3=>60 ); $local = array( 'x'=>20, 'y'=>100 ); $angle = 10; $image2 = new ImageToTest($src2); $image2->fontMark($content,$font_url,$size,$color,$local,$angle); $image2->show(); $image2->save('hahahah');*/ $src3 = './image/wbg.jpg'; $source = './image/water.jpg'; $local = array( 'x'=>20, 'y'=>100 ); $font_url = './image/YGYcuhei.ttf'; $size = 38; $color = array( 0=>2, 1=>222, 2=>222, 3=>60 ); $alpha = 60; $angle = 50; $image3 = new ImageToTest($src3); $image3->imageMark($source,$local,$alpha); $image3->thumb(700,550); $image3->fontMark('Hello',$font_url,$size,$color,$local,$angle); $image3->show(); $image3->save('WAWAWAWAWA');
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php gd2 上傳圖片/文字水印/圖片水印/等比例縮略圖/實(shí)現(xiàn)代碼
- php文字水印和php圖片水印實(shí)現(xiàn)代碼(二種加水印方法)
- php給圖片添加文字水印方法匯總
- php圖片添加文字水印實(shí)現(xiàn)代碼
- php給圖片加文字水印
- php面向?qū)ο笈c面向過程兩種方法給圖片添加文字水印
- PHP添加文字水印或圖片水印的水印類完整源代碼與使用示例
- PHP Imagick完美實(shí)現(xiàn)圖片裁切、生成縮略圖、添加水印
- php下圖片文字混合水印與縮略圖實(shí)現(xiàn)代碼
- php圖片處理:加水印、縮略圖的實(shí)現(xiàn)(自定義函數(shù):watermark、thumbnail)
相關(guān)文章
yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證詳解
這篇文章主要給大家介紹了關(guān)于yii2項(xiàng)目實(shí)戰(zhàn)之restful api授權(quán)驗(yàn)證的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05Mac環(huán)境下php操作mysql數(shù)據(jù)庫(kù)的方法分享
今天在mac上搭建好了php的環(huán)境,我們就把php操作mysql數(shù)據(jù)庫(kù)的方法分享給大家,有需要的小伙伴參考下。2015-05-05php輸出echo、print、print_r、printf、sprintf、var_dump的區(qū)別比較
本篇文章是對(duì)php輸出echo、print、print_r、printf、sprintf、var_dump的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06再Docker中架設(shè)完整的WordPress站點(diǎn)全攻略
這篇文章主要介紹了再Docker中架設(shè)完整的WordPress站點(diǎn)全攻略,Docker是當(dāng)下最火爆的虛擬機(jī)類技術(shù),需要的朋友可以參考下2015-07-07一個(gè)顯示某段時(shí)間內(nèi)每個(gè)月的方法 返回由這些月份組成的數(shù)組
一個(gè)顯示某段時(shí)間內(nèi)每個(gè)月的方法 返回由這些月份組成的數(shù)組,需要的朋友可以參考下2012-05-05php中獲取遠(yuǎn)程客戶端的真實(shí)ip地址的方法
在PHP 中使用 $_SERVER["REMOTE_ADDR"] 來取得客戶端的 IP 地址,但如果客戶端是使用代理服務(wù)器來訪問,那取到的就是代理服務(wù)器的 IP 地址,而不是真正的客戶端 IP 地址。2011-08-08