Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1572441
  • 博文数量: 50
  • 博客积分: 9971
  • 博客等级: 中将
  • 技术积分: 2615
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-03 16:03
文章分类

全部博文(50)

文章存档

2011年(2)

2010年(2)

2009年(41)

2008年(5)

我的朋友

分类:

2009-04-29 17:03:51

在用 php 处理图像时,一个最常用的功能就是生成缩略图,本文介绍的是摘录自 AutoIndex 的缩略图显示函数,该函数可以根据图像的扩展名自 动执行对应的处理函数,支持gif、jpg、png,可以自定义缩略图的大小,相信对新手学习php图像处理函数很有帮助。


PHP:
  1.  
  2. // 说明:摘录自 AutoIndex 的缩略图显示函数
  3. // 整理:
  4.  
  5. //显示缩略图核心函数
  6. function display_thumbnail($file, $thumbnail_height)
  7. {
  8. global $html_heading;
  9. if (!@is_file($file))
  10. {
  11. header('HTTP/1.0 404 Not Found');
  12. die("$html_heading

    File not found: ".htmlentities($file).'

    '
    );
  13. }
  14. switch (ext($file))
  15. {
  16. case 'gif':
  17. $src = @imagecreatefromgif($file);
  18. break;
  19. case 'jpeg':
  20. case 'jpg':
  21. case 'jpe':
  22. $src = @imagecreatefromjpeg($file);
  23. break;
  24. case 'png':
  25. $src = @imagecreatefrompng($file);
  26. break;
  27. default:
  28. die("$html_heading

    Unsupported file extension.

    "
    );
  29. }
  30. if ($src === false)
  31. {
  32. die("$html_heading

    Unsupported image type.

    "
    );
  33. }
  34. header('Content-Type: image/jpeg');
  35. header('Cache-Control: public, max-age=3600, must-revalidate');
  36. header('Expires: '.gmdate('D, d M Y H:i:s', time()+3600).' GMT');
  37. $src_height = imagesy($src);
  38. if ($src_height <= $thumbnail_height)
  39. {
  40. imagejpeg($src, '', 100);
  41. }
  42. else
  43. {
  44. $src_width = imagesx($src);
  45. $thumb_width = $thumbnail_height * ($src_width / $src_height);
  46. $thumb = imagecreatetruecolor($thumb_width, $thumbnail_height);
  47. imagecopyresampled($thumb, $src, 0, 0, 0, 0, $thumb_width,
  48. $thumbnail_height, $src_width, $src_height);
  49. imagejpeg($thumb, '', 100);
  50. imagedestroy($thumb);
  51. }
  52. imagedestroy($src);
  53. die();
  54. }
  55.  
  56. //获取文件扩展名
  57. function ext($fn)
  58. //return the lowercase file extension of $fn, not including the leading dot
  59. {
  60. $fn = get_basename($fn);
  61. return (strpos($fn, '.') ? strtolower(substr(strrchr($fn, '.'), 1)) : '');
  62. }
  63.  
  64. //获取完整文件名
  65. function get_basename($fn)
  66. //returns everything after the slash, or the original string if there is no slash
  67. {
  68. return basename(str_replace('\\', '/', $fn));
  69. }
  70.  
  71. //调用方式 参数(图片名,缩略图最大高度)
  72. display_thumbnail('newwebpick.com.jpg', '200');
  73.  
  74. ?>
  75.  
阅读(867) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-08-24 18:59:19

l'sadf