php 為圖片加浮水印函數和縮圖的函數代碼_PHP教程

來源:互聯網
上載者:User
php 為圖片加浮水印函數和縮圖的函數代碼
/**
* 為圖片加浮水印
* @param string $desImg 靶心圖表片 參數格式為 ./images/pic.jpg
* @param string $waterImg 浮水印圖片 參數格式同上,浮水印圖片為 png格式,背景透明
* @param int positon 浮水印位置 1:頂部居左 2:頂部居右 3:置中 4 :底部居左 5:底部居右
* @param bool $saveas 是否另存新檔,預設值false,表示覆蓋原圖
* @param int $alpha 浮水印圖片的不透明度
* @return string $savepath 新圖片的路徑
* **/
function watermark($desImg,$waterImg,$positon=1,$saveas=false,$alpha=30)
{
//擷取目圖片的基本資料
$temp=pathinfo($desImg);
$name=$temp["basename"];//檔案名稱
$path=$temp["dirname"];//檔案所在的檔案夾
$extension=$temp["extension"];//副檔名
if($saveas)
{
//需要另存新檔
$name=rtrim($name,".$extension")."_2.";//重新命名
$savepath=$path."/".$name.$extension;
}
else
{
//不需要另存新檔則覆蓋原圖
$savepath=$path."/".$name;
}
$info=getImageInfo($desImg);//擷取靶心圖表片的資訊
$info2=getImageInfo($waterImg);//擷取浮水印圖片的資訊

$desImg=create($desImg);//從原圖建立
$waterImg=create($waterImg);//從浮水印圖片建立
//位置1:頂部居左
if($positon==1)
{
$x=0;
$y=0;
}
//位置2:頂部居右
if($positon==2)
{
$x=$info[0]-$info2[0];
$y=0;
}
//位置3:置中
if($positon==3)
{
$x=($info[0]-$info2[0])/2;
$y=($info[1]-$info2[1])/2;
}
//位置4:底部居左
if($positon==4)
{
$x=0;
$y=$info[1]-$info2[1];
}
//位置5:底部居右
if($positon==5)
{
$x=$info[0]-$info2[0];
$y=$info[1]-$info2[1];
}
imagecopymerge($desImg,$waterImg,$x,$y,0,0,$info2[0],$info2[1],$alpha);
imagejpeg($desImg,$savepath);
imagedestroy($desImg);
imagedestroy($waterImg);
return $savepath;
}
/**
* 擷取圖片的資訊,width,height,image/type
* @param string $src 圖片路徑
* @return 數組
* **/
function getImageInfo($src)
{
return getimagesize($src);
}
/**
* 建立圖片,返回資源類型
* @param string $src 圖片路徑
* @return resource $im 返回資源類型
* **/
function create($src)
{
$info=getImageInfo($src);
switch ($info[2])
{
case 1:
$im=imagecreatefromgif($src);
break;
case 2:
$im=imagecreatefromjpeg($src);
break;
case 3:
$im=imagecreatefrompng($src);
break;
}
return $im;
}
/**
* 縮圖主函數
* @param string $src 圖片路徑
* @param int $w 縮圖寬度
* @param int $h 縮圖高度
* @return mixed 返回縮圖路徑
* **/

function resize($src,$w,$h)
{
$temp=pathinfo($src);
$name=$temp["basename"];//檔案名稱
$dir=$temp["dirname"];//檔案所在的檔案夾
$extension=$temp["extension"];//副檔名
$savepath="{$dir}/{$name}.thumb.jpg";//縮圖儲存路徑,新的檔案名稱為*.thumb.jpg

//擷取圖片的基本資料
$info=getImageInfo($src);
$width=$info[0];//擷取圖片寬度
$height=$info[1];//擷取圖片高度
$per1=round($width/$height,2);//計算原圖長寬比
$per2=round($w/$h,2);//計算縮圖長寬比

//計算縮放比例
if($per1>$per2||$per1==$per2)
{
//原圖長寬比大於或者等於縮圖長寬比,則按照寬度優先
$per=$w/$width;
}
if($per1<$per2)
{
//原圖長寬比小於縮圖長寬比,則按照高度優先
$per=$h/$height;
}
$temp_w=intval($width*$per);//計算原圖縮放後的寬度
$temp_h=intval($height*$per);//計算原圖縮放後的高度
$temp_img=imagecreatetruecolor($temp_w,$temp_h);//建立畫布
$im=create($src);
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
if($per1>$per2)
{
imagejpeg($temp_img,$savepath);
return addBg($savepath,$w,$h,"w");
//寬度優先,在縮放之後高度不足的情況下補上背景
}
if($per1==$per2)
{
imagejpeg($temp_img,$savepath);
return $savepath;
//等比縮放
}
if($per1<$per2)
{
imagejpeg($temp_img,$savepath);

return addBg($savepath,$w,$h,"h");
//高度優先,在縮放之後寬度不足的情況下補上背景
}
}
/**
* 添加背景
* @param string $src 圖片路徑
* @param int $w 背景映像寬度
* @param int $h 背景映像高度
* @param String $first 決定映像最終位置的,w 寬度優先 h 高度優先 wh:等比
* @return 返回加上背景的圖片
* **/
function addBg($src,$w,$h,$fisrt="w")
{
$bg=imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);//填充背景

//擷取靶心圖表片資訊
$info=getImageInfo($src);
$width=$info[0];//靶心圖表片寬度
$height=$info[1];//靶心圖表片高度
$img=create($src);
if($fisrt=="wh")
{
//等比縮放
return $src;
}
else
{
if($fisrt=="w")
{
$x=0;
$y=($h-$height)/2;//垂直置中
}
if($fisrt=="h")
{
$x=($w-$width)/2;//水平置中
$y=0;
}
imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
imagejpeg($bg,$src);
imagedestroy($bg);
imagedestroy($img);
return $src;
}

}


http://www.bkjia.com/PHPjc/444990.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444990.htmlTechArticlephp 為圖片加浮水印函數和縮圖的函數代碼 /** * 為圖片加浮水印 * @param string $desImg 靶心圖表片 參數格式為 ./images/pic.jpg * @param string $waterImg 浮水印...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.