php 다기 능 이미지 처리 클래스 공유

11006 단어 php사진 처리
본 논문 의 사례 는 여러분 에 게 다기 능 phop 이미지 처리 류 를 공유 하 였 으 며,여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

<?php 
/** 
 * Image.class.php       
 * @author Administrator 
 * 
 */ 
class Image{ 
   
  private $path=''; 
  /** 
   *      
   * @param unknown $path 
   */ 
  function __construct($path=''){ 
    if (!empty($path)) { 
      $this->path=$path; 
    } 
  } 
  /** 
   +----------------------------------------------- 
   *        
   +----------------------------------------------- 
   * @param unknown $name          
   * @param unknown $width        
   * @param unknown $height        
   * @param string $thumb_prixs         
   * @return mixed $newname            
   */ 
  function thumb($name,$width,$height,$thumb_prixs='th_'){ 
    //       
    $Info=$this->ImageInfo($name); //     ,  ,   
    //      ,              ,jpg,gif,png 
    $imagres=$this->Img_resouce($name, $Info); 
    //              , 
    $size=$this->getNewSize($name,$width,$height,$Info); 
    //        ,       
    $newimg=$this->getImage($imagres,$size,$Info); 
    //         ,             
    $newname=$this->SaveNewImage($newimg,$thumb_prixs.$name,$Info); 
    return $newname;  
  } 
   
  /** 
   +----------------------------------------------------------------------- 
   *        
   +----------------------------------------------------------------------- 
   * @param unknown $backname        
   * @param unknown $watername       
   * @param number $waterpos       
   * @param string $wa_prixs        
   * @return boolean 
   */ 
  function waterMark($backname,$watername,$waterpos=0,$wa_prixs='wa_') { 
     
    if (file_exists($this->path.$backname) && file_exists($this->path.$watername)) { 
       
      $backinfo=$this->ImageInfo($backname); 
       
      $waterinfo=$this->ImageInfo($watername); 
       
      if(!$pos=$this->getPos($backinfo, $waterinfo, $waterpos)){ 
        echo "             "; 
        return false; 
      } 
       $backimg=$this->Img_resouce($backname, $backinfo); 
        
       $waterimg=$this->Img_resouce($watername, $waterinfo); 
       var_dump($backimg);exit(); 
       $backimg=$this->CopyImage($backimg, $waterimg, $pos, $waterinfo); 
        
       $this->SaveNewImage($backimg, $wa_prixs.$backname, $backinfo); 
    }else{ 
       
      echo "        "; 
      return false; 
    } 
  } 
  /** 
   +----------------------------------------------------------------------- 
   *          
   +----------------------------------------------------------------------- 
   * @param unknown $name 
   * @return unknown 
   */ 
   
   private function ImageInfo($img) { 
    $imageInfo=getimagesize($img); 
    if ($imageInfo!==false) { 
      $imageType=strtolower(substr(image_type_to_extension($imageInfo[2]),1)); 
      $imageSize=filesize($img); 
      $Info=array( 
        "width" => $imageInfo[0], 
        "height" => $imageInfo[1], 
        "type" => $imageType, 
        "size" => $imageSize, 
        "mime" => $imageInfo['mime'] 
      ); 
      return $Info; 
    }else{ 
      return false; 
    }   
  } 
   
   
  /** 
   +-------------------------------------------------------- 
   *           
   +-------------------------------------------------------- 
   * @param unknown $name 
   * @param unknown $imaginfo 
   * @return boolean|resource 
   */ 
  private function Img_resouce($name,$imageinfo){ 
    $iamgeres=$this->path.$name; 
    //var_dump($iamgeres);exit(); 
     switch ($imageinfo['type']) { 
      case 'gif': 
       $img=imagecreatefromgif($name); 
         break; 
      case 'jpg': 
       $img=imagecreatefromjpeg($name); 
         break; 
      case 'png': 
       $img=imagecreatefrompng($name); 
         break;             
     } 
     return $img; 
  } 
  /** 
   +-------------------------------------------------- 
   *            
   +-------------------------------------------------- 
   * @param unknown $name 
   * @param unknown $width 
   * @param unknown $height 
   * @param unknown $imaginfo 
   * @return Ambigous <unknown, number> 
   */ 
  private function getNewSize($name,$width,$height,$imaginfo){ 
       $size['width']=$imaginfo['width']; 
       $size['height']=$imaginfo['height']; 
    if ($width<$imaginfo['width']) { 
      $size['width']=$width; 
    } 
    if ($height<$imaginfo['height']) { 
      $size['height']=$height; 
    } 
    //          
    if ($imaginfo['width']*$size['width']>$imaginfo['height']*$size['height']) { 
      $size['height']=round($imaginfo['height']*$size['width']/$imaginfo['width']); 
    }else{ 
      $size['width']=round($imaginfo['width']*$size['height']/$imaginfo['height']); 
    } 
    return $size;  
  } 
  private function getImage($imageres,$size,$imageinfo){ 
    //          
    $newimg=imagecreatetruecolor($size['width'], $size['height']); 
    //            
    $otsc=imagecolortransparent($imageres); 
    //              
    if ($otsc>=0&&$otsc<=imagecolorstotal($imageres)) { 
      //         
      $stran=imagecolorsforindex($imageres, $otsc); 
      //        
      $newt=imagecolorallocate($imageres, $stran['red'], $stran['green'], $stran['blue']); 
      //       
      imagefill($newimg, 0, 0, $newt); 
      //         
      imagecolortransparent($newimg,$newt);     
    } 
    imagecopyresized($newimg, $imageres, 0, 0, 0, 0, $size['width'], $size['height'], $imageinfo['width'], $imageinfo['height']);   
     
    imagedestroy($imageres); 
     
    return $newimg;  
   } 
   /** 
    +---------------------------------------------- 
    *       
    +---------------------------------------------- 
    * @param unknown $newimg 
    * @param unknown $newname 
    * @param unknown $imageinfo 
    * @return unknown  
    */ 
   private function SaveNewImage($newimg,$newname,$imageinfo){ 
    switch ($imageinfo['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; 
   } 
    
   /** 
    +----------------------------------------------------------------- 
    *          
    +----------------------------------------------------------------- 
    * @param unknown $backinfo       
    * @param unknown $waterinfo       
    * @param unknown $waterpos       
    * @return boolean|multitype:number        
    */ 
   private function getPos($backinfo,$waterinfo,$waterpos) { 
     
    if ($backinfo['width']<$waterinfo['width']||$backinfo['height']<$waterinfo['height']) { 
      return false; 
    } 
    switch ($waterpos) { 
       
      case 1://    
        $posX=0; 
        $posY=0; 
         break; 
      case 2://    
        $posX=$backinfo['width']-$waterinfo['width']/2; 
        $posY=0; 
         break; 
      case 3://    
        $posX=$backinfo['width']-$waterinfo['width']; 
        $posY=0; 
         break; 
      case 4://    
        $posX=0; 
        $posY=$backinfo['height']-$waterinfo['height']/2; 
         break; 
      case 5://    
        $posX=$backinfo['width']-$waterinfo['width']/2; 
        $posY=$backinfo['height']-$waterinfo['height']/2; 
         break; 
      case 6://    
        $posX=$backinfo['width']-$waterinfo['width']; 
        $posY=$backinfo['height']-$waterinfo['height']/2; 
         break; 
      case 7://     
        $posX=0; 
        $posY=$backinfo['height']-$waterinfo['height']; 
         break; 
      case 8://     
        $posX=$backinfo['width']-$waterinfo['width']/2; 
        $posY=$backinfo['height']-$waterinfo['height']; 
         break; 
      case 9://     
        $posX=$backinfo['width']-$waterinfo['width']; 
        $posY=$backinfo['height']-$waterinfo['height']; 
         break; 
      case 0: 
      default : 
        $posX=rand(0,$backinfo['width']-$waterinfo['width']); 
        $posY=rand(0,$backinfo['height']-$waterinfo['height']); 
         break; 
    } 
    return array('posX'=>$posX,'posY'=>$posY); 
   } 
   /** 
    +------------------------------------------------------------------- 
    *      
    +------------------------------------------------------------------- 
    * @param unknown $backimg        
    * @param unknown $waterimg       
    * @param unknown $pos          
    * @param unknown $waterinfo       
    * @return unknown 
    */ 
   private function CopyImage($backimg,$waterimg,$pos,$waterinfo) { 
      
    imagecopy($backimg, $waterimg, $pos['posX'], $pos['posY'], 0, 0, $waterinfo['width'], $waterinfo['height']); 
    imagedestroy($waterimg);     
     return $backimg; 
   } 
     
} 
?> 
이상 은 본 고의 모든 내용 이 므 로 여러분 들 이 PHP 프로 그래 밍 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기