課程
/后端開發(fā)
/PHP
/GD庫實(shí)現(xiàn)圖片水印與縮略圖
給圖片添加水印的顏色只能是RGBM嗎
2016-10-27
源自:GD庫實(shí)現(xiàn)圖片水印與縮略圖 3-1
正在回答
<?phpclass Image {??? /*圖片的基本信息*/??? private $info;??? /*構(gòu)造函數(shù),打開圖片,讀取到內(nèi)存中*/??? 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){??????? //1.在內(nèi)存中建立一個真色彩圖片??????? $image_thumb = imagecreatetruecolor($width,$height);??????? //2.核心步驟,將原圖復(fù)制到新建的真色彩圖片上,并且按照一定比例壓縮??????? imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);??????? //3.銷毀原始圖片??????? imagedestroy($this->image);??????? $this->image = $image_thumb;??? }??? /*操作圖片(添加文字水印)*/??? public function fontmark($content,$font_url,$size,$color,$local,$angle){??????? //設(shè)置字體的顏色和透明度,參數(shù)1 內(nèi)存中的圖片 2 red 3 gleen 4 bule 5 透明度??????? $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){??????? $info2 = getimagesize($source);??????? $type2 = image_type_to_extension($info2[2],false);??????? $fun2 = "imagecreatefrom{$type2}";??????? $water = $fun2($source);??????? imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);??????? 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, $newname . "." . $this->info["type"]);??? }??? /*用析構(gòu)函數(shù)銷毀圖片*/??? public function __destruct(){??????? imagedestroy($this->image);??? }}
qq_Runningman_14242494 提問者
不是,可以參考php手冊
舉報
帶你快速高效的完成圖片處理工作,還可以加深對面向?qū)ο蟮睦斫?/p> 進(jìn)入課程
4 回答添加圖片水?。喉撁鏌o報錯,但是無顯示圖片
1 回答給圖片添加水印,為什么圖片格式更換了就不行呢?
1 回答圖片水印變形
4 回答imagecopymerge可以實(shí)現(xiàn)圖片水印
2 回答出不來水印也不保存圖片
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-10-28
<?php
class Image {
??? /*圖片的基本信息*/
??? private $info;
??? /*構(gòu)造函數(shù),打開圖片,讀取到內(nèi)存中*/
??? 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){
??????? //1.在內(nèi)存中建立一個真色彩圖片
??????? $image_thumb = imagecreatetruecolor($width,$height);
??????? //2.核心步驟,將原圖復(fù)制到新建的真色彩圖片上,并且按照一定比例壓縮
??????? imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
??????? //3.銷毀原始圖片
??????? imagedestroy($this->image);
??????? $this->image = $image_thumb;
??? }
??? /*操作圖片(添加文字水印)*/
??? public function fontmark($content,$font_url,$size,$color,$local,$angle){
??????? //設(shè)置字體的顏色和透明度,參數(shù)1 內(nèi)存中的圖片 2 red 3 gleen 4 bule 5 透明度
??????? $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){
??????? $info2 = getimagesize($source);
??????? $type2 = image_type_to_extension($info2[2],false);
??????? $fun2 = "imagecreatefrom{$type2}";
??????? $water = $fun2($source);
??????? imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
??????? 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, $newname . "." . $this->info["type"]);
??? }
??? /*用析構(gòu)函數(shù)銷毀圖片*/
??? public function __destruct(){
??????? imagedestroy($this->image);
??? }
}
2016-10-28
不是,可以參考php手冊