① 誰有能用的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給圖片添加水印的問題,求大神解答
加兩文字水印,弄成加兩次水印就行,不過效率就差點。可以考慮加圖片的水印
你用什麼編輯器?如果是記事本的話很容易出現亂碼問題,網頁是UTF-8了,但是你記事本存儲的卻是GBK。推薦用Editlus 3,網上有注冊版的。
③ php如何給圖片加文字水印
我知道的有三種,都是使用GD庫的image函數
一種是直接在圖片上寫文字
imagefttext();
一種是帶透明度的水印圖片
image();
還有一種是可以自定義水印圖片透明度的
imagemerge();
你想要什麼效果,可以接著細說
④ php圖片水印代碼問題拜託了各位 謝謝
不顯示的話就是你沒輸出來,請參考以下代碼重新檢查一遍: ------------------------------華麗分割線------------------------------------- <? /* * 功能:PHP圖片水印 (水印支持圖片或文字) * 參數: * $groundImage 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式; * $waterPos 水印位置,有10種狀態,0為隨機位置; * 1為頂端居左,2為頂端居中,3為頂端居右; * 4為中部居左,5為中部居中,6為中部居右; * 7為底端居左,8為底端居中,9為底端居右; * $waterImage 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式; * $waterText 文字水印,即把文字作為為水印,支持ASCII碼,不支持中文; * $fontSize 文字大小,值為1、2、3、4或5,默認為5; * $textColor 文字顏色,值為十六進制顏色值,默認為#CCCCCC(白灰色); * $fontfile ttf字體文件,即用來設置文字水印的字體。使用windows的用戶在系統盤的目錄中 * 搜索*.ttf可以得到系統中安裝的字體文件,將所要的文件拷到網站合適的目錄中, * 默認是當前目錄下arial.ttf。 * $xOffset 水平偏移量,即在默認水印坐標值基礎上加上這個值,默認為0,如果你想留給水印留 * 出水平方向上的邊距,可以設置這個值,如:2 則表示在默認的基礎上向右移2個單位,-2 表示向左移兩單位 * $yOffset 垂直偏移量,即在默認水印坐標值基礎上加上這個值,默認為0,如果你想留給水印留 * 出垂直方向上的邊距,可以設置這個值,如:2 則表示在默認的基礎上向下移2個單位,-2 表示向上移兩單位 * 返回值: * 0 水印成功 * 1 水印圖片格式目前不支持 * 2 要水印的背景圖片不存在 * 3 需要加水印的圖片的長度或寬度比水印圖片或文字區域還小,無法生成水印 * 4 字體文件不存在 * 5 水印文字顏色格式不正確 * 6 水印背景圖片格式目前不支持 * 修改記錄: * * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG * $waterImage 和 $waterText 最好不要同時使用,選其中之一即可,優先使用 $waterImage。 * 當$waterImage有效時,參數$waterString、$stringFont、$stringColor均不生效。 * 加水印後的圖片的文件名和 $groundImage 一樣。 * 作者:高西林 * 日期:2007-4-28 * 說明:本程序根據longware的程序改寫而成。 */ function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$fontSize=12,$textColor="#CCCCCC", $fontfile='./arial.ttf',$xOffset=0,$yOffset=0) { $isWaterImage = FALSE; //讀取水印文件 if(!empty($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:return 1; } } //讀取背景圖片 if(!empty($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:return 1; } } else { return 2; } //水印位置 if($isWaterImage) { //圖片水印 $w = $water_w; $h = $water_h; $label = "圖片的"; } else { //文字水印 if(!file_exists($fontfile))return 4; $temp = imagettfbbox($fontSize,0,$fontfile,$waterText);//取得使用 TrueType 字體的文本的范圍 $w = $temp[2] - $temp[6]; $h = $temp[3] - $temp[7]; unset($temp); } if( ($ground_w < $w) || ($ground_h < $h) ) { return 3; } 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 + $xOffset, $posY + $yOffset, 0, 0, $water_w,$water_h);//拷貝水印到目標文件 } else {//文字水印 if( !empty($textColor) && (strlen($textColor)==7) ) { $R = hexdec(substr($textColor,1,2)); $G = hexdec(substr($textColor,3,2)); $B = hexdec(substr($textColor,5)); } else { return 5; } imagettftext ( $ground_im, $fontSize, 0, $posX + $xOffset, $posY + $h + $yOffset, imagecolorallocate($ground_im, $R, $G, $B), $fontfile, $waterText); } //生成水印後的圖片 @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: return 6; } //釋放內存 if(isset($water_info)) unset($water_info); if(isset($water_im)) imagedestroy($water_im); unset($ground_info); imagedestroy($ground_im); // return 0; } ?> <?php ////////////////////// if(isset($_POST['submit'])) { if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0) { $uploadfile = "./".time()."_".$_FILES['userfile']['name']; if (($_FILES['userfile']['tmp_name'], $uploadfile)) { if($_POST['watertype'] == 0) { $msg = "returnvalue=".imageWaterMark($uploadfile,$_POST['waterpos'],"",$_POST['watercontent'],$_POST['fontsize'],$_POST['fontcolor'],$_POST['fontfile'],$_POST['xoffset'],$_POST['yoffset']); } else { $msg = "returnvalue=".imageWaterMark($uploadfile,$_POST['waterpos'],$_POST['watercontent']); } echo "<img src=\"".$uploadfile."\" border=\"0\">"; } else { $msg = "Fail!"; } } } ?> <html> <head> <meta http-equiv=content-type content="text/html; charset=utf-8"> <title>水印函數測試</title> </head> <body> <form enctype="multipart/form-data" method="POST"> <table> <tr> <td><input name="watertype" type="radio" value=0 checked>文字水印<input type="radio" name="watertype" value=1>水印圖片</td> </tr> <tr> <td><input name="watercontent" value="blog.csdn.net/alin0725">水印文字內容或水印圖片文件名</td> </tr> <tr> <td><input name="fontcolor" value="#CCCCCC">文字水印顏色</td> </tr> <tr> <td><input name="fontsize" value="10">文字字體大小</td> </tr> <tr> <td><input name="fontfile" value="./arial.ttf">文字字體文件ttf格式</td> </tr> <tr> <td>水印位置<input name="waterpos" value=0> 0為隨機,其他位置值如下: <table> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </td> </tr> <tr> <td>x方向上的偏移量<input name="xoffset" value=0>y方向上的偏移量<input name="yoffset" value=0> </td> <tr> <tr> <td>背景圖片: <input name="userfile" type="file"> </td> </tr> <tr> <td><input type="submit" name="submit" value="提交"></td> </tr> <tr> <td>消息:<?php echo $msg; ?></td> </tr> </table> </form> </body> </html>
⑤ php通過表單上傳兩張圖片並合成(類似圖片加水印效果)
你好只能幫到你這里的
支持水印,日期,縮略圖的php多文件上傳類
<?php
//視圖表單
//支持多張圖片上傳
classupload{
var$dir;//附件存放物理目錄
var$time;//自定義文件上傳時間
var$allow_types;//允許上傳附件類型
var$field;//上傳控制項名稱
var$maxsize;//最大允許文件大小,單位為KB
var$thumb_width;//縮略圖寬度
var$thumb_height;//縮略圖高度
var$watermark_file;//水印圖片地址
var$watermark_pos;//水印位置
var$watermark_trans;//水印透明度
//構造函數
//$types:允許上傳的文件類型,$maxsize:允許大小,$field:上傳控制項名稱,$time:自定義上傳時間
functionupload($types='jpg|png',$maxsize=1024,$field='attach',$time=''){
$this->allow_types=explode('|',$types);
$this->maxsize=$maxsize*1024;
$this->field=$field;
$this->time=$time?$time:time();
}
//設置並創建文件具體存放的目錄
//$basedir:基目錄,必須為物理路徑
//$filedir:自定義子目錄,可用參數{y}、{m}、{d}
functionset_dir($basedir,$filedir=''){
$dir=$basedir;
!is_dir($dir)&&@mkdir($dir,0777);
if(!empty($filedir)){
$filedir=str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));//用string_replace把{y}{m}{d}幾個標簽進行替換
$dirs=explode('/',$filedir);
foreach($dirsas$d){
!empty($d)&&$dir.=$d.'/';
!is_dir($dir)&&@mkdir($dir,0777);
}
}
$this->dir=$dir;
}
//圖片縮略圖設置,如果不生成縮略圖則不用設置
//$width:縮略圖寬度,$height:縮略圖高度
functionset_thumb($width=0,$height=0){
$this->thumb_width=$width;
$this->thumb_height=$height;
}
//圖片水印設置,如果不生成添加水印則不用設置
//$file:水印圖片,$pos:水印位置,$trans:水印透明度
functionset_watermark($file,$pos=6,$trans=80){
$this->watermark_file=$file;
$this->watermark_pos=$pos;
$this->watermark_trans=$trans;
}
/*—————————————————————-
執行文件上傳,處理完返回一個包含上傳成功或失敗的文件信息數組,
其中:name為文件名,上傳成功時是上傳到伺服器上的文件名,上傳失敗則是本地的文件名
dir為伺服器上存放該附件的物理路徑,上傳失敗不存在該值
size為附件大小,上傳失敗不存在該值
flag為狀態標識,1表示成功,-1表示文件類型不允許,-2表示文件大小超出
—————————————————————–*/
functionexecute(){
$files=array();//成功上傳的文件信息
$field=$this->field;
$keys=array_keys($_FILES[$field]['name']);
foreach($keysas$key){
if(!$_FILES[$field]['name'][$key])continue;
$fileext=$this->fileext($_FILES[$field]['name'][$key]);//獲取文件擴展名
$filename=date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext;//生成文件名
$filedir=$this->dir;//附件實際存放目錄
$filesize=$_FILES[$field]['size'][$key];//文件大小
//文件類型不允許
if(!in_array($fileext,$this->allow_types)){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['flag']=-1;
continue;
}
//文件大小超出
if($filesize>$this->maxsize){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['name']=$filesize;
$files[$key]['flag']=-2;
continue;
}
$files[$key]['name']=$filename;
$files[$key]['dir']=$filedir;
$files[$key]['size']=$filesize;
//保存上傳文件並刪除臨時文件
if(is_uploaded_file($_FILES[$field]['tmp_name'][$key])){
move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
@unlink($_FILES[$field]['tmp_name'][$key]);
$files[$key]['flag']=1;
//對圖片進行加水印和生成縮略圖,這里演示只支持jpg和png(gif生成的話會沒了幀的)
if(in_array($fileext,array('jpg','png'))){
if($this->thumb_width){
if($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)){
$files[$key]['thumb']='thumb_'.$filename;//縮略圖文件名
}
}
$this->create_watermark($filedir.$filename);
}
}
}
return$files;
}
//創建縮略圖,以相同的擴展名生成縮略圖
//$src_file:來源圖像路徑,$thumb_file:縮略圖路徑
functioncreate_thumb($src_file,$thumb_file){
$t_width=$this->thumb_width;
$t_height=$this->thumb_height;
if(!file_exists($src_file))returnfalse;
$src_info=getImageSize($src_file);
//如果來源圖像小於或等於縮略圖則拷貝源圖像作為縮略圖,免去操作
if($src_info[0]<=$t_width&&$src_info[1]<=$t_height){
if(!($src_file,$thumb_file)){
returnfalse;
}
returntrue;
}
//按比例計算縮略圖大小
if(($src_info[0]-$t_width)>($src_info[1]-$t_height)){
$t_height=($t_width/$src_info[0])*$src_info[1];
}else{
$t_width=($t_height/$src_info[1])*$src_info[0];
}
//取得文件擴展名
$fileext=$this->fileext($src_file);
switch($fileext){
case'jpg':
$src_img=ImageCreateFromJPEG($src_file);break;
case'png':
$src_img=ImageCreateFromPNG($src_file);break;
case'gif':
$src_img=ImageCreateFromGIF($src_file);break;
}
//創建一個真彩色的縮略圖像
$thumb_img=@ImageCreateTrueColor($t_width,$t_height);
//ImageCopyResampled函數拷貝的圖像平滑度較好,優先考慮
if(function_exists('imageresampled')){
@ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}else{
@ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}
//生成縮略圖
switch($fileext){
case'jpg':
ImageJPEG($thumb_img,$thumb_file);break;
case'gif':
ImageGIF($thumb_img,$thumb_file);break;
case'png':
ImagePNG($thumb_img,$thumb_file);break;
}
//銷毀臨時圖像
@ImageDestroy($src_img);
@ImageDestroy($thumb_img);
returntrue;
}
//為圖片添加水印
//$file:要添加水印的文件
functioncreate_watermark($file){
//文件不存在則返回
if(!file_exists($this->watermark_file)||!file_exists($file))return;
if(!function_exists('getImageSize'))return;
//檢查GD支持的文件類型
$gd_allow_types=array();
if(function_exists('ImageCreateFromGIF'))$gd_allow_types['image/gif']='ImageCreateFromGIF';
if(function_exists('ImageCreateFromPNG'))$gd_allow_types['image/png']='ImageCreateFromPNG';
if(function_exists('ImageCreateFromJPEG'))$gd_allow_types['image/jpeg']='ImageCreateFromJPEG';
//獲取文件信息
$fileinfo=getImageSize($file);
$wminfo=getImageSize($this->watermark_file);
if($fileinfo[0]<$wminfo[0]||$fileinfo[1]<$wminfo[1])return;
if(array_key_exists($fileinfo['mime'],$gd_allow_types)){
if(array_key_exists($wminfo['mime'],$gd_allow_types)){
//從文件創建圖像
$temp=$gd_allow_types[$fileinfo['mime']]($file);
$temp_wm=$gd_allow_types[$wminfo['mime']]($this->watermark_file);
//水印位置
switch($this->watermark_pos){
case1://頂部居左
$dst_x=0;$dst_y=0;break;
case2://頂部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=0;break;
case3://頂部居右
$dst_x=$fileinfo[0];$dst_y=0;break;
case4://底部居左
$dst_x=0;$dst_y=$fileinfo[1];break;
case5://底部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=$fileinfo[1];break;
case6://底部居右
$dst_x=$fileinfo[0]-$wminfo[0];$dst_y=$fileinfo[1]-$wminfo[1];break;
default://隨機
$dst_x=mt_rand(0,$fileinfo[0]-$wminfo[0]);$dst_y=mt_rand(0,$fileinfo[1]-$wminfo[1]);
}
if(function_exists('ImageAlphaBlending'))ImageAlphaBlending($temp_wm,True);//設定圖像的混色模式
if(function_exists('ImageSaveAlpha'))ImageSaveAlpha($temp_wm,True);//保存完整的alpha通道信息
//為圖像添加水印
if(function_exists('imageCopyMerge')){
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
}else{
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
}
//保存圖片
switch($fileinfo['mime']){
case'image/jpeg':
@imageJPEG($temp,$file);
break;
case'image/png':
@imagePNG($temp,$file);
break;
case'image/gif':
@imageGIF($temp,$file);
break;
}
//銷毀零時圖像
@imageDestroy($temp);
@imageDestroy($temp_wm);
}
}
}
//獲取文件擴展名
functionfileext($filename){
returnstrtolower(substr(strrchr($filename,'.'),1,10));
}
}
?>
⑥ php 給圖片添加文字或圖片 並實現保存,,急救!!
簡單說,這就是PHP的一個生成水印的功能了。
直接帖代碼給你。並附上注釋吧,應該能看懂。
<?php
header("Content-type: image/jpeg"); //瀏覽器輸出,如不需要可去掉此行
$im = @imagecreatefromjpeg('test.jpg'); //從圖片建立文件,此處以jpg文件格式為例
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$text = 'Testing...'; //要寫到圖上的文字
$font = 'arial.ttf'; //寫的文字用到的字體。
$srcw=imagesx($im);
imagettftext($im, 20, 0, $srcw-210, 21, $grey, $font, $text);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagettftext($im, 20, 0, 9, 19, $white, $font, $text);
imagepng($im);
imagedestroy($im);
?>
⑦ 誰有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給圖片添加文字水印
請確認C:\WINDOWS\Fonts\simkai.ttf';是否支持中文
或不要轉換
$str = iconv('GB2312','UTF-8',$str);
直接
$str=$str;
⑨ PHP給圖片加水印的思想是什麼
貼個php的圖片處理類 你可以自己研究下 共同進步 呵呵
/*
此類包含以下功能
A.生成縮略圖
B.給圖片添加文字水印(包括中文)
C.將指定的圖片旋轉90度/180度/270度,並保存至文件
D.將圖片水平/垂直翻轉,並保存至文件
E.在線裁剪圖片任意部分矩形(正在編寫之中)
具體目標效果暫時可以參考 http://yananzb.com/cut/cut.htm
期待您的響應
成員函數說明
---------------------------------------------------------------------------------------------
public void CImage::__construct(string $src_image_file)
功能:類CImage的構造函數
參數
$src_image_file 字元串,源圖片文件名 注意,目前只支持gif,png,jpeg,jpg格式,這是GD庫的限制,並非本程序的局限
---------------------------------------------------------------------------------------------
public bool CImage: thumb($image_dist,$x)
功能:根據源圖片生成縮略圖,並保存至文件
$image_dist 字元串 目標縮略圖片路徑及文件名 如 /File/th.jpg
$x 整型 目標縮略圖片的尺寸限制即當原始圖片的寬大於高時,那麼新的縮略圖的寬為$x,反之高為$x
---------------------------------------------------------------------------------------------
public bool image_press($image_dist,$str,$font="simkai.ttf")
函數功能:
圖片生成水印並保存新圖片至目標文件
參數說明:
$image_dist 字元串 目標圖片名
$str 字元串 要寫入到圖片水印的字元串
$font 字元串 合法的系統字體名或WEB目錄中正確的字體文件名
---------------------------------------------------------------------------------------------
public bool rotate($image_dist,$angle)
函數功能:
將源圖片旋轉一定角度並將新圖片保存至文件
參數說明:
$image_dist 字元串 目標圖片文件名
$angle 整數 要旋轉的角度 只能是90或180或270度
---------------------------------------------------------------------------------------------
public bool rotate_h($image_dist)
函數功能:
將源圖片水平翻轉,並將新圖片保存至文件
參數說明:
$image_dist 字元串 目標圖片文件名
---------------------------------------------------------------------------------------------
public bool rotate_v($image_dist)
函數功能:
將源圖片垂直翻轉,並將新圖片保存至文件
參數說明:
$image_dist 字元串 目標圖片文件名
---------------------------------------------------------------------------------------------
使用範例:
$p=new CImage("s.jpg"); //創建一個圖片處理對象
$p->thumb("thumb.jpg",300); //生成縮略圖 限制尺寸為300,保存為thumb.jpg
$p->rotate("rt.jpg",90); //旋轉90度,並保存為rt.jpg,類似地,你可以將90換成180,270進行旋轉
$p->rotate_h("h.jpg"); //水平翻轉
$p->rotate_v("v.jpg"); //垂直翻轉
*/
class CImage{
var $src_image;
var $width;
var $height;
var $image_type;
var $img;
var $src_x;
var $src_y;
function __construct($image_file)
{
$info=GetImageSize($image_file);
$this->src_image=$image_file;
$this->width=$info[0];
$this->height=$info[1];
switch($info[2])
{
case 1:
$this->image_type="gif";
break;
case 2:
$this->image_type="jpeg";
break;
case 3:
$this->image_type="png";
break;
default:
return false;
//echo("Unsurport Image type.");
break;
} //swith end
//echo "ok";
$new_function='ImageCreateFrom'.ucfirst($this->image_type);
$this->img=$new_function($this->src_image);
$this->src_x=ImageSX($this->img);
$this->src_y=ImageSY($this->img);
}
function thumb($image_dist,$x) //$x為新圖的限制邊的尺寸
{
$src_x=ImageSX($this->img);
$src_y=ImageSY($this->img);
$scale=min($x/$src_x,$x/$src_y);
if($scale<1)
{
$new_x=floor($scale*$src_x);
$new_y=floor($scale*$src_y);
$img_tmp=ImageCreateTrueColor($new_x,$new_y); //set the size of Canvas for the new Image
ImageCopyResampled($img_tmp,$this->img,0,0,0,0,$new_x,$new_y,$src_x,$src_y); //Resampled
ImageDestroy($this->img);
$new_function="Image".ucfirst($this->image_type);
return $new_function($img_tmp,$image_dist);
}
} // thumb end
//給圖片生成文字水印
function image_press($image_dist,$str,$font="simkai.ttf") {
$str=iconv("GB2312","utf-8",$str);
$blue=ImageColorAllocate($this->img,90,255,255);
$white=ImageColorAllocate($this->img,255,0,0);
ImageTTFText($this->img,20,0,$this->src_x/2/2,$this->src_y-80,$white,$font,$str);
$new_function="Image".ucfirst($this->image_type);
return $new_function($this->img,$image_dist);
}
function rotate($image_dist,$angle)
{
$img_tmp=null;
$new_function="Image".ucfirst($this->image_type);
if(($angle!=90)&&($angle!=180)&&($angle!=270))
{
echo("Un-valid angle on calling CImage::rotate(\$image_dist,\$angle) .<p>The valid angle must be 90 or 180 or 270.");
return false;
}
if(($angle==90)||($angle==270))
{
$img_tmp=ImageCreateTrueColor($this->src_y,$this->src_x);
}
else
{
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
}
switch($angle)
{
case 90:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_y-$j-1,$i,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;
case 180:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_x-$i-1,$this->src_y-$j-1,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;
case 270:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$j,$this->src_x-$i-1,ImageColorAt($this->img,$i,$j));
}
}
return $new_function($img_tmp,$image_dist);
break;
} //end switch
} //end rotate
function rotate_h($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,$this->src_x-1,0,$this->src_x,$this->src_y,-$this->src_x,$this->src_y); //水平翻轉
return $new_function($img_tmp,$image_dist);
}
function rotate_v($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,0,$this->src_y-1,$this->src_x,$this->src_y,$this->src_x,-$this->src_y);
return $new_function($img_tmp,$image_dist);
}
} //end CImage
⑩ 用php代碼怎麼以背景圖片加上文字生成新的圖片,然後在標題處絕對調用該圖片
<?php
ob_clean(); //清除輸出緩存
header("Content-type:image/jpeg"); //設置輸出類型
$img="images/test.jpg"; //背景圖片名
if(isset($_GET["img"]))$img=$_GET["img"]; //也可以通過img參數傳入
$im=imagecreatefromjpeg($img); //讀入背景圖片
$text="文字內容"; //要加上的文字內容
if(isset($_GET["text"]))$text=$_GET["text"]; //也可以通過text參數傳入
$fontFile="xxx.ttf"; //字體文件名,必須要
$fontSize=36; //字體尺寸
$fontColor=ImageColorAllocate($im,0,0,0); //字體顏色,這里是黑色
$textAngle=0; //文字顯示的角度,0表示水平顯示
$textLeft=20; //文字顯示的x坐標
$textTop=60; //文字顯示的y坐標
imagefttext($im,$fontSize,$textAngle,$textLeft,$textTop,$fontColor,$fontFile,$text); //把文字覆蓋到圖片上
Imagejpeg($im); //輸出圖片
ImageDestroy($im); //銷毀圖片
?>
把以上文字保存為php文件,比如 img.php
然後在需要調用圖片的地方用 <img src="img.php?img=背景圖片文件路徑&text=要加上的文字"/> 來調用
比如 <img src="img.php?img=images/back.jpg&text=你好"/>