php實現圖片添加文字浮水印程式碼

來源:互聯網
上載者:User
 代碼如下 複製代碼


/*PHP圖片加文字浮水印類庫
該類庫暫時只支援文字浮水印,位置為右下角,顏色隨機

調用方法:
1、在需要加浮水印的檔案頂部引入類庫:
include_once 'imageClass.php';
2、聲明新類:
$tpl=new image_fu;
3、給圖片浮水印提供參數:
$tpl->img(圖片路徑,浮水印文字,字型路徑,字型大小,字型角度);
比如:$tpl->img('abc.jpg','這是浮水印文字','ziti.ttf',30,0)
*/

代碼如下:

watermark.php
<?php
class image_fu{

private $image;
private $img_info;
private $img_width;
private $img_height;
private $img_im;
private $img_text;
private $img_ttf='';
private $img_new;
private $img_text_size;
private $img_jd;

function img($img='',$txt='',$ttf='',$size=12,$jiaodu=0){
if(isset($img)&&file_exists($img)){//檢測圖片是否存在
$this->image   =$img;
$this->img_text=$txt;
$this->img_text_size=$size;
$this->img_jd=$jiaodu;
if(file_exists($ttf)){
$this->img_ttf=$ttf;
}else{
exit('字型檔:'.$ttf.'不存在!');
}
$this->imgyesno();
}else{
exit('圖片檔案:'.$img.'不存在');
}
}

private function imgyesno(){

$this->img_info  =getimagesize($this->image);
$this->img_width =$this->img_info[0];//圖片寬
$this->img_height=$this->img_info[1];//圖片高

//檢測圖片類型
switch($this->img_info[2]){
case 1:$this->img_im = imagecreatefromgif($this->image);break;
case 2:$this->img_im = imagecreatefromjpeg($this->image);break;
case 3:$this->img_im = imagecreatefrompng($this->image);break;
default:exit('圖片格式不支援浮水印');
}

$this->img_text();
}

private function img_text(){

imagealphablending($this->img_im,true);

//設定顏色
$color=imagecolorallocate($this->img_im,rand(0,255),rand(0,255),rand(0,255));
$txt_height=$this->img_text_size;
$txt_jiaodu=$this->img_jd;
$ttf_im=imagettfbbox($txt_height,$txt_jiaodu,$this->img_ttf,$this->img_text);
$w = $ttf_im[2] - $ttf_im[6];
$h = $ttf_im[3] - $ttf_im[7];
//$w = $ttf_im[7];
//$h = $ttf_im[8];

unset($ttf_im);

$txt_y     =$this->img_height-$h;
$txt_x     =$this->img_width-$w;
//$txt_y     =0;
//$txt_x     =0;

$this->img_new=@imagettftext($this->img_im,$txt_height,$txt_jiaodu,$txt_x,$txt_y,$color,

$this->img_ttf,$this->img_text);

@unlink($this->image);//刪除圖片
switch($this->img_info[2]) {//取得背景圖片的格式
case 1:imagegif($this->img_im,$this->image);break;
case 2:imagejpeg($this->img_im,$this->image);break;
case 3:imagepng($this->img_im,$this->image);break;
default: exit('浮水印圖片失敗');
}

}

//顯示圖片
function img_show(){echo '<img src="'.$this->image.'" border="0" alt="'.$this->img_text.'" />';}

//釋放記憶體
private function img_nothing(){
unset($this->img_info);
imagedestroy($this->img_im);
}

}

$addwk    =    new image_fu();
$addwk->img('2.jpg','testwatermark','華康少女字型.ttf',33,0);
$addwk->img_show();   //調用顯示圖片

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.