Posted By

toreskog on 03/11/11


Tagged

php resize image gd


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

notturnale
czehner
tux-world


GD Resize Crop


 / Published in: PHP
 

  1. if(isset($_GET['url']) && $_GET['url']!=''){
  2. $tm = explode("?",$_GET['url']);
  3. if(isset($_GET['size'])) {
  4. if($_GET['size']=="tiny") {
  5. $dwidth = 85;
  6. $dheight = 57;
  7. }
  8. else {
  9. $dwidth = 600;
  10. $dheight = 460;
  11. }
  12. }
  13. else {
  14. $dwidth = 500;
  15. $dheight = 250;
  16. }
  17. $picurl = urldecode($_GET['url']);
  18. $info = getimagesize($picurl);
  19.  
  20. list ($width, $height) = $info;
  21. if ($info['mime'] == 'image/jpeg') {
  22. $source = imagecreatefromjpeg($picurl);
  23. }
  24. else if ($info['mime'] == 'image/bmp') {
  25. $source = imagecreatefrombmp($picurl);
  26. }
  27. $source_aspect_ratio = $width / $height;
  28. $desired_aspect_ratio = $dwidth / $dheight;
  29. if ( $source_aspect_ratio > $desired_aspect_ratio ) {
  30. $temp_height = $dheight;
  31. $temp_width = ( int ) ( $dheight * $source_aspect_ratio );
  32. }
  33. else {
  34. $temp_width = $dwidth;
  35. $temp_height = ( int ) ( $dwidth / $source_aspect_ratio );
  36. }
  37. $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  38. imagecopyresampled($temp_gdim, $source, 0, 0, 0, 0, $temp_width, $temp_height, $width, $height);
  39.  
  40. $x0 = ( $temp_width - $dwidth ) / 2;
  41. $y0 = ( $temp_height - $dheight ) / 2;
  42.  
  43. $thumb = imagecreatetruecolor( $dwidth, $dheight );
  44. imagecopy($thumb,$temp_gdim,0, 0, $x0, $y0, $dwidth, $dheight );
  45.  
  46. header('Content-type: image/png');
  47. imagejpeg($thumb, "", 100);
  48. }

Report this snippet  

You need to login to post a comment.