亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
Add a Text Watermark
Add an Image Watermark (Logo or PNG)
Notes and Tips
首頁 後端開發(fā) php教程 如何在PHP中的圖像中添加水印

如何在PHP中的圖像中添加水印

Sep 15, 2025 am 03:26 AM
php 水印

使用PHP的GD庫可為圖片添加水印。首先加載原圖和水印(文字或圖像),再用imagecopy()或imagettftext()合併,最後保存輸出。支持JPEG、PNG等格式,注意處理透明度和字體路徑,確保GD擴展已啟用。

How to add a watermark to an image in php

To add a watermark to an image in PHP, you can use the GD library, which is commonly available in most PHP installations. The process involves loading the original image and the watermark (text or image), then merging them using image functions. Below are steps and examples for both text and image watermarks.

Add a Text Watermark

Use this method if you want to overlay text (like a copyright notice) on an image.

  • Create or load the source image using imagecreatefromjpeg() , imagecreatefrompng() , or similar.
  • Allocate a color for the text using imagecolorallocate() .
  • Use imagestring() or imagettftext() to add text to the image.
  • Output or save the final image with imagejpeg() or imagepng() .

Example:

// Load the image
$im = imagecreatefromjpeg('photo.jpg');
<p>// Set the watermark text
$text = '? MySite.com';</p> <p>// Set font size and font path (TTF font recommended)
$font = 'arial.ttf';
$font_size = 20;</p> <p>// Allocate color (R, G, B)
$color = imagecolorallocate($im, 255, 255, 255); // White text</p> <p>// Get image dimensions
$width = imagesx($im);
$height = imagesy($im);</p> <p>// Calculate position (bottom-right)
$text_box = imagettfbbox($font_size, 0, $font, $text);
$text_width = $text_box[4] - $text_box[0];
$x = $width - $text_width - 10;
$y = $height - 10;</p> <p>// Add the text
imagettftext($im, $font_size, 0, $x, $y, $color, $font, $text);</p> <p>// Save the image
imagejpeg($im, 'watermarked.jpg', 90);</p> <p>// Free memory
imagedestroy($im);</p>

Add an Image Watermark (Logo or PNG)

Use this to overlay a transparent PNG logo or icon onto your image.

  • Load both the main image and the watermark image.
  • Use imagecopy() or imagecopymerge() to place the watermark.
  • Adjust position and opacity as needed.
  • Save the result.

Example:

// Load the main image
$main = imagecreatefromjpeg('photo.jpg');
<p>// Load the watermark (PNG with transparency)
$watermark = imagecreatefrompng('logo.png');</p> <p>// Get dimensions
$main_w = imagesx($main);
$main_h = imagesy($main);
$watermark_w = imagesx($watermark);
$watermark_h = imagesy($watermark);</p> <p>// Set position (eg, bottom-right)
$dest_x = $main_w - $watermark_w - 10;
$dest_y = $main_h - $watermark_h - 10;</p> <p>// Merge watermark (100% opacity; use imagecopymerge for transparency)
imagecopy($main, $watermark, $dest_x, $dest_y, 0, 0, $watermark_w, $watermark_h);</p> <p>// Save the image
imagejpeg($main, 'watermarked.jpg', 90);</p> <p>// Free memory
imagedestroy($main);
imagedestroy($watermark);</p>

Notes and Tips

Ensure the GD extension is enabled in your PHP setup. Handle different image types (JPEG, PNG, GIF) carefully, as they require proper creation and saving functions. For PNGs, preserve transparency by using imagealphablending() and imagesavealpha() . Also, always validate and sanitize input if users upload images.

Basically just load, blend, and save — doesn't need to be complicated.

以上是如何在PHP中的圖像中添加水印的詳細內容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

如何在PHP中迴聲HTML標籤 如何在PHP中迴聲HTML標籤 Sep 29, 2025 am 02:25 AM

使用單引號或轉義雙引號在PHP中輸出HTML,推薦用單引號包裹字符串以避免屬性引號衝突,可結合變量拼接或heredoc語法生成動態(tài)內容。

如何使用PHP中的GET請求變量? 如何使用PHP中的GET請求變量? Sep 29, 2025 am 01:30 AM

Use$_GETtoaccessURLquerystringvariablesinPHP,suchasname=Johnandage=30fromhttps://example.com/search.php?name=John&age=30;alwaysvalidateandsanitizeinputsusingfilter_input()andavoidsensitivedatainURLsduetoexposurerisks.

什麼是特徵以及如何在PHP中使用它們 什麼是特徵以及如何在PHP中使用它們 Oct 02, 2025 am 04:17 AM

特質sinphpenablehorizo????ntalcodereusebyAllowingClassobalingMethodMethodSsobabableTraitContainers,旁路lephingsingleinheritancelimits.forexample,theloggabletraitprovidesalog(theloggabletraitprovidesalog)()methodyClassusisitit,suptoyclassusisitit,shisthencuser,shisthencuser,shisthencallencall $ the canthencall $ thiscrigthiscrea thiscreacreacrea

如何使用set_error_handler在PHP中創(chuàng)建自定義錯誤處理程序 如何使用set_error_handler在PHP中創(chuàng)建自定義錯誤處理程序 Oct 02, 2025 am 03:54 AM

set_error_handlerinPHPenablescustomerrorhandlingbydefiningafunctionthatinterceptsrecoverableerrors,allowingcontrolledlogginganduser-friendlyresponses;itacceptsparameterslike$errno,$errstr,$errfile,and$errlinetocaptureerrordetails,isregisteredviaset_e

如何在 PHP 中將字符串從一種字符編碼轉換為另一種字符編碼 如何在 PHP 中將字符串從一種字符編碼轉換為另一種字符編碼 Oct 09, 2025 am 03:45 AM

使用mb_convert_encoding()函數(shù)可將字符串在不同字符編碼間轉換,需確保PHP的MultibyteString擴展已啟用。 1.該函數(shù)格式為mb_convert_encoding(字符串,目標編碼,源編碼),如將ISO-8859-1轉為UTF-8;2.可結合mb_detect_encoding()檢測源編碼,但結果可能不準確;3.常用於將舊編碼數(shù)據(jù)轉為UTF-8以適配現(xiàn)代應用;4.替代方案iconv()支持//TRANSLIT和//IGNORE選項,但跨平臺一致性較差;5.推薦優(yōu)先

如何將INTL擴展用於PHP國際化 如何將INTL擴展用於PHP國際化 Oct 04, 2025 am 12:51 AM

答案:PHP的intl擴展基於ICU庫實現(xiàn)國際化,支持多語言格式化、翻譯和排序。首先安裝並啟用intl擴展,Linux系統(tǒng)使用apt-get或yum安裝,Windows在php.ini中開啟extension=intl。通過NumberFormatter按地區(qū)格式化數(shù)字,如de_DE輸出1.234.567,89;IntlDateFormatter處理日期顯示,如fr_FR顯示“l(fā)undi4septembre2023”;CurrencyFormatter格式化貨幣,en_US顯示$99.99。 Me

俄羅斯搜索引擎免費在線通道_俄羅斯搜索引擎直達官網(wǎng) 俄羅斯搜索引擎免費在線通道_俄羅斯搜索引擎直達官網(wǎng) Sep 30, 2025 am 09:56 AM

俄羅斯搜索引擎免費在線通道是yandex.com,該平臺提供網(wǎng)頁檢索、新聞聚合、地圖導航、多語言翻譯服務,並整合郵箱、雲(yún)存儲、本地生活及多媒體功能,具備俄語優(yōu)化和智能推薦等技術優(yōu)勢。

如何在PHP中驗證服務器端的用戶輸入? 如何在PHP中驗證服務器端的用戶輸入? Oct 03, 2025 am 03:23 AM

服務器式validationinphpiscrucialforsecurityAndDaintegrity.1.UseFilter_Input()andfilter_var()

See all articles