imagesize.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. header("Content-Type: image/jpg");
  3. $image = imagecreatefromjpeg($_REQUEST['image']);
  4. $filename = $_REQUEST['image'];
  5. if ($_REQUEST['w']!='' && $_REQUEST['h']!='') {
  6. $thumb_width = $_REQUEST['w'];
  7. $thumb_height = $_REQUEST['h'];
  8. }
  9. else {
  10. $thumb_width = 400;
  11. $thumb_height = 400;
  12. }
  13. $width = imagesx($image);
  14. $height = imagesy($image);
  15. $original_aspect = $width / $height;
  16. $thumb_aspect = $thumb_width / $thumb_height;
  17. if ( $original_aspect >= $thumb_aspect ) {
  18. $new_height = $thumb_height;
  19. $new_width = $width / ($height / $thumb_height);
  20. }
  21. else {
  22. $new_width = $thumb_width;
  23. $new_height = $height / ($width / $thumb_width);
  24. }
  25. $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
  26. imagecopyresized($thumb,
  27. $image,
  28. 0 - ($new_width - $thumb_width) / 2,
  29. 0 - ($new_height - $thumb_height) / 2,
  30. 0, 0,
  31. $new_width, $new_height,
  32. $width, $height);
  33. imagejpeg($thumb,null,100);