创建图像缩略图并将其缓存在PHP中

创建图像缩略图是很常见的做法,并且有一些脚本可让您使用GD2库在PHP中执行此操作。但是,对于通常应该做的简单任务来说,它们通常是多余的,因此在进行一些搜索和测试后,我发现了以下ImageResize类,该类来自http://shiege.com/scripts/thumbnail/。我已将代码修改为PHP5,但如果要使用PHP4版本,则可以从网站上获取它。

class ImageResize
{
  public $img;
 
  public function ImageResize($imgfile)
  {
    //检测图像格式
    $this->img["format"] = ereg_replace(".*\.(.*)$","\\1",$imgfile);
    $this->img["format"] = strtoupper($this->img["format"]);
    if($this->img["format"] == "JPG" || $this->img["format"] == "JPEG"){
      //JPEG
      $this->img["format"] = "JPEG";
      $this->img["src"]  =  ImageCreateFromJPEG ($imgfile);
    }elseif($this->img["format"] == "PNG"){
      //PNG
      $this->img["format"] = "PNG";
      $this->img["src"]  =  ImageCreateFromPNG ($imgfile);
    }elseif($this->img["format"] == "GIF"){
      //GIF
      $this->img["format"] = "GIF";
      $this->img["src"]  =  ImageCreateFromGif($imgfile);
    } elseif ($this->img["format"] == "WBMP"){
      //WBMP
      $this->img["format"] = "WBMP";
      $this->img["src"]  =  ImageCreateFromWBMP ($imgfile);
    } else {
      //DEFAULT
      echo "Not Supported File";
      exit();
    };
    $this->img["lebar"]  =  imagesx($this->img["src"]);
    $this->img["tinggi"]  =  imagesy($this->img["src"]);
    //默认质量jpeg
    $this->img["quality"] = 75;
  }
 
  public 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"];
  }
 
  public 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"];
  }
 
  public 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"];
    };
  }
 
  public function jpeg_quality($quality = 75)
  {
    //jpeg品质
    $this->img["quality"] = $quality;
  }
 
  public function show()
  {
    //显示拇指
    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"]);
    imagecopyresampled($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"]);
    };
  }
 
  public function save($save = "")
  {
    //节省拇指
    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"]);
    imagecopyresampled($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");
    };
  }
}

将其放入一个名为class.ImageResize.php的文件中,您可以使用它来调整大小并显示所需的任何图像。

include('class.ImageResize.php');
// 创建ImageResize对象
$originalImage = new ImageResize("anImage.jpg");
// 使用显示功能将该图像打印到屏幕上
$originalImage->show();
// 使用保存功能将此图像保存到另一个文件-保留空白以另存为thumb.anImage.jpg
$originalImage->save("anotherFile.png");
// 使用尺寸功能之一调整图像尺寸
$originalImage->size_width(120);
// 再次保存...
$originalImage->save("thumb_anotherFile.png");

如果要创建简单的缩略图缓存功能,则可以使用以下代码。它检查图像是否存在以及是否早于30天。如果是,则删除该文件,并且由于该文件不再存在(或者从不存在),将运行创建缩略图的代码的下一部分。

$imageName = str_replace(dirname("./images/", "" , "http://www.example.com/images/an_image.jpg");
 
if (file_exists("./thumb_cache" . $imageName)) {
  // 2592000 = 30天
  if ( time() - filemtime("./thumb_cache".$imageName) > 2592000 ) {
    unlink("./thumb_cache".$imageName);
  }
}
if (!file_exists("./thumb_cache" . $imageName)) {
  include('class.ImageResize.php');
  // 如果缓存文件不存在,则创建它。
  $originalImage = new ImageResize("./images/" . $_result['image_path']);
  $originalImage->size_width(120);
  $originalImage->save("./thumb_cache".$imageName);
}