php 圖片處理類(簡單易用)

來源:互聯網
上載者:User
一個php圖片處理類,可以縮放圖片,可以給圖片加浮水印,有需要的朋友,可以參考下。

代碼如下:

path=rtrim($path, "/")."/";       }       /* 對圖片進行縮放       *       * 參數$name: 是需要處理的圖片名稱       * 參數$width:是縮放後的寬度       * 參數$height:是縮放後的高度       * 參數$qz: 是新圖片的名稱首碼       * 傳回值:就是縮放後的圖片名稱,失敗則返回false       *       */    function thumb($name, $width, $height, $qz="th_"){    //擷取圖片資訊    $imgInfo=$this->getInfo($name); //圖片的寬度,高度,類型    //擷取圖片資源, 各種類型的圖片都可以建立資源 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 <= imagecolorstotal($srcImg)){        $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 < $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 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;       }       /* 功能:為圖片加浮水印圖片       * 參數$groundName: 背景圖片,即需要加浮水印的圖片       * 參數$waterName: 水錢圖片       * 參數#aterPost:浮水印位置, 10種狀態,        *  0為隨機位置       *       *  1. 為頂端居左  2. 為頂端置中  3 為頂端居右       *  4  為中部居左  5. 為中部置中  6 為中部居右       *  7 . 為底端居左 8. 為底端置中, 9. 為底端居右       *       * 參數$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"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"])){        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);       }        }  
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.