四海人民公摄 - 海外华人摄影爱好者论坛

 找回密码
 注册
搜索
热搜: 活动 交友 discuz
查看: 1085|回复: 0

php resize images

[复制链接]
发表于 2009-12-18 14:43:05 | 显示全部楼层 |阅读模式

Resize Images (create thumbnails)

This script will take any image and automatically create a thumbnail of any size you want.   The instructions for using this script are in the comment lines at the top of the script.

  1. <?php
  2. // this script creates a thumbnail image from an image file - can be a .jpg .gif or .png file
  3. // where $thumbsize is the maximum width or height of the resized thumbnail image
  4. // where this script is named resize.php
  5. // call this script with an image tag
  6. // <img src="resize.php?path=imagepath"> where path is a relative path from the document root - such as subdirectory/image.jpg
  7. $thumbsize = 200;
  8. $imagesource = $_SERVER['DOCUMENT_ROOT']."/".$_GET['path'];
  9. if (!file_exists($imagesource)) die();
  10. $filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
  11. if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
  12. if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
  13. if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
  14. if (empty($image)) die();
  15. $imagewidth = imagesx($image);
  16. $imageheight = imagesy($image);
  17. if ($imagewidth >= $imageheight) {
  18. $thumbwidth = $thumbsize;
  19. $factor = $thumbsize / $imagewidth;
  20. $thumbheight = $imageheight * $factor;
  21. }
  22. if ($imageheight >= $imagewidth) {
  23. $thumbheight = $thumbsize;
  24. $factor = $thumbsize / $imageheight;
  25. $thumbwidth = $imagewidth * $factor;
  26. }
  27. $thumb = @imagecreatetruecolor($thumbwidth,$thumbheight);
  28. imagecopyresized($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
  29. header("Content-type: image/jpeg");
  30. imagejpeg($thumb);
  31. imagedestroy($image);
  32. imagedestroy($thumb);
  33. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|四海人民公摄 - 海外华人摄影爱好者网站

GMT+8, 2024-5-18 11:18

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

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