php生成缩略图经典类

从国外网站找到的一款php生成缩略图代码,有需要的朋友可以参考一下,代码如下:

  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details:
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. */
  21. class SimpleImage {
  22. var $image;
  23. var $image_type;
  24. function load($filename) {
  25. $image_info = getimagesize($filename);
  26. $this->image_type = $image_info[2];
  27. if( $this->image_type == IMAGETYPE_JPEG ) {
  28. $this->image = imagecreatefromjpeg($filename);
  29. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  30. $this->image = imagecreatefromgif($filename);
  31. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  32. $this->image = imagecreatefrompng($filename);
  33. }
  34. }
  35. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  36. if( $image_type == IMAGETYPE_JPEG ) {
  37. imagejpeg($this->image,$filename,$compression);
  38. } elseif( $image_type == IMAGETYPE_GIF ) {
  39. imagegif($this->image,$filename);
  40. } elseif( $image_type == IMAGETYPE_PNG ) {
  41. imagepng($this->image,$filename);
  42. }
  43. if( $permissions != null) {
  44. chmod($filename,$permissions);
  45. }
  46. }
  47. function output($image_type=IMAGETYPE_JPEG) {
  48. if( $image_type == IMAGETYPE_JPEG ) {
  49. imagejpeg($this->image);
  50. } elseif( $image_type == IMAGETYPE_GIF ) {
  51. imagegif($this->image);
  52. } elseif( $image_type == IMAGETYPE_PNG ) {
  53. imagepng($this->image);
  54. }
  55. }
  56. function getWidth() {
  57. return imagesx($this->image);
  58. }
  59. function getHeight() {
  60. return imagesy($this->image);
  61. }
  62. function resizeToHeight($height) {
  63. $ratio = $height / $this->getHeight();
  64. $width = $this->getWidth() * $ratio;
  65. $this->resize($width,$height);
  66. }
  67. function resizeToWidth($width) {
  68. $ratio = $width / $this->getWidth();
  69. $height = $this->getheight() * $ratio;
  70. $this->resize($width,$height);
  71. }
  72. function scale($scale) {
  73. $width = $this->getWidth() * $scale/100;
  74. $height = $this->getheight() * $scale/100;
  75. $this->resize($width,$height);
  76. }
  77. function resize($width,$height) {
  78. $new_image = imagecreatetruecolor($width, $height);
  79. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  80. $this->image = $new_image;
  81. }
  82. }//开源代码phpfensi.com
  83. ?>

保存文件作为SimpleImage之上,php和看看以下的例子如何使用脚本,下面的第一个例子将加载一个文件命名图片,jpg调整到250像素宽,400像素高,picture2.jpg重新保存,代码如下:

  1. <?php
  2. include('SimpleImage.php');
  3. $image = new SimpleImage();
  4. $image->load('picture.jpg');
  5. $image->resize(250,400);
  6. $image->save('picture2.jpg');
  7. ?>

如果你想调整specifed宽度但保持尺寸比例相同的脚本可以为你计算出所需的高度,只使用resizeToWidth函数,代码如下:

  1. <?php
  2. include('SimpleImage.php');
  3. $image = new SimpleImage();
  4. $image->load('picture.jpg');
  5. $image->resizeToWidth(250);
  6. $image->save('picture2.jpg');//开源代码phpfensi.com
  7. ?>