《PHP實戰:一個經典實用的PHP圖像處理類分享》要點:
本文介紹了PHP實戰:一個經典實用的PHP圖像處理類分享,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本圖像處理類可以完成對圖片的縮放、加水印和裁剪的功能,支持多種圖片類型的處理,縮放時進行優化等.
PHP應用
<?php
/**
file: image.class.php 類名為Image
圖像處理類,可以完成對各種類型的圖像進行縮放、加圖片水印和剪裁的操作.
*/
class Image {
/* 圖片保存的路徑 */
private $path;
/**
* 實例圖像對象時傳遞圖像的一個路徑,默認值是當前目錄
* @param string $path 可以指定處理圖片的路徑
*/
function __construct($path="./"){
$this->path = rtrim($path,"/")."/";
}
/**
* 對指定的圖像進行縮放
* @param string $name 是需要處理的圖片名稱
* @param int $width 縮放后的寬度
* @param int $height 縮放后的高度
* @param string $qz 是新圖片的前綴
* @return mixed 是縮放后的圖片名稱,失敗返回false;
*/
function thumb($name, $width, $height,$qz="th_"){
/* 獲取圖片寬度、高度、及類型信息 */
$imgInfo = $this->getInfo($name);
/* 獲取背景圖片的資源 */
$srcImg = $this->getImg($name, $imgInfo);
/* 獲取新圖片尺寸 */
$size = $this->getNewSize($name,$width, $height,$imgInfo);
/* 獲取新的圖片資源 */
$newImg = $this->kidOfImage($srcImg, $size,$imgInfo);
/* 通過本類的私有辦法,保存縮略圖并返回新縮略圖的名稱,以"th_"為前綴 */
return $this->createNewImage($newImg, $qz.$name,$imgInfo);
}
/**
* 為圖片添加水印
* @param string $groundName 背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式
* @param string $waterName 圖片水印,即作為水印的圖片,暫只支持GIF,JPG,PNG格式
* @param int $waterPos 水印位置,有10種狀態,0為隨機位置;
* 1為頂端居左,2為頂端居中,3為頂端居右;
* 4為中部居左,5為中部居中,6為中部居右;
* 7為底端居左,8為底端居中,9為底端居右;
* @param string $qz 加水印后的圖片的文件名在原文件名前面加上這個前綴
* @return mixed 是生成水印后的圖片名稱,失敗返回false
*/
function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
/*獲取水印圖片是當前路徑,還是指定了路徑*/
$curpath = rtrim($this->path,"/")."/";
$dir = dirname($waterName);
if($dir == "."){
$wpath = $curpath;
}else{
$wpath = $dir."/";
$waterName = basename($waterName);
}
/*水印圖片和背景圖片必須都要存在*/
if(file_exists($curpath.$groundName) && file_exists($wpath.$waterName)){
$groundInfo = $this->getInfo($groundName); //獲取背景信息
$waterInfo = $this->getInfo($waterName, $dir); //獲取水印圖片信息
/*如果背景比水印圖片還小,就會被水印全部蓋住*/
if(!$pos = $this->position($groundInfo, $waterInfo, $waterPos)){
echo '水印不應該比背景圖片小!';
return false;
}
$groundImg = $this->getImg($groundName, $groundInfo); //獲取背景圖像資源
$waterImg = $this->getImg($waterName, $waterInfo, $dir); //獲取水印圖片資源
/* 調用私有辦法將水印圖像按指定位置復制到背景圖片中 */
$groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
/* 通過本類的私有辦法,保存加水圖片并返回新圖片的名稱,默認以"wa_"為前綴 */
return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
}else{
echo '圖片或水印圖片不存在!';
return false;
}
}
/**
* 在一個大的背景圖片中剪裁出指定區域的圖片
* @param string $name 需要剪切的背景圖片
* @param int $x 剪切圖片左邊開始的位置
* @param int $y 剪切圖片頂部開始的位置
* @param int $width 圖片剪裁的寬度
* @param int $height 圖片剪裁的高度
* @param string $qz 新圖片的名稱前綴
* @return mixed 裁剪后的圖片名稱,失敗返回false;
*/
function cut($name, $x, $y, $width, $height, $qz="cu_"){
$imgInfo=$this->getInfo($name); //獲取圖片信息
/* 裁剪的位置不能超出背景圖片范圍 */
if( (($x+$width) > $imgInfo['width']) || (($y+$height) > $imgInfo['height'])){
echo "裁剪的位置超出了背景圖片范圍!";
return false;
}
$back = $this->getImg($name, $imgInfo); //獲取圖片資源
/* 創建一個可以保存裁剪后圖片的資源 */
$cutimg = imagecreatetruecolor($width, $height);
/* 使用imagecopyresampled()函數對圖片進行裁剪 */
imagecopyresampled($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height);
imagedestroy($back);
/* 通過本類的私有辦法,保存剪切圖并返回新圖片的名稱,默認以"cu_"為前綴 */
return $this->createNewImage($cutimg, $qz.$name,$imgInfo);
}
/* 內部使用的私有辦法,用來確定水印圖片的位置 */
private function position($groundInfo, $waterInfo, $waterPos){
/* 需要加水印的圖片的長度或寬度比水印還小,無法生成水印 */
if( ($groundInfo["width"]<$waterInfo["width"]) || ($groundInfo["height"]<$waterInfo["height"]) ) {
return false;
}
switch($waterPos) {
case 1: //1為頂端居左
$posX = 0;
$posY = 0;
break;
case 2: //2為頂端居中
$posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
$posY = 0;
break;
case 3: //3為頂端居右
$posX = $groundInfo["width"] - $waterInfo["width"];
$posY = 0;
break;
case 4: //4為中部居左
$posX = 0;
$posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
break;
case 5: //5為中部居中
$posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
$posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
break;
case 6: //6為中部居右
$posX = $groundInfo["width"] - $waterInfo["width"];
$posY = ($groundInfo["height"] - $waterInfo["height"]) / 2;
break;
case 7: //7為底端居左
$posX = 0;
$posY = $groundInfo["height"] - $waterInfo["height"];
break;
case 8: //8為底端居中
$posX = ($groundInfo["width"] - $waterInfo["width"]) / 2;
$posY = $groundInfo["height"] - $waterInfo["height"];
break;
case 9: //9為底端居右
$posX = $groundInfo["width"] - $waterInfo["width"];
$posY = $groundInfo["height"] - $waterInfo["height"];
break;
case 0:
default: //隨機
$posX = rand(0,($groundInfo["width"] - $waterInfo["width"]));
$posY = rand(0,($groundInfo["height"] - $waterInfo["height"]));
break;
}
return array("posX"=>$posX, "posY"=>$posY);
}
/* 內部使用的私有辦法,用于獲取圖片的屬性信息(寬度、高度和類型) */
private function getInfo($name, $path=".") {
$spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/';
$data = getimagesize($spath.$name);
$imgInfo["width"] = $data[0];
$imgInfo["height"] = $data[1];
$imgInfo["type"] = $data[2];
return $imgInfo;
}
/*內部使用的私有辦法, 用于創建支持各種圖片格式(jpg,gif,png三種)資源 */
private function getImg($name, $imgInfo, $path='.'){
$spath = $path=="." ? rtrim($this->path,"/")."/" : $path.'/';
$srcPic = $spath.$name;
switch ($imgInfo["type"]) {
case 1: //gif
$img = imagecreatefromgif($srcPic);
break;
case 2: //jpg
$img = imagecreatefromjpeg($srcPic);
break;
case 3: //png
$img = imagecreatefrompng($srcPic);
break;
default:
return false;
break;
}
return $img;
}
/* 內部使用的私有辦法,返回等比例縮放的圖片寬度和高度,如果原圖比縮放后的還小保持不變 */
private function getNewSize($name, $width, $height, $imgInfo){
$size["width"] = $imgInfo["width"]; //原圖片的寬度
$size["height"] = $imgInfo["height"]; //原圖片的高度
if($width < $imgInfo["width"]){
$size["width"]=$width; //縮放的寬度如果比原圖小才重新設置寬度
}
if($height < $imgInfo["height"]){
$size["height"] = $height; //縮放的高度如果比原圖小才重新設置高度
}
/* 等比例縮放的算法 */
if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){
$size["height"] = round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);
}else{
$size["width"] = round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);
}
return $size;
}
/* 內部使用的私有辦法,用于保存圖像,并保留原有圖片格式 */
private function createNewImage($newImg, $newName, $imgInfo){
$this->path = rtrim($this->path,"/")."/";
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
$result = imagePng($newImg, $this->path.$newName);
break;
}
imagedestroy($newImg);
return $newName;
}
/* 內部使用的私有辦法,用于加水印時復制圖像 */
private function copyImage($groundImg, $waterImg, $pos, $waterInfo){
imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"],$waterInfo["height"]);
imagedestroy($waterImg);
return $groundImg;
}
/* 內部使用的私有辦法,處理帶有透明度的圖片保持原樣 */
private function kidOfImage($srcImg, $size, $imgInfo){
$newImg = imagecreatetruecolor($size["width"], $size["height"]);
$otsc = imagecolortransparent($srcImg);
if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {
$transparentcolor = imagecolorsforindex( $srcImg, $otsc );
$newtransparentcolor = imagecolorallocate(
$newImg,
$transparentcolor['red'],
$transparentcolor['green'],
$transparentcolor['blue']
);
imagefill( $newImg, 0, 0, $newtransparentcolor );
imagecolortransparent( $newImg, $newtransparentcolor );
}
imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );
imagedestroy($srcImg);
return $newImg;
}
}
《PHP實戰:一個經典實用的PHP圖像處理類分享》是否對您有啟發,歡迎查看更多與《PHP實戰:一個經典實用的PHP圖像處理類分享》相關教程,學精學透。維易PHP學院為您提供精彩教程。