Discuz教程网

PHP用GD库生成高质量的缩略图片

[复制链接]
authicon dly 发表于 2011-3-14 09:11:27 | 显示全部楼层 |阅读模式
PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想。今天试用PHP,GD库来生成缩略图。虽然并不100%完美。可是也应该可以满足缩略图的要求了。
以下是PHP源代码(ResizeImage.php)。
代码如下:


  1. <?php
  2. $FILENAME="image.thumb";
  3. // 生成图片的宽度
  4. $RESIZEWIDTH=400;
  5. // 生成图片的高度
  6. $RESIZEHEIGHT=400;
  7. function ResizeImage($im,$maxwidth,$maxheight,$name){
  8. $width = imagesx($im);
  9. $height = imagesy($im);
  10. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  11. if($maxwidth && $width > $maxwidth){
  12. $widthratio = $maxwidth/$width;
  13. $RESIZEWIDTH=true;
  14. }
  15. if($maxheight && $height > $maxheight){
  16. $heightratio = $maxheight/$height;
  17. $RESIZEHEIGHT=true;
  18. }
  19. if($RESIZEWIDTH && $RESIZEHEIGHT){
  20. if($widthratio < $heightratio){
  21. $ratio = $widthratio;
  22. }else{
  23. $ratio = $heightratio;
  24. }
  25. }elseif($RESIZEWIDTH){
  26. $ratio = $widthratio;
  27. }elseif($RESIZEHEIGHT){
  28. $ratio = $heightratio;
  29. }
  30. $newwidth = $width * $ratio;
  31. $newheight = $height * $ratio;
  32. if(function_exists("imagecopyresampled")){
  33. $newim = imagecreatetruecolor($newwidth, $newheight);
  34. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  35. }else{
  36. $newim = imagecreate($newwidth, $newheight);
  37. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  38. }
  39. ImageJpeg ($newim,$name . ".jpg");
  40. ImageDestroy ($newim);
  41. }else{
  42. ImageJpeg ($im,$name . ".jpg");
  43. }
  44. }
  45. if($_FILES['image']['size']){
  46. if($_FILES['image']['type'] == "image/pjpeg"){
  47. $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  48. }elseif($_FILES['image']['type'] == "image/x-png"){
  49. $im = imagecreatefrompng($_FILES['image']['tmp_name']);
  50. }elseif($_FILES['image']['type'] == "image/gif"){
  51. $im = imagecreatefromgif($_FILES['image']['tmp_name']);
  52. }
  53. if($im){
  54. if(file_exists("$FILENAME.jpg")){
  55. unlink("$FILENAME.jpg");
  56. }
  57. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
  58. ImageDestroy ($im);
  59. }
  60. }
  61. ?>
复制代码

以下是测试代码(demo.php)
代码如下:

  1. <?php
  2. include('ResizeImage.php');
  3. if(!empty($_POST)){
  4. echo($FILENAME.".jpg?cache=".rand(0,999999));
  5. }
  6. ?>
  7. <form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
  8. <input type="file" name="image" size="50" value="浏览"><p>
  9. <input type="submit" value="上传图片">
  10. </form>
复制代码






上一篇:用php的ob_start来生成静态页面的方法分析
下一篇:让PHP COOKIE立即生效,不用刷新就可以使用
authicon 风中徜徉 发表于 2011-5-16 07:59:30 | 显示全部楼层
支持楼主,顶一下
authicon 月之海洋 发表于 2011-5-16 21:59:41 | 显示全部楼层
楼主真强大
authicon rin421 发表于 2011-5-21 14:59:50 | 显示全部楼层
很好 很强大。。谢谢分享
authicon Cute宝贝儿 发表于 2011-5-23 21:59:51 | 显示全部楼层
顶你一下,好贴要顶!
authicon forever21 发表于 2011-5-24 00:59:32 | 显示全部楼层
不错,我喜欢
authicon ningbear 发表于 2011-5-25 15:59:48 | 显示全部楼层
我回不回呢 考虑再三 还是不回了吧 ^_^
authicon 82xiaochong911 发表于 2011-5-28 08:00:02 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon lightning123 发表于 2011-6-18 11:59:41 | 显示全部楼层
楼主威武
authicon 风中徜徉 发表于 2011-6-25 07:59:48 | 显示全部楼层
这个不错呀
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 07:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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