本文目錄一覽:
請問php中怎麼給照片加水印,時間和定位地址
水印要啟用gd庫,如果你用框架的話按照裡面調用方法就行,定位則需要第三方API服務,比如百度地圖等
GD庫是什麼,功能是什麼
GD庫,是php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片。
在網站上GD庫通常用來生成縮略圖,或者用來對圖片加水印,或者用來生成漢字驗證碼,或者對網站數據生成報表等。在PHP處理圖像,可使用GD庫,而GD庫開始時是支持GIF的,但由於GIF使用了有版權爭議的LZW演算法,會引起法律問題,於是從 GD 庫 1.6 版起所有的 GIF 支持都移除了,但是又在 GD 庫 2.0.28 版起又加了回來。如果使用二者之間版本的 GD 庫時 GIF 相關函數不可用。
PHP如何對上傳的圖片加水印?
這個要用到PHP的GD擴展,用這個擴展庫可以給圖片加水印。
參考一下這段代碼:
?php
/*
* 功能:PHP圖片水印 (水印支持圖片或文字)
* 參數:
* $groundImage 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式;
* $waterPos 水印位置,有10種狀態,0為隨機位置;
* 1為頂端居左,2為頂端居中,3為頂端居右;
* 4為中部居左,5為中部居中,6為中部居右;
* 7為底端居左,8為底端居中,9為底端居右;
* $waterImage 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式;
* $waterText 文字水印,即把文字作為為水印,支持ASCII碼,不支持中文;
* $textFont 文字大小,值為1、2、3、4或5,默認為5;
* $textColor 文字顏色,值為十六進位顏色值,默認為#FF0000(紅色);
*
* 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
* $waterImage 和 $waterText 最好不要同時使用,選其中之一即可,優先使用 $waterImage。
* 當$waterImage有效時,參數$waterString、$stringFont、$stringColor均不生效。
* 加水印後的圖片的文件名和 $groundImage 一樣。
* 作者:longware @ 2004-11-3 14:15:13
*/
function imageWaterMark($groundImage,$waterPos=0,$waterImage=」”,$waterText=」”,$textFont=5,$textColor=」#FF0000″)
{
$isWaterImage = FALSE;
$formatMsg = 「暫不支持該文件格式,請用圖片處理軟體將圖片轉換為GIF、JPG、PNG格式。」;
//讀取水印文件
if(!emptyempty($waterImage) file_exists($waterImage))
{
$isWaterImage = TRUE;
$water_info = getimagesize($waterImage);
$water_w = $water_info[0];//取得水印圖片的寬
$water_h = $water_info[1];//取得水印圖片的高
switch($water_info[2])//取得水印圖片的格式
{
case 1:$water_im = imagecreatefromgif($waterImage);break;
case 2:$water_im = imagecreatefromjpeg($waterImage);break;
case 3:$water_im = imagecreatefrompng($waterImage);break;
default:die($formatMsg);
}
}
//讀取背景圖片
if(!emptyempty($groundImage) file_exists($groundImage))
{
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0];//取得背景圖片的寬
$ground_h = $ground_info[1];//取得背景圖片的高
switch($ground_info[2])//取得背景圖片的格式
{
case 1:$ground_im = imagecreatefromgif($groundImage);break;
case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
case 3:$ground_im = imagecreatefrompng($groundImage);break;
default:die($formatMsg);
}
}
else
{
die(」需要加水印的圖片不存在!」);
}
//水印位置
if($isWaterImage)//圖片水印
{
$w = $water_w;
$h = $water_h;
$label = 「圖片的」;
}
else//文字水印
{
$temp = imagettfbbox(ceil($textFont*5),0,」./cour.ttf」,$waterText);//取得使用 TrueType 字體的文本的範圍
$w = $temp[2] – $temp[6];
$h = $temp[3] – $temp[7];
unset($temp);
$label = 「文字區域」;
}
if( ($ground_w$w) || ($ground_h$h) )
{
echo 「需要加水印的圖片的長度或寬度比水印」.$label.」還小,無法生成水印!」;
return;
}
switch($waterPos)
{
case 0://隨機
$posX = rand(0,($ground_w – $w));
$posY = rand(0,($ground_h – $h));
break;
case 1://1為頂端居左
$posX = 0;
$posY = 0;
break;
case 2://2為頂端居中
$posX = ($ground_w – $w) / 2;
$posY = 0;
break;
case 3://3為頂端居右
$posX = $ground_w – $w;
$posY = 0;
break;
case 4://4為中部居左
$posX = 0;
$posY = ($ground_h – $h) / 2;
break;
case 5://5為中部居中
$posX = ($ground_w – $w) / 2;
$posY = ($ground_h – $h) / 2;
break;
case 6://6為中部居右
$posX = $ground_w – $w;
$posY = ($ground_h – $h) / 2;
break;
case 7://7為底端居左
$posX = 0;
$posY = $ground_h – $h;
break;
case 8://8為底端居中
$posX = ($ground_w – $w) / 2;
$posY = $ground_h – $h;
break;
case 9://9為底端居右
$posX = $ground_w – $w;
$posY = $ground_h – $h;
break;
default://隨機
$posX = rand(0,($ground_w – $w));
$posY = rand(0,($ground_h – $h));
break;
}
//設定圖像的混色模式
imagealphablending($ground_im, true);
if($isWaterImage)//圖片水印
{
imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷貝水印到目標文件
}
else//文字水印
{
if( !emptyempty($textColor) (strlen($textColor)==7) )
{
$R = hexdec(substr($textColor,1,2));
$G = hexdec(substr($textColor,3,2));
$B = hexdec(substr($textColor,5));
}
else
{
die(」水印文字顏色格式不正確!」);
}
imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
}
//生成水印後的圖片
@unlink($groundImage);
switch($ground_info[2])//取得背景圖片的格式
{
case 1:imagegif($ground_im,$groundImage);break;
case 2:imagejpeg($ground_im,$groundImage);break;
case 3:imagepng($ground_im,$groundImage);break;
default:die($errorMsg);
}
//釋放內存
if(isset($water_info)) unset($water_info);
if(isset($water_im)) imagedestroy($water_im);
unset($ground_info);
imagedestroy($ground_im);
}
//—————————————————————————————
$id=$_REQUEST[‘id’];
$num = count($_FILES[‘userfile’][‘name’]);
print_r($_FILES[‘userfile’]);
print_r($_FILES[‘userfile’][‘name’]);
echo $num;
echo 「bR」;
if(isset($id)){
for($i=0;$i$id;$i++){
if(isset($_FILES) !emptyempty($_FILES[‘userfile’]) $_FILES[‘userfile’][‘size’]0)
{
$uploadfile = 「./」.time().」_」.$_FILES[‘userfile’][name][$i];
echo 「br」;
echo $uploadfile;
if (copy($_FILES[‘userfile’][‘tmp_name’][$i], $uploadfile))
{
echo 「OKbr」;
//文字水印
//imageWaterMark($uploadfile,5,」”,」” target=”_blank”」,5,」#cccccc「);
//圖片水印
$waterImage=」logo_ok1.gif」;//水印圖片路徑
imageWaterMark($uploadfile,9,$waterImage);
echo 「img src=\」”.$uploadfile.」\」 border=\」0\」」;
}
else
{
echo 「Failbr」;
}
}
}
}
?
form enctype=」multipart/form-data」 method=」POST」
?php
for($a=0;$a$id;$a++){
echo 「文件: input name=\」userfile[]\」 type=\」file\」br」;
}
?
input type=」submit」 value=」上傳」
/form
php如何給圖片加文字水印
我知道的有三種,都是使用GD庫的image函數
一種是直接在圖片上寫文字
imagefttext();
一種是帶透明度的水印圖片
imagecopy();
還有一種是可以自定義水印圖片透明度的
imagecopymerge();
你想要什麼效果,可以接著細說
php如何實現自動加水印
加水印邏輯有兩種
一種是上傳直接加水印
另一種是利用偽靜態將圖片訪問重定向到處理程序,臨時加水印緩存並輸出
php處理圖片加水印可以使用gd庫中的相關函數
以下為臨時手打代碼,可以按此思路優化,有問題可以聯繫本人
//此處需根據上傳的圖片格式使用對應函數實例化圖片
$img=imagecreatefromjpg($imgurl);
//根據水印圖片路徑實例化水印
$waterImg=imagecreatefrompng($waterpath);
//獲取原圖及水印圖片尺寸,用以計算是否需要縮放及放置位置
list($width, $height, $type, $attr) = getimagesize($imgurl);
list($waterw, $waterh, $type, $attr) = getimagesize($waterpath);
$scale=1;
$waterReleaseW=$waterw;
$waterReleaseH=$waterh;
if($waterReleaseW$width*.5){
$scale=$width*.5/$waterw;
$waterReleaseW = $width*.5;
$waterReleaseH = $waterh*$scale;
}
if($waterReleaseH$height*.5){
$scale *= $height*.5/$waterh;
$waterReleaseH = $height*.5;
$waterReleaseW = $waterw*$scale;
}
//將水印圖片拷貝到原圖指定位置(此示例為右下角)
imagecopyresized($img,$waterImg,
$width-$waterReleaseW-10,$height-$waterReleaseH-10,
0,0,
$width-10,$height-10,
$waterw,$waterh);
//銷毀水印圖片實例
imagedestroy($waterImg);
//水印後圖片保存
imagejpeg($img,$newpath);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309619.html