GD Create Image Thumb


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



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function createThumb($width,$height,$quality,$filename,$newfilename){
  4. //@author: zemin
  5. if (file_exists($newfilename) || is_file($newfilename)) return false;
  6. $info = getimagesize($filename);
  7. $types = array(
  8. 1 => 'gif',
  9. 2 => 'jpeg',
  10. 3 => 'png'
  11. );
  12. if (!$types[$info[2]]) return false;
  13. $ratio_orig = $info[0]/$info[1];
  14. if ($width/$height > $ratio_orig) {
  15. $width = $height*$ratio_orig;
  16. } else {
  17. $height = $width/$ratio_orig;
  18. }
  19. eval('
  20. $image_p = imagecreatetruecolor($width, $height);
  21. $image = imagecreatefrom'.$types[$info[2]].'($filename);
  22. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
  23. $result = image'.$types[$info[2]].'($image_p, $newfilename, $quality);
  24. ');
  25. return $result;
  26. }
  27.  
  28. var_dump(createThumb(500,300,100,'files/test.jpg','test/newThumb.jpg'));
  29. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.