日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php给图片加图片水印,php给图片添加文字或图片水印实现代码

發布時間:2025/3/20 php 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php给图片加图片水印,php给图片添加文字或图片水印实现代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原標題:php給圖片添加文字或圖片水印實現代碼

一、文字水印

文字水印就是在圖片上加上文字,主要使用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);

新創建一張圖片,然后打印文字水印:

// imagecreatefromstring

// imageCreateFromPng Create a new image from file or URL 創建圖片對象

// Create a 300x100 image,新創建一張圖片

$im = imagecreatetruecolor(500, 300);

// set color

$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);

$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Make the background red

// function imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color) {}

imagefilledrectangle($im, 0, 0, 300, 100, $red);

// Path to our ttf font file

$font_file = './font/Arial.ttf';

// imagefttext ($image, $size, $angle, $x, $y, $color, $fontfile, $text, $extrainfo = null )

// Draw the text 'PHP Manual' using font size 13

imagefttext($im, 13, 0, 150, 50, $black, $font_file, 'PHP Manual');

// Output image to the browser

header('Content-Type: image/png');

imagepng($im);

imagedestroy($im);

二、圖片水印

圖片水印就是將一張圖片加在另外一張圖片上,主要使用gd庫的imagecopy和imagecopymerge。

$dst_path = 'myimage.jpg';

$src_path = '';

//創建圖片的實例

$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, 30);

//如果水印圖片本身帶透明色,則使用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);

效果圖:

三、其他有關圖像處理的函數

/*

*返回圖像的大小及圖像類型

// Get the size of an image

$size = getimagesize("?750x956_120");

print_r($size);

// 打印結果

Array

(

[0] => 750

[1] => 956

[2] => 2

[3] => width="750" height="956"

[bits] => 8

[channels] => 3

[mime] => image/jpeg

)

*/

/**

* imagecopy — 拷貝圖像的一部分

*/

// bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

// 將 src_im 圖像中坐標從 src_x,src_y 開始,寬度為 src_w,高度為 src_h 的一部分拷貝到 dst_im 圖像中坐標為 dst_x 和 dst_y 的位置上。

/*

*

* http://php.net/manual/zh/function.imagecopymerge.php

*imagecopymerge — 拷貝并合并圖像的一部分

*bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

*

*

責任編輯:

總結

以上是生活随笔為你收集整理的php给图片加图片水印,php给图片添加文字或图片水印实现代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。