PHP GD缩略图生成器0.1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP GD缩略图生成器0.1相关的知识,希望对你有一定的参考价值。

Tweaked from the icant.co.uk thumbnail generator
  1. <?php
  2. ##
  3. #
  4. # $name = full path to original source
  5. # $filename = full path to thumbnail
  6. # $new_w thumbnail width
  7. # $new_h thumbnail height
  8. # $debug echo a few diagnostics
  9. #
  10. ##
  11.  
  12. function createthumb($name,$filename,$new_w=500,$new_h=373,$debug=false) {
  13. $results = "";
  14. $array = explode(".",strtolower($name));
  15. $system = end($array);
  16. $results = "file extension = $system <br>";
  17. if (preg_match("/jpg|jpeg/",$system)) $src_img = imagecreatefromjpeg($name);
  18. if (preg_match("/png/",$system)) $src_img = imagecreatefrompng($name);
  19. $old_x=imageSX($src_img);
  20. $old_y=imageSY($src_img);
  21.  
  22. $results .= "image size = $old_x x $old_y <br>";
  23.  
  24. if($old_x > $old_y) {
  25. $thumb_h = $new_h;
  26. $ratio = $new_h / $old_y;
  27. $thumb_w = ($old_x * $ratio);
  28. }
  29. if($old_x < $old_y) {
  30. $thumb_w = $new_w;
  31. $ratio = $new_w / $old_x;
  32. $thumb_h = ($old_y * $ratio);
  33. }
  34. if($old_x == $old_y) {
  35. $thumb_w = $new_w;
  36. $thumb_h = $new_h;
  37. }
  38. if($new_h < $thumb_h) {
  39. $yloc = round(($thumb_h - $new_h) / 2);
  40. } else {
  41. $yloc = 0;
  42. }
  43.  
  44. $results .= "output size = $thumb_w x $thumb_h <br>
  45. y offset = $yloc <br>";
  46. $dst_img = ImageCreateTrueColor($new_w,$new_h);
  47. imagecopyresampled($dst_img,$src_img,0,0,0,$yloc,$thumb_w,$thumb_h,$old_x,$old_y);
  48. if (preg_match("/png/",$system)) {
  49. imagepng($dst_img,$filename);
  50. } else {
  51. imagejpeg($dst_img,$filename);
  52. }
  53. imagedestroy($dst_img);
  54. imagedestroy($src_img);
  55. if($debug) echo $results;
  56. return "Resized the image successfully!";
  57. }
  58.  
  59. ?>

以上是关于PHP GD缩略图生成器0.1的主要内容,如果未能解决你的问题,请参考以下文章

PHP gd 从生成的图像制作缩略图

听说PHP通过GD库生成缩略图后图片的大小没有变化是吗?

使用 php GD 创建 MP4 视频缩略图

php基础之gd图像生成缩放logo水印和简单验证码实现

PHP缩略图组件的使用?

php使用GD库实现图片水印和缩略图——封装成类