本文目錄一覽:
- 1、我這有個已經實現了php上傳圖片的功能的類,但是現在還要給上傳的每張圖片加水印,請高手幫忙……
- 2、誰有能用的php給圖片加文字水印,最好有使用方法,注意:一定是能用的,功能強不強大另說
- 3、PHP執行FTP 上傳 加水印問題
- 4、phpcms v9 如何讓編輯器中上傳的圖片加水印
- 5、各位大神,怎麼把水印加在直播上
- 6、php批量水印添加源碼
我這有個已經實現了php上傳圖片的功能的類,但是現在還要給上傳的每張圖片加水印,請高手幫忙……
給你一個封裝的圖片處理的類吧!包含縮放和剪切功能!
構造方法只需要傳遞圖片所在路徑即可!對應方法及參數都有注釋!
請給予最佳答案!!
?php
class Img{
private $path;
//構造方法,初始化圖片信息
function __construct($path=’./imgs/’){
$this-path=rtrim($path,’/’).’/’;
}
/**
* 對圖片進行縮放
* 參數對應:文件名 縮放後寬度 縮放後高度 縮放後圖片名前綴
*/
function thumb($name,$width,$height,$pre=”th_”){
if(file_exists($this-path.$name)){
$imgInfo=$this-getInfo($name);
$img=$this-getImg($name,$imgInfo);
$newSize=$this-getNewSize($name,$width,$height,$imgInfo);
$newImg=$this-getNewInfo($img,$newSize,$imgInfo);
return $this-createNewImage($newImg, $pre.$name, $imgInfo);
}else{
echo ‘圖片’.$this-path.$name.’不存在,請檢查文件名及路徑是否填寫正確’;
}
}
//輔助圖片處理,獲取圖片的寬、高、類型屬性
private function getInfo($name){
$temp=getImageSize($this-path.$name);
$imgInfo[‘width’]=$temp[0];
$imgInfo[‘height’]=$temp[1];
$imgInfo[‘type’]=$temp[2];
return $imgInfo;
}
//輔助圖片處理,獲取創建的圖片資源
private function getImg($name,$imgInfo){
$src=$this-path.$name;
switch($imgInfo[‘type’]){
case 1:
$img=imagecreatefromgif($src);
break;
case 2:
$img=imagecreatefromjpeg($src);
break;
case 3:
$img=imagecreatefrompng($src);
break;
}
return $img;
}
//輔助圖片處理,獲取創建的圖片資源
private function getNewSize($name,$width,$height,$imgInfo){
$newSize[‘width’]=$imgInfo[‘width’];
$newSize[‘height’]=$imgInfo[‘height’];
if($width$imgInfo[‘width’]){
$newSize[‘width’]=$width;
}
if($height$imgInfo[‘height’]){
$newSize[‘height’]=$height;
}
if($imgInfo[“width”]*$newSize[“width”] $imgInfo[“height”] * $newSize[“height”]){
$newSize[“height”]=round($imgInfo[“height”]*$newSize[“width”]/$imgInfo[“width”]);
}else{
$newSize[“width”]=round($imgInfo[“width”]*$newSize[“height”]/$imgInfo[“height”]);
}
print_r($newSize);
return $newSize;
}
//輔助圖片處理,獲取縮放的圖片資源
private function getNewInfo($img,$newSize,$imgInfo){
$newImg=imagecreatetruecolor($newSize[‘height’],$newSize[‘height’]);
$otsc=imagecolortransparent($img);
if($otsc =0 $otsc = imagecolorstotal($img)){
$tran=imagecolorsforindex($img, $otsc);
$newt=imagecolorallocate($newImg, $tran[“red”], $tran[“green”], $tran[“blue”]);
imagefill($newImg, 0, 0, $newt);
imagecolortransparent($newImg, $newt);
}
imagecopyresized($newImg, $img, 0, 0, 0, 0, $newSize[“width”], $newSize[“height”], $imgInfo[“width”], $imgInfo[“height”]);
imagedestroy($img);
return $newImg;
}
//輔助圖片處理,創建新的圖片
private function createNewImage($newImg, $newName, $imgInfo){
switch($imgInfo[“type”]){
case 1://gif
$result=imageGif($newImg, $this-path.$newName);
break;
case 2://jpg
$result=imageJPEG($newImg, $this-path.$newName);
break;
case 3://png
$return=imagepng($newImg, $this-path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
/**
* 對圖片加水印
* 參數對應:需水印圖片 水印圖片 加水印後圖片名前綴
*/
function waterMark($name,$wname,$pre=”wa_”){
if(file_exists($this-path.$name)){
if(file_exists($this-path.$wname)){
$info=$this-getInfo($name);
$winfo=$this-getInfo($wname);
if($p=$this-getPosition($info,$winfo)){
$img=$this-getImg($name,$info);
$wImg=$this-getImg($wname,$winfo);
imagecopy($img, $wImg, $p[“x”], $p[“y”], 0, 0, $winfo[“width”], $winfo[“height”]);
imagedestroy($wImg);
return $this-createNewImage($img,$pre.$name,$info);
}else{
echo ‘水印圖片尺寸大於原圖片尺寸’;
}
}else{
echo ‘水印圖片’.$this-path.$wname.’不存在,請檢查文件名及路徑是否填寫正確’;
}
}else{
echo ‘圖片’.$this-path.$name.’不存在,請檢查文件名及路徑是否填寫正確’;
}
}
//輔助圖片處理,獲取水印圖片應處坐標
private function getPosition($info,$winfo){
if($info[‘width’]$winfo[‘width’]||$info[‘height’]$winfo[‘height’]){
return false;
}
$x=$info[‘width’]-$winfo[‘width’];
$y=$info[‘height’]-$winfo[‘height’];
return array(‘x’=$x,’y’=$y);
}
/**
* 圖片剪切函數
* 對應參數:原圖片 X坐標 Y坐標 寬度 高度
*/
function cut($name,$x,$y,$width,$height,$pre=’cx_’){
$imgInfo=$this-getInfo($name);
$img=$this-getImg($name,$imgInfo);
$newImg=imagecreatetruecolor($width,$height);
imagecopyresampled($newImg,$img,0,0,$x,$y,$width,$height,$width,$height);
return $this-createNewImage($newImg, $pre.$name, $imgInfo);
}
}
誰有能用的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,$jiaodu=0){
if(isset($img)file_exists($img)){//檢測圖片是否存在
$this-image =$img;
$this-img_text=$txt;
$this-img_text_size=$size;
$this-img_jd=$jiaodu;
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_jiaodu=$this-img_jd;
$ttf_im=imagettfbbox($txt_height,$txt_jiaodu,$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_jiaodu,$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執行FTP 上傳 加水印問題
這個不管你加不加水印,圖片都是先上傳到你的伺服器的緩存,再上傳到FTP,加水印是中間那裡再加個使用GD庫處理一下圖片
phpcms v9 如何讓編輯器中上傳的圖片加水印
登陸後台,進入後台首頁看到頂級菜單的「設置”,
點擊一下,左邊出現「站點設置」,此時看有自己的站點名,
點擊右邊的「修改」,此時出現「編輯站點「對話框,我們滑動滑鼠滾輪,可以看到對話框底部有「附件配置」的相關設置
在這裡可以附件的大小,與附件的類型,
還有最重要的是開啟圖片水印功能,水印的大小設置,水印圖片的設置,
與水印的透明度設置,水印放在圖片的哪個位置,都有非常詳細的說明,
如果不太會,可以每個者嘗試一下,看看效果是否怎樣,需要選擇比較好的效果設置一下,好了
各位大神,怎麼把水印加在直播上
如果添加水印到視頻上電腦不少軟體都可以辦到,如PS即可辦到,但是直播添加水印這個目前還是無法辦到。
php批量水印添加源碼
這個要用到文件列遍函數
$dir=”./images/*.jpg”; //以jpg圖片為例
$image_files=glob($dir); //將目錄中指定類型文件url讀入$image_files數組
然後用循環語句進行添加水印
for($i=0;$icount($image_files);$i++)
{
//調用圖片文件
$this_image=$image_files[$i];
//添加水印代碼
}
原創文章,作者:PJIW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140423.html