需求:業(yè)務(wù)流程中,用戶(hù)微信上傳圖片,展示的時(shí)候自動(dòng)添加上水印(地址,時(shí)間,備注等信息)
- 開(kāi)發(fā)思路1、將每個(gè)元素添加分別單獨(dú)到背景圖片上,調(diào)整位置和透明度
- 開(kāi)發(fā)思路2、將水印信息先整合為一張水印圖片,再將水印圖片添加到背景圖片,調(diào)整位置和透明度
因?yàn)榭紤]到不同圖片尺寸大小等問(wèn)題,元素內(nèi)容又多,根據(jù)思路1,定位問(wèn)題太麻煩,而且還因調(diào)整大小等因素,決定先整合為一張水印圖片
1、參考水印相機(jī),確認(rèn)水印模板
本來(lái)水印圖片打算全部用代碼生成的,開(kāi)發(fā)的過(guò)程中發(fā)現(xiàn)元素太多,背景色也上下分層,代碼可以實(shí)現(xiàn),但是ps更有性?xún)r(jià)比,于是打算先用ps畫(huà)一個(gè)水印模板,然后在利用代碼添加元素。
這個(gè)是參考模板,水印相機(jī)生成的照片(模仿他的樣子開(kāi)發(fā),抄誰(shuí)不會(huì)?。?br>
這個(gè)是ps做的水印模板圖片,這些元素固定,開(kāi)發(fā)時(shí)只需要再增加時(shí)間,地點(diǎn)和備注信息。
2、水印圖片上添加時(shí)間、地點(diǎn)備注信息
添加文字水印,函數(shù)imagefttext需要用到字體,在電腦上C:\Windows\fonts里面黑體【simhei.ttf】,效果于水印圖片上文字不一致,看了ps源文件,是Adobe 黑體 std字體,于是網(wǎng)上找了【AdobeHeitiStd-Regular.otf】(強(qiáng)迫癥患者,要保持一致的嘛)。
$font_src = 'D:\php\wwwroot\127.0.0.1\device\font\AdobeHeitiStd-Regular.otf';
$image_logo_src = './image/logo.png';
$time = date('Y-m-d H:i:s');
$watermark_address = '上海市上海市上海上海館';
$watermark_notes = '這是一個(gè)備注,這是一個(gè)備注,這是一個(gè)備注';
//創(chuàng)建水印圖片
$image_logo = imagecreatefrompng($image_logo_src);
//獲取水印尺寸
$image_logo_info = getimagesize($image_logo_src);
$image_logo_width = $image_logo_info[0];
$image_logo_height = $image_logo_info[1];
//設(shè)置字體顏色
$white = imagecolorallocate($image_logo, 255, 255, 255);
$blue = imagecolorallocate($image_logo, 100, 154, 222);
//根據(jù)位置往圖片中寫(xiě)入文字
imagefttext( resource $image, float $size, float $angle,
int $x, int $y, int $color, string $fontfile,
string $text, array $extrainfo);
imagefttext($image_logo, 30, 0, 240, 155, $white, $font_src, $time);
imagefttext($image_logo, 38, 0, 200, 278, $blue, $font_src, $watermark_address);
imagefttext($image_logo, 20, 0, 150, 340, $blue, $font_src, $watermark_notes);
生成的水印照片,時(shí)間,地點(diǎn)和備注在添加的時(shí)候,用imagefttext來(lái)設(shè)置位置參數(shù)。
- $image:它指定要處理的圖像。
- $size:它指定要使用的字體大小(以磅為單位)。
- $angle:它以度為單位指定角度。
- $x:它指定x坐標(biāo)。
- $y:它指定y坐標(biāo)。
- $color:它指定文本所需顏色的索引。
- $fontfile:它指定要使用的字體。
- $text:它指定要寫(xiě)入的文本。
- $extrainfo (Optional):它指定了額外的信息。
3、創(chuàng)建背景圖片
imagecreatefrom 系列函數(shù)用于從文件或 URL 載入一幅圖像,成功返回圖像資源,失敗則返回一個(gè)空字符串。
如果imagecreatefrom后邊跟照片類(lèi)型,如果是jpg更換為jpeg
- imagecreatefromgif():創(chuàng)建一塊畫(huà)布,并從 GIF 文件或 URL 地址載入一副圖像
- imagecreatefromjpeg():創(chuàng)建一塊畫(huà)布,并從 JPEG 文件或 URL 地址載入一副圖像
- imagecreatefrompng():創(chuàng)建一塊畫(huà)布,并從 PNG 文件或 URL 地址載入一副圖像
- imagecreatefromwbmp():創(chuàng)建一塊畫(huà)布,并從WBMP 文件或 URL 地址載入一副圖像
- imagecreatefromstring():創(chuàng)建一塊畫(huà)布,并從字符串中的圖像流新建一副圖像
$image_bg_src = './image/1.jpg';
$image_bg_info = getimagesize($image_bg_src);
$image_type = explode('/', $image_bg_info['mime'])[1];
$image_type = $image_type == 'jpg' ? 'jpeg' : $image_type;
$image_bg_width = $image_bg_info[0];
$image_bg_height = $image_bg_info[1];
$image_fun_info = "imagecreatefrom" . $image_type;
$image_canvas = $image_fun_info($image_bg_src);
4、水印調(diào)整格式
計(jì)算水印圖片的寬高,理想情況下無(wú)論背景圖片 大小、寬高 如果改變,水印只占據(jù)圖片的左下角
- 如果背景圖片 寬>高,水印圖片 寬 為背景圖片的 0.5(自己看效果定),按照比例求出水印圖片的高(新寬 / 舊寬 * 舊高)
- 如果背景圖片 高>寬,水印圖片 高 為背景圖片的 0.3(自己看效果定),按照比例求出水印圖片的寬(新高 / 舊高 * 舊寬)
根據(jù)圖片重新調(diào)整水印的大小
- imagecopy() 拷貝圖像資源的一部分
- imagecopyresampled() 重采樣拷貝部分圖像并調(diào)整大小
- imagecopyresized 拷貝部分圖像并調(diào)整大小
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
- dst_image 目標(biāo)圖象連接資源。
- src_image 源圖象連接資源。
- dst_x 目標(biāo) X 坐標(biāo)點(diǎn)。
- dst_y 目標(biāo) Y 坐標(biāo)點(diǎn)。
- src_x 源的 X 坐標(biāo)點(diǎn)。
- src_y 源的 Y 坐標(biāo)點(diǎn)。
- dst_w 目標(biāo)寬度。
- dst_h 目標(biāo)高度。
- src_w 源圖象的寬度。
- src_h 源圖象的高度。
//計(jì)算比例
if ($image_bg_width > $image_bg_height) {
$image_logo_height_new = round($image_bg_height * 0.3);
$image_logo_width_new = round($image_logo_height_new / $image_logo_height * $image_logo_width);
$offset = $image_bg_height * 0.03;
} else {
$image_logo_width_new = round($image_bg_width * 0.5);
$image_logo_height_new = round($image_logo_width_new / $image_logo_width * $image_logo_height);
$offset = $image_bg_width * 0.03;
}
$image_logo_new = imagecreatetruecolor($image_logo_width_new, $image_logo_height_new);
imagecopyresampled($image_logo_new, $image_logo, 0, 0, 0, 0, $image_logo_width_new, $image_logo_height_new, $image_logo_width, $image_logo_height);
5、將水印放在背景圖上
imagecopymerge()函數(shù)是一個(gè)強(qiáng)大的PHP圖像處理函數(shù),可以用于將兩個(gè)圖像合并到一個(gè)單一的圖像中,并在混合時(shí)應(yīng)用透明度。imagecopymerge()函數(shù)是通過(guò)按指定比例在兩個(gè)圖像之間繪制一個(gè)透明的GIF來(lái)實(shí)現(xiàn)的。
- 如果背景圖片 寬>高,水印圖片距離左下角 左邊 和 底部 的距離為 背景圖片 高 的 3%(根據(jù)實(shí)際效果可微調(diào))
- 如果背景圖片 高>寬,水印圖片距離左下角 底部 和 左邊 的距離為 背景圖片 寬 的 3%(根據(jù)實(shí)際效果可微調(diào))
imagecopymerge($image_canvas, $image_logo_new, $offset, $image_bg_height - $offset - $image_logo_height_new, 0, 0, $image_logo_width_new, $image_logo_height_new, 75);
#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 )
- dst_im 目標(biāo)圖像
- src_im 被拷貝的源圖像
- dst_x 目標(biāo)圖像開(kāi)始 x 坐標(biāo)
- dst_y 目標(biāo)圖像開(kāi)始 y 坐標(biāo),x,y同為 0 則從左上角開(kāi)始
- src_x 拷貝圖像開(kāi)始 x 坐標(biāo)
- src_y 拷貝圖像開(kāi)始 y 坐標(biāo),x,y同為 0 則從左上角開(kāi)始拷貝
- src_w (從 src_x 開(kāi)始)拷貝的寬度
- src_h (從 src_y 開(kāi)始)拷貝的高度
- pct 圖像合并程度,取值 0-100,當(dāng) pct=0 時(shí),實(shí)際上什么也沒(méi)做,反之完全合并.
6、全部代碼和效果
-
寬>高
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-830677.html
-
高>寬
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830677.html
$font_src = 'D:\php\wwwroot\127.0.0.1\device\font\AdobeHeitiStd-Regular.otf';
$time = date('Y-m-d H:i:s');
$image_logo_src = './image/logo.png';
$image_bg_src = './image/1.jpg';
$watermark_address = '上海市上海市上海上海館';
$watermark_notes = '這是一個(gè)備注,這是一個(gè)備注,這是一個(gè)備注';
$image_bg_info = getimagesize($image_bg_src);
$image_type = explode('/', $image_bg_info['mime'])[1];
$image_type = $image_type == 'jpg' ? 'jpeg' : $image_type;
$image_bg_width = $image_bg_info[0];
$image_bg_height = $image_bg_info[1];
$image_fun_info = "imagecreatefrom" . $image_type;
$image_canvas = $image_fun_info($image_bg_src);
//創(chuàng)建一個(gè)圖片
$image_logo = imagecreatefrompng($image_logo_src);
//創(chuàng)建logo
$image_logo_info = getimagesize($image_logo_src);
$image_logo_width = $image_logo_info[0];
$image_logo_height = $image_logo_info[1];
//設(shè)置字體顏色
$white = imagecolorallocate($image_logo, 255, 255, 255);
$blue = imagecolorallocate($image_logo, 100, 154, 222);
//往圖片中寫(xiě)入文字
imagefttext($image_logo, 30, 0, 240, 155, $white, $font_src, $time);
imagefttext($image_logo, 38, 0, 200, 278, $blue, $font_src, $watermark_address);
imagefttext($image_logo, 20, 0, 150, 340, $blue, $font_src, $watermark_notes);
//計(jì)算比例
if ($image_bg_width > $image_bg_height) {
$image_logo_height_new = round($image_bg_height * 0.3);
$image_logo_width_new = round($image_logo_height_new / $image_logo_height * $image_logo_width);
$offset = $image_bg_height * 0.03;
} else {
$image_logo_width_new = round($image_bg_width * 0.5);
$image_logo_height_new = round($image_logo_width_new / $image_logo_width * $image_logo_height);
$offset = $image_bg_width * 0.03;
}
//改變水印的尺寸
$image_logo_new = imagecreatetruecolor($image_logo_width_new, $image_logo_height_new);
imagecopyresampled($image_logo_new, $image_logo, 0, 0, 0, 0, $image_logo_width_new, $image_logo_height_new, $image_logo_width, $image_logo_height);
//將水印放到圖片的合適位置
imagecopymerge($image_canvas, $image_logo_new, $offset, $image_bg_height - $offset - $image_logo_height_new, 0, 0, $image_logo_width_new, $image_logo_height_new, 75);
header('Content-Type: image/' . $image_type);
$image_fun_out = "image" . $image_type;
$image_fun_out($image_canvas);
imagedestroy($image_canvas);
imagedestroy($image_logo);
imagedestroy($image_logo_new);
到了這里,關(guān)于圖片添加水印,仿照水印相機(jī)樣式,定位時(shí)間備注等的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!