??? class Image {?
??????? private $path;?
??????? //構(gòu)造方法用來對圖片所在位置進行初使化??
??????? function __construct($path="./"){?
??????????? $this->path=rtrim($path, "/")."/";?
??????? }?
??????? /* 對圖片進行縮放
???????? *
???????? * 參數(shù)$name: 是需要處理的圖片名稱
???????? * 參數(shù)$width:是縮放后的寬度
???????? * 參數(shù)$height:是縮放后的高度
???????? * 參數(shù)$qz: 是新圖片的名稱前綴
???????? * 返回值:就是縮放后的圖片名稱,失敗則返回false
???????? *
???????? */?
??????? function thumb($name, $width, $height, $qz="th_"){?
??????????? //獲取圖片信息??
??????????? $imgInfo=$this->getInfo($name); //圖片的寬度,高度,類型??
??????????? //獲取圖片資源, 各種類型的圖片都可以創(chuàng)建資源 jpg, gif, png??
??????????? $srcImg=$this->getImg($name, $imgInfo);?
??????????? //獲取計算圖片等比例之后的大小, $size["width"], $size["height"]??
??????????? $size=$this->getNewSize($name, $width, $height, $imgInfo);?
??????????? //獲取新的圖片資源, 處理一下gif透明背景??
??????????? $newImg=$this->kidOfImage($srcImg, $size, $imgInfo);?
??????????? //另存為一個新的圖片,返回新的縮放后的圖片名稱??????
??????????? return $this->createNewImage($newImg, $qz.$name, $imgInfo);???
??????? }?
?
??????? 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;?
??????? }?
?
??????? private function kidOfImage($srcImg, $size, $imgInfo){?
??????????? $newImg=imagecreatetruecolor($size["width"], $size["height"]);?
?????????????
??????????? $otsc=imagecolortransparent($srcImg);?
?
??????????? if($otsc >=0 && $otsc
??????????????? $tran=imagecolorsforindex($srcImg, $otsc);?
?
??????????????? $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);?
?
??????????????? imagefill($newImg, 0, 0, $newt);?
?
??????????????? imagecolortransparent($newImg, $newt);?
??????????? }?
?
??????????? imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);?
?
??????????? imagedestroy($srcImg);?
?
??????????? return $newImg;?
??????? }?
?
??????? private function getNewSize($name, $width, $height, $imgInfo){?
??????????? $size["width"]=$imgInfo["width"];?
??????????? $size["height"]=$imgInfo["height"];?
?
??????????? //縮放的寬度如果比原圖小才重新設置寬度??
??????????? if($width
??????????????? $size["width"]=$width;?
??????????? }?
??????????? //縮放的高度如果比原圖小才重新設置高度??
??????????? if($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 getInfo($name){?
??????????? $data=getImageSize($this->path.$name);?
?
??????????? $imageInfo["width"]=$data[0];?
??????????? $imageInfo["height"]=$data[1];?
??????????? $imageInfo["type"]=$data[2];?
?
??????????? return $imageInfo;?
??????? }?
?
??????? private function getImg($name, $imgInfo){?
??????????? $srcPic=$this->path.$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;?
?????????????????
??????????? }?
?
??????????? return $img;?
??????? }?
??????? /* 功能:為圖片加水印圖片
???????? * 參數(shù)$groundName: 背景圖片,即需要加水印的圖片
???????? * 參數(shù)$waterName: 水錢圖片
???????? * 參數(shù)#aterPost:水印位置, 10種狀態(tài),?
???????? *? 0為隨機位置
???????? *
???????? *? 1. 為頂端居左? 2. 為頂端居中? 3 為頂端居右
???????? *? 4? 為中部居左? 5. 為中部居中? 6 為中部居右
???????? *? 7 . 為底端居左 8. 為底端居中, 9. 為底端居右
???????? *
???????? * 參數(shù)$qz : 是加水印后的圖片名稱前綴
???????? * 返回值:就是處理后圖片的名稱
???????? *
???????? */?
??????? function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){?
?????????
??????????? if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){?
??????????????? $groundInfo=$this->getInfo($groundName);?
??????????????? $waterInfo=$this->getInfo($waterName);?
??????????????? //水印的位置??
??????????????? if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){?
??????????????????? echo "水印不應該比背景圖片??!";?
??????????????????? return;?
??????????????? }?
?
??????????????? $groundImg=$this->getImg($groundName, $groundInfo);?
??????????????? $waterImg=$this->getImg($waterName, $waterInfo);?
?
??????????????? $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);?
?
??????????????? return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);?
??????????? }else{?
??????????????? echo "圖片或水印圖片不存在";?
??????????????? return false;?
??????????? }?
??????? }?
?
??????? 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 position($groundInfo, $waterInfo, $waterPos){?
??????????? //需要背景比水印圖片大??
??????????? if(($groundInfo["width"]
??????????????? return false;?
??????????? }?
?
??????????? switch($waterPos){?
??????????????? case 1:?
??????????????????? $posX=0;?
??????????????????? $posY=0;?
??????????????????? break;?
??????????????? case 2:?
??????????????????? $posX=($groundInfo["width"]-$waterInfo["width"])/2;?
??????????????????? $posY=0;?
??????????????????? break;?
??????????????? case 3:?
??????????????????? $posX=$groundInfo["width"]-$waterInfo["width"];?
??????????????????? $posY=0;?
??????????????????? break;?
??????????????? case 4:?
??????????????????? $posX=0;?
??????????????????? $posY=($groundInfo["height"]-$waterInfo["height"]) /2;?
??????????????????? break;?
??????????????? case 5:?
??????????????????? $posX=($groundInfo["width"]-$waterInfo["width"])/2;?
??????????????????? $posY=($groundInfo["height"]-$waterInfo["height"]) /2;?
??????????????????? break;?
??????????????? case 6:?
??????????????????? $posX=$groundInfo["width"]-$waterInfo["width"];?
??????????????????? $posY=($groundInfo["height"]-$waterInfo["height"]) /2;?
??????????????????? break;?
??????????????? case 7:?
??????????????????? $posX=0;?
??????????????????? $posY=$groundInfo["height"]-$waterInfo["height"];?
??????????????????? break;?
??????????????? case 8:?
??????????????????? $posX=($groundInfo["width"]-$waterInfo["width"])/2;?
??????????????????? $posY=$groundInfo["height"]-$waterInfo["height"];?
??????????????????? break;?
??????????????? case 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);?
??????? }?
?
??? }?
?class Image {
??private $path;
??//構(gòu)造方法用來對圖片所在位置進行初使化
??function __construct($path="./"){
???$this->path=rtrim($path, "/")."/";
??}
??/* 對圖片進行縮放
?? *
?? * 參數(shù)$name: 是需要處理的圖片名稱
?? * 參數(shù)$width:是縮放后的寬度
?? * 參數(shù)$height:是縮放后的高度
?? * 參數(shù)$qz: 是新圖片的名稱前綴
?? * 返回值:就是縮放后的圖片名稱,失敗則返回false
?? *
?? */
??function thumb($name, $width, $height, $qz="th_"){
???//獲取圖片信息
???$imgInfo=$this->getInfo($name); //圖片的寬度,高度,類型
???//獲取圖片資源, 各種類型的圖片都可以創(chuàng)建資源 jpg, gif, png
???$srcImg=$this->getImg($name, $imgInfo);
???//獲取計算圖片等比例之后的大小, $size["width"], $size["height"]
???$size=$this->getNewSize($name, $width, $height, $imgInfo);
???//獲取新的圖片資源, 處理一下gif透明背景
???$newImg=$this->kidOfImage($srcImg, $size, $imgInfo);
???//另存為一個新的圖片,返回新的縮放后的圖片名稱?
???return $this->createNewImage($newImg, $qz.$name, $imgInfo);?
??}
??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;
??}
??private function kidOfImage($srcImg, $size, $imgInfo){
???$newImg=imagecreatetruecolor($size["width"], $size["height"]);
???
???$otsc=imagecolortransparent($srcImg);
???if($otsc >=0 && $otsc ????$tran=imagecolorsforindex($srcImg, $otsc);
????$newt=imagecolorallocate($newimg, $tran["red"], $tran["green"], $tran["blue"]);
????imagefill($newImg, 0, 0, $newt);
????imagecolortransparent($newImg, $newt);
???}
???imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);
???imagedestroy($srcImg);
???return $newImg;
??}
??private function getNewSize($name, $width, $height, $imgInfo){
???$size["width"]=$imgInfo["width"];
???$size["height"]=$imgInfo["height"];
???//縮放的寬度如果比原圖小才重新設置寬度
???if($width
????$size["width"]=$width;
???}
???//縮放的高度如果比原圖小才重新設置高度
???if($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;
??}
立即學習“PHP免費學習筆記(深入)”;
??private function getInfo($name){
???$data=getImageSize($this->path.$name);
???$imageInfo["width"]=$data[0];
???$imageInfo["height"]=$data[1];
???$imageInfo["type"]=$data[2];
???return $imageInfo;
??}
??private function getImg($name, $imgInfo){
???$srcPic=$this->path.$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;
????
???}
???return $img;
??}
??/* 功能:為圖片加水印圖片
?? * 參數(shù)$groundName: 背景圖片,即需要加水印的圖片
?? * 參數(shù)$waterName: 水錢圖片
?? * 參數(shù)#aterPost:水印位置, 10種狀態(tài),
?? *? 0為隨機位置
?? *
?? *? 1. 為頂端居左? 2. 為頂端居中? 3 為頂端居右
?? *? 4? 為中部居左? 5. 為中部居中? 6 為中部居右
?? *? 7 . 為底端居左 8. 為底端居中, 9. 為底端居右
?? *
?? * 參數(shù)$qz : 是加水印后的圖片名稱前綴
?? * 返回值:就是處理后圖片的名稱
?? *
?? */
??function waterMark($groundName, $waterName, $waterPos=0, $qz="wa_"){
??
???if(file_exists($this->path.$groundName) && file_exists($this->path.$waterName)){
????$groundInfo=$this->getInfo($groundName);
????$waterInfo=$this->getInfo($waterName);
????//水印的位置
????if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){
?????echo "水印不應該比背景圖片?。?;
?????return;
????}
????$groundImg=$this->getImg($groundName, $groundInfo);
????$waterImg=$this->getImg($waterName, $waterInfo);
????$groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo);
????return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo);
???}else{
????echo "圖片或水印圖片不存在";
????return false;
???}
??}
??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 position($groundInfo, $waterInfo, $waterPos){
???//需要背景比水印圖片大
???if(($groundInfo["width"]
????return false;
???}
???switch($waterPos){
????case 1:
?????$posX=0;
?????$posY=0;
?????break;
????case 2:
?????$posX=($groundInfo["width"]-$waterInfo["width"])/2;
?????$posY=0;
?????break;
????case 3:
?????$posX=$groundInfo["width"]-$waterInfo["width"];
?????$posY=0;
?????break;
????case 4:
?????$posX=0;
?????$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
?????break;
????case 5:
?????$posX=($groundInfo["width"]-$waterInfo["width"])/2;
?????$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
?????break;
????case 6:
?????$posX=$groundInfo["width"]-$waterInfo["width"];
?????$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
?????break;
????case 7:
?????$posX=0;
?????$posY=$groundInfo["height"]-$waterInfo["height"];
?????break;
????case 8:
?????$posX=($groundInfo["width"]-$waterInfo["width"])/2;
?????$posY=$groundInfo["height"]-$waterInfo["height"];
?????break;
????case 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);
??}
?}
?摘自 chaojie2009的專欄
PHP怎么學習?PHP怎么入門?PHP在哪學?PHP怎么學才快?不用擔心,這里為大家提供了PHP速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號