PHPはGDライブラリのサムネイルに基づいてコードを生成します(jpg、gif、pngフォーマットに対応します)。


相変わらず、直接コードを入れます。

<?php
/**
 *       ,    :
 */

$newimage=new ImageResize();
$newimage->resize("tu.jpg","tu_lit.jpg",1000,1000);
echo $newimage->GetLastError();

class ImageResize{
  private $localimage;//    
  private $remoteimage;//       
  private $localinfo;//    
  private $error;
      
  function resize($localimg, $remoteimg, $x, $y) {
    //      gd    
    if(!$this->_checkenv()){
      return false;
    }
    $this->localimage = $localimg;
    $this->remoteimage = $remoteimg;
    $this->localinfo = getimagesize($this->localimage); //         
    return $this->_resize($x,$y);
  }
  /**
   *           GD
   */
  private function _checkenv(){
    if(!function_exists('gd_info')){
      $this->error[]="       GD    ,    GD    PHP    ";
      return false;
    }
    return true;
  }
  
  /**
   *         
   * @param int $x         
   * @param int $y         
   * @return boolean
   */
  private function _resize($x,$y){
    if(!$this->localinfo){
      $this->error[]="         ";
      return false;
    }
    //      
    $im=@$this->_create($this->localinfo[2]);
    if(!$im){
      $this->error[]="  GD        :{$this->localinfo['mime']}";
      return false;
    }
    $dstsize=$this->_dstsize($x, $y);
    $dstim=@imagecreatetruecolor($dstsize["width"],$dstsize["height"]);
    $whitecolor=@imagecolorallocatealpha($dstim, 0, 0, 0,127);
    imagefill($dstim,0,0,$whitecolor);
    $re=@imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstsize["width"], $dstsize["height"], $this->localinfo[0], $this->localinfo[1]);
    if(!$re){
      $this->error[]="        ";
      return false;
    }
    if(!imagejpeg($dstim, $this->remoteimage)){
      if(!imagepng($dstim,$this->remoteimage)){
        if(!imagegif($dstim,$this->remoteimage)){
          $this->error[]="      {$this->remoteimage}  ,   gd                  。";
          return false;
        }
      }
    }
    $this->error[]="success";
    return true;
  }
  
  /**
   *         ,      
   * @param        $code
   * @return resource/boolean      resourse     false
   */
  private function _create($code){
    $src=$this->localimage;
    switch ($code){
      case 1:
        return imagecreatefromgif($src);
        break;
      case 2:
        return imagecreatefromjpeg($src);
        break;
      case 3:
        return imagecreatefrompng($src);
        break;
      default :
        return false;
        break;
    }
  }
  
  /**
   *           
   * @param int $x         
   * @param int $y         
   * @return array               
   */
  private function _dstsize($x,$y){
    list($srcwidth,$srcheight)=$this->localinfo;
    if(($srcwidth/$srcheight)<($x/$y)){
      $x=floor($y*$srcwidth/$srcheight);
    }else{
      $y=floor($x*$srcheight/$srcwidth);
    }
    $dstsize["width"]=$x;
    $dstsize["height"]=$y;
    return $dstsize;
  }


  /**
   *           
   * return string
   */
  function GetLastError(){
    return array_pop($this->error);
  }
  
  /**
   *         
   * return array
   */
  function GetAllError(){
    return $this->error;
  }
}