PHP简单生成缩略图相册的方法

这篇文章主要介绍了PHP简单生成缩略图相册的方法,实例分析了php生成缩略图的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下,本文实例讲述了PHP简单生成缩略图相册的方法,分享给大家供大家参考,具体如下:

  1. <?php
  2. /*
  3. * written by mot
  4. * 根目录下自己新建image thumb目录
  5. * */
  6. class thumb{
  7. private $src;
  8. private $source;
  9. private $s_width;
  10. private $s_height;
  11. private $dest;
  12. private $d_height;
  13. private $d_width;
  14. private $name;
  15. public function thumb($image_path,$rate = 0.5){
  16. $this->src = $image_path;
  17. $this->source = imagecreatefromjpeg($image_path);
  18. $s_size = getimagesize($image_path);
  19. $this->s_height = $s_size[1];
  20. $this->s_width = $s_size[0];
  21. $this->d_height = 100;
  22. $this->d_width = 100;
  23. $this->dest = imagecreate($this->d_width, $this->d_height);
  24. $this->name = explode('.jpg', $image_path);
  25. $this->name = $this->name[0];
  26. }
  27. public function make(){
  28. imagecopyresized($this->dest, $this->source, 0, 0, 0, 0, $this->d_width, $this->d_height,
  29. $this->s_width, $this->s_height);
  30. $thumb = str_replace('image', 'thumb', $this->name.'-thumb.jpg');
  31. imagejpeg($this->dest,$thumb,100);
  32. $img = $thumb;
  33. echo "<a href=$this->src><img src=$img></a>";
  34. }
  35. }
  36. $hl = opendir(".\\image\\");
  37. while(false != $file = readdir($hl)){
  38. if($file == '.' || $file == '..') continue;
  39. $path = '.\\image\\'.$file;
  40. $tmp = new thumb($path,0.3);
  41. $tmp->make();
  42. }

希望本文所述对大家的php程序设计有所帮助。