加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 学习资料

PHP一般情况下生成的缩略图都比较不理想

时间:2021-03-21 23:50:05  来源:  作者:
PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想。今天试用PHP,GD库来生成缩略图。虽然并不100%完美。可是也应该可以满足缩略图的要求了。
  1. <?php
  2. $FILENAME="image.thumb";
  3. // 生成图片的宽度
  4. $RESIZEWIDTH=400;
  5. // 生成图片的高度
  6. $RESIZEHEIGHT=400;
  7.  
  8. function ResizeImage($im,$maxwidth,$maxheight,$name){
  9. $width = imagesx($im);
  10. $height = imagesy($im);
  11. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  12. if($maxwidth && $width > $maxwidth){
  13. $widthratio = $maxwidth/$width;
  14. $RESIZEWIDTH=true;
  15. }
  16. if($maxheight && $height > $maxheight){
  17. $heightratio = $maxheight/$height;
  18. $RESIZEHEIGHT=true;
  19. }
  20. if($RESIZEWIDTH && $RESIZEHEIGHT){
  21. if($widthratio < $heightratio){
  22. $ratio = $widthratio;
  23. }else{
  24. $ratio = $heightratio;
  25. }
  26. }elseif($RESIZEWIDTH){
  27. $ratio = $widthratio;
  28. }elseif($RESIZEHEIGHT){
  29. $ratio = $heightratio;
  30. }
  31. $newwidth = $width * $ratio;
  32. $newheight = $height * $ratio;
  33. if(function_exists("imagecopyresampled")){
  34. $newim = imagecreatetruecolor($newwidth, $newheight);
  35. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  36. }else{
  37. $newim = imagecreate($newwidth, $newheight);
  38. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  39. }
  40. ImageJpeg ($newim,$name . ".jpg");
  41. ImageDestroy ($newim);
  42. }else{
  43. ImageJpeg ($im,$name . ".jpg");
  44. }
  45. }
  46.  
  47. if($_FILES['image']['size']){
  48. if($_FILES['image']['type'] == "image/pjpeg"){
  49. $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  50. }elseif($_FILES['image']['type'] == "image/x-png"){
  51. $im = imagecreatefrompng($_FILES['image']['tmp_name']);
  52. }elseif($_FILES['image']['type'] == "image/gif"){
  53. $im = imagecreatefromgif($_FILES['image']['tmp_name']);
  54. } 2881064151
  55. if($im){
  56. if(file_exists("$FILENAME.jpg")){
  57. unlink("$FILENAME.jpg");
  58. }
  59. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
  60. ImageDestroy ($im);
  61. }
  62. }
  63. ?>
复制代码
以下是测试代码(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间隔一段时间执行一次某段代码
实现php间隔一段时间
相关文章
    无相关信息
栏目更新
栏目热门