程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php文字水印和php圖片水印實現代碼

php文字水印和php圖片水印實現代碼

編輯:關於PHP編程

     有時上傳圖片時需要給網站加上水印,水印可以分為文字水印和圖片水印,下面就實現這二種水印

    文字水印   文字水印就是在圖片上加上文字,主要使用gd庫的imagefttext方法,並且需要字體文件。效果圖如下:   32.jpg 實現代碼如下:   代碼如下: $dst_path = 'dst.jpg';   //創建圖片的實例 $dst = imagecreatefromstring(file_get_contents($dst_path));   //打上文字 $font = './simsun.ttc';//字體 $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字體顏色 imagefttext($dst, 13, 0, 20, 20, $black, $font, '快樂編程');   //輸出圖片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) {     case 1://GIF         header('Content-Type: image/gif');         imagegif($dst);         break;     case 2://JPG         header('Content-Type: image/jpeg');         imagejpeg($dst);         break;     case 3://PNG         header('Content-Type: image/png');         imagepng($dst);         break;     default:         break; }   imagedestroy($dst);       圖片水印   圖片水印就是將一張圖片加在另外一張圖片上,主要使用gd庫的imagecopy和imagecopymerge。效果圖如下: 33.jpg   實現代碼如下:    代碼如下: $dst_path = 'dst.jpg'; $src_path = 'src.jpg';   //創建圖片的實例 $dst = imagecreatefromstring(file_get_contents($dst_path)); $src = imagecreatefromstring(file_get_contents($src_path));   //獲取水印圖片的寬高 list($src_w, $src_h) = getimagesize($src_path);   //將水印圖片復制到目標圖片上,最後個參數50是設置透明度,這裡實現半透明效果 imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50); //如果水印圖片本身帶透明色,則使用imagecopy方法 //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);   //輸出圖片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) {     case 1://GIF         header('Content-Type: image/gif');         imagegif($dst);         break;     case 2://JPG         header('Content-Type: image/jpeg');         imagejpeg($dst);         break;     case 3://PNG         header('Content-Type: image/png');         imagepng($dst);         break;     default:         break; }   imagedestroy($dst); imagedestroy($src);  
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved