PHP Thumbnail function


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



Copy this code and paste it in your HTML
  1. function set_thumb($file, $photos_dir=�uploads/photos�,$thumbs_dir=�uploads/photos/thumbs�, $square_size=167, $quality=100) {
  2. //check if thumb exists
  3. if (!file_exists($thumbs_dir."/".$file)) {
  4. //get image info
  5. list($width, $height, $type, $attr) = getimagesize($photos_dir."/".$file);
  6. //set dimensions
  7. if($width> $height) {
  8. $width_t=$square_size;
  9. //respect the ratio
  10. $height_t=round($height/$width*$square_size);
  11. //set the offset
  12. $off_y=ceil(($width_t-$height_t)/2);
  13. $off_x=0;
  14. } elseif($height> $width) {
  15. $height_t=$square_size;
  16. $width_t=round($width/$height*$square_size);
  17. $off_x=ceil(($height_t-$width_t)/2);
  18. $off_y=0;
  19. } else {
  20. $width_t=$height_t=$square_size;
  21. $off_x=$off_y=0;
  22. }
  23. $thumb=imagecreatefromjpeg($photos_dir."/".$file);
  24. $thumb_p = imagecreatetruecolor($square_size, $square_size);
  25. //default background is black
  26. $bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
  27. imagefill ( $thumb_p, 0, 0, $bg );
  28. imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
  29. imagejpeg($thumb_p,$thumbs_dir."/".$file,$quality);
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.