① 用PHP代碼給圖片加水印
這篇文章主要介紹了用PHP代碼給圖片加水印的相關資料,需要的朋友可以參考下
先找好一張圖片,更名為face.jpeg,創建watermark.php:
?
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
*
Created
by
PhpStorm.
*
User:
Administrator
*
Date:
2015/6/29
*
Time:
22:27
*/
$img
=
imagecreatefromjpeg('face.jpeg');//根據已有的JPG創建image
header('Content-type:image/jpeg');//設置mime
type
imagestring($img,5,5,5,'Vito-L',imagecolorallocate($img,255,0,0));//生成水印,imagestring(圖片,字體,位置x,位置y,字元串,顏色)
imagejpeg($img);//輸出圖片
//整幅圖像的左上角為
0,0
效果如下:
以上所述就是本文的全部內容了,希望大家能夠喜歡。
② 誰有php批量處理圖片、圖片生成縮略圖、圖片添加水印的函數
//批量處理圖片、圖片生成縮略圖、圖片添加水印
$dir=opendir(dirname(__FILE__));
while(!!$_file=readdir($dir)){
list($filesname,$kzm)=explode(".",$_file);//獲取擴展名
if($kzm=="gif"or$kzm=="jpg"or$kzm=="JPG"or$kzm=="png"){
if(!makethumb("$_file","120","120","100")){
echo'執行成功!';
}else{
echo'執行失敗!';
}
}
}
closedir($dir);
/**
*處理縮略圖並添加水印函數
*@accesspubliuc
*@param$srcFile-----------圖片文件名
*@param$dstFile-----------另存的文件名
*@param$dstW-------------圖片保存的寬度
*@param$dstH--------------圖片保存的高度
*@param$rate---------------圖片保存的品質
*@param$markwords-----水印文字
*@param$markimage-----水印圖片
*@param使用方法makethumb("a.jpg","b.jpg","120","120","100");
*/
functionmakethumb($srcFile/*,$dstFile*/,$dstW,$dstH,$rate=100/*,$markwords=null,$markimage=null*/){
$data=GetImageSize($srcFile);
switch($data[2]){
case1:
$im=@ImageCreateFromGIF($srcFile);
break;
case2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im)returnFalse;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if($srcW*$dstH>$srcH*$dstW){
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2);
$fdstW=$dstW;
}
else
{
$fdstW=round($srcW*$dstH/$srcH);
$dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$white=ImageColorAllocate($ni,255,255,255);
$black=ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);//填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
//if($markwords!=null){
//$markwords=iconv("gb2312","UTF-8",$markwords);
////轉換文字編碼
//ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords);//寫入文字水印,參數依次為,文字大小|偏轉度|橫坐標|縱坐標|文字顏色|文字類型|文字內容
//}elseif($markimage!=null){
//$wimage_data=GetImageSize($markimage);
//switch($wimage_data[2]){
//case1:
//$wimage=@ImageCreateFromGIF($markimage);
//break;
//case2:
//$wimage=@ImageCreateFromJPEG($markimage);
//break;
//case3:
//$wimage=@ImageCreateFromPNG($markimage);
//break;
//}
//image($ni,$wimage,500,560,0,0,88,31);//寫入圖片水印,水印圖片大小默認為88*31
//imagedestroy($wimage);
//}
$dstFile=$srcFile.'.gif';
ImageJpeg($ni,$dstFile,$rate);
//ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
③ 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)//圖片水印
{
image($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 (($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
echo 「OK<br>」;
//文字水印
//imageWaterMark($uploadfile,5,」",」www.lvye.info" target="_blank">http://www.lvye.info」,5,」#cccccc「);
//圖片水印
$waterImage=」logo_ok1.gif」;//水印圖片路徑
imageWaterMark($uploadfile,9,$waterImage);
echo 「<img src=\」".$uploadfile.」\」 border=\」0\」>」;
}
else
{
echo 「Fail<br>」;
}
}
}
}
?>
<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給圖片添加文字水印
請確認C:\WINDOWS\Fonts\simkai.ttf';是否支持中文
或不要轉換
$str = iconv('GB2312','UTF-8',$str);
直接
$str=$str;
⑤ php給圖片加文字水印
arrayimagettftext(resource$image,float$size,float$angle,int$x,int$y,int$color,string$fontfile,string$text)
⑥ 誰有能用的php給圖片加文字水印,最好有使用方法,注意:一定是能用的,功能強不強大另說
<?php
/*PHP圖片加文字水印類庫
QQ:3697578482 傷心的歌
該類庫暫時只支持文字水印,位置為右下角,顏色隨機
調用方法:
1、在需要加水印的文件頂部引入類庫:
include_once 'imageClass.php';
2、聲明新類:
$tpl=new image_fu;
3、給圖片水印提供參數:
$tpl->img(圖片路徑,水印文字,字體路徑,字體大小,字體角度);
比如:$tpl->img('abc.jpg','這是水印文字','ziti.ttf',30,0)
*/
class image_fu{
private $image;
private $img_info;
private $img_width;
private $img_height;
private $img_im;
private $img_text;
private $img_ttf='';
private $img_new;
private $img_text_size;
private $img_jd;
function img($img='',$txt='',$ttf='',$size=12,$jiao=0){
if(isset($img)&&file_exists($img)){//檢測圖片是否存在
$this->image =$img;
$this->img_text=$txt;
$this->img_text_size=$size;
$this->img_jd=$jiao;
if(file_exists($ttf)){
$this->img_ttf=$ttf;
}else{
exit('字體文件:'.$ttf.'不存在!');
}
$this->imgyesno();
}else{
exit('圖片文件:'.$img.'不存在');
}
}
private function imgyesno(){
$this->img_info =getimagesize($this->image);
$this->img_width =$this->img_info[0];//圖片寬
$this->img_height=$this->img_info[1];//圖片高
//檢測圖片類型
switch($this->img_info[2]){
case 1:$this->img_im = imagecreatefromgif($this->image);break;
case 2:$this->img_im = imagecreatefromjpeg($this->image);break;
case 3:$this->img_im = imagecreatefrompng($this->image);break;
default:exit('圖片格式不支持水印');
}
$this->img_text();
}
private function img_text(){
imagealphablending($this->img_im,true);
//設定顏色
$color=imagecolorallocate($this->img_im,rand(0,255),rand(0,255),rand(0,255));
$txt_height=$this->img_text_size;
$txt_jiao=$this->img_jd;
$ttf_im=imagettfbbox($txt_height,$txt_jiao,$this->img_ttf,$this->img_text);
$w = $ttf_im[2] - $ttf_im[6];
$h = $ttf_im[3] - $ttf_im[7];
//$w = $ttf_im[7];
//$h = $ttf_im[8];
unset($ttf_im);
$txt_y =$this->img_height-$h;
$txt_x =$this->img_width-$w;
//$txt_y =0;
//$txt_x =0;
$this->img_new=@imagettftext($this->img_im,$txt_height,$txt_jiao,$txt_x,$txt_y,$color,$this->img_ttf,$this->img_text);
@unlink($this->image);//刪除圖片
switch($this->img_info[2]) {//取得背景圖片的格式
case 1:imagegif($this->img_im,$this->image);break;
case 2:imagejpeg($this->img_im,$this->image);break;
case 3:imagepng($this->img_im,$this->image);break;
default: exit('水印圖片失敗');
}
}
//顯示圖片
function img_show(){echo '<img src="'.$this->image.'" border="0" alt="'.$this->img_text.'" />';}
//釋放內存
private function img_nothing(){
unset($this->img_info);
imagedestroy($this->img_im);
}
}
?>
⑦ php如何給圖片加文字水印
我知道的有三種,都是使用GD庫的image函數
一種是直接在圖片上寫文字
imagefttext();
一種是帶透明度的水印圖片
image();
還有一種是可以自定義水印圖片透明度的
imagemerge();
你想要什麼效果,可以接著細說
⑧ 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;
}
//將水印圖片拷貝到原圖指定位置(此示例為右下角)
imageresized($img,$waterImg,
$width-$waterReleaseW-10,$height-$waterReleaseH-10,
0,0,
$width-10,$height-10,
$waterw,$waterh);
//銷毀水印圖片實例
imagedestroy($waterImg);
//水印後圖片保存
imagejpeg($img,$newpath);
⑨ php添加文字和圖片水印問題。
//可能是你的編碼設置有問題,在配置文件中設置編碼為UTF-8或者別的,反正和你其它地方編碼統一
header('Content-Type:text/html;charset=utf-8');
⑩ 關於PHP給圖片添加水印的問題,求大神解答
加兩文字水印,弄成加兩次水印就行,不過效率就差點。可以考慮加圖片的水印
你用什麼編輯器?如果是記事本的話很容易出現亂碼問題,網頁是UTF-8了,但是你記事本存儲的卻是GBK。推薦用Editlus 3,網上有注冊版的。