Discuz教程网

php缩略图制作代码

[复制链接]
authicon dly 发表于 2012-1-21 21:21:50 | 显示全部楼层 |阅读模式
今天由于上传头像需要,找到了一个很好的程序,不敢独享,特此分享出来,它就是很多朋友可能需要的东西,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. }
  83. ?>
复制代码

使用起来也很简单,而且没有任何问题
  1. $newfile = UPLOAD_DIR."/icons/".md5($_SESSION['USER']->email).".jpg";//上传文件保存的目录
  2. $image = new SimpleImage();
  3. $image->load($_FILES['icons']['tmp_name']);//上传的临时文件名
  4. $image->resizeToWidth(80);设置宽度
  5. $image->save($newfile);
复制代码




上一篇:T中楼了,来申请免费空间
下一篇:bbs.580mxd.com申请免费空间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 09:18

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表