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

來源:互聯網
上載者:User

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

文字浮水印 文字浮水印就是在圖片上加上文字,主要使用gd庫的imagefttext方法,並且需要字型檔。效果圖如下: 實現代碼如下: 代碼如下:$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。效果圖如下: 實現代碼如下:  代碼如下:$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); 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.