PHP GD Thumbnail Generator 0.1


/ Published in: PHP
Save to your folder(s)

Tweaked from the icant.co.uk thumbnail generator


Copy this code and paste it in your HTML
  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. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.