PHP ~ resize image function ~

我改了一些code~~~

因為他的寫法是抓檔案的副檔名.........如果遇到xxxx.jpg實際上是png檔的情況

就會出錯

我用另外的寫法避免這問題~~~藍色部分XD

------------------------------------------------------------------------------------------------
class thumbnail
{
 var $img;

 function thumbnail($imgfile)
 {
  //detect image format

  switch(exif_imagetype($imgfile))
  {
   case IMAGETYPE_JPEG:
    $this->img["format"]="JPEG";
    $this->img["src"] = ImageCreateFromJPEG ($imgfile);
    break;
   case IMAGETYPE_PNG:
    $this->img["format"]="PNG";
    $this->img["src"] = ImageCreateFromPNG ($imgfile);
    break;
   case IMAGETYPE_GIF:
    $this->img["format"]="GIF";
    $this->img["src"] = ImageCreateFromGIF ($imgfile);
    break;
   case IMAGETYPE_BMP:
    $this->img["format"]="WBMP";
    $this->img["src"] = ImageCreateFromWBMP ($imgfile);
    break;

   default:
    echo "Not Supported File";
    break;
  } 
  
  @$this->img["lebar"] = imagesx($this->img["src"]);
  @$this->img["tinggi"] = imagesy($this->img["src"]);
  //default quality jpeg
  $this->img["quality"]=75;
 }

 function size_height($size=100)
 {
  //height
     $this->img["tinggi_thumb"]=$size;
     @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
 }

 function size_width($size=100)
 {
  //width
  $this->img["lebar_thumb"]=$size;
     @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
 }

 function size_auto($size=100)
 {
  //size
  if ($this->img["lebar"]>=$this->img["tinggi"]) {
      $this->img["lebar_thumb"]=$size;
      @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
  } else {
      $this->img["tinggi_thumb"]=$size;
      @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
   }
 }

 function jpeg_quality($quality=75)
 {
  //jpeg quality
  $this->img["quality"]=$quality;
 }

 function show()
 {
  //show thumb
  @Header("Content-Type: image/".$this->img["format"]);

  /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
      @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

  if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
   //JPEG
   imageJPEG($this->img["des"],"",$this->img["quality"]);
  } elseif ($this->img["format"]=="PNG") {
   //PNG
   imagePNG($this->img["des"]);
  } elseif ($this->img["format"]=="GIF") {
   //GIF
   imageGIF($this->img["des"]);
  } elseif ($this->img["format"]=="WBMP") {
   //WBMP
   imageWBMP($this->img["des"]);
  }
 }

 function save($save="")
 {
  //save thumb
  if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
  /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
      @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

  if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
   //JPEG
   imageJPEG($this->img["des"],"$save",$this->img["quality"]);
  } elseif ($this->img["format"]=="PNG") {
   //PNG
   imagePNG($this->img["des"],"$save");
  } elseif ($this->img["format"]=="GIF") {
   //GIF
   imageGIF($this->img["des"],"$save");
  } elseif ($this->img["format"]=="WBMP") {
   //WBMP
   imageWBMP($this->img["des"],"$save");
  }
 }
}
------------------------------------------------------------------------------------------------

    $thumbnail = new thumbnail('img/xxx.jpg'');
    $thumbnail -> size_height(180);
    $thumbnail -> size_width(135);
    $thumbnail -> save('img/xxx_thumb.jpg');



source
http://www.phpclasses.org/browse/file/4571.html

2 則留言:

匿名 說...

I would like to exchange links with your site bittyferrari.blogspot.com
Is this possible?

匿名 說...

I would like to exchange links with your site bittyferrari.blogspot.com
Is this possible?