Posted By

Abe on 10/27/10


Tagged

resize image thumbnail


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

luman


Image Resize


 / Published in: PHP
 

URL: http://www.phptoys.com/e107_plugins/content/content.php?content.46

  1. <?php
  2. function resizeImage($originalImage,$toWidth,$toHeight){
  3. // Get the original geometry and calculate scales
  4. list($width, $height) = getimagesize($originalImage);
  5. $xscale=$width/$toWidth;
  6. $yscale=$height/$toHeight;
  7.  
  8. // Recalculate new size with default ratio
  9. if ($yscale>$xscale){
  10. $new_width = round($width * (1/$yscale));
  11. $new_height = round($height * (1/$yscale));
  12. }
  13. else {
  14. $new_width = round($width * (1/$xscale));
  15. $new_height = round($height * (1/$xscale));
  16. }
  17.  
  18. // Resize the original image
  19. $imageResized = imagecreatetruecolor($new_width, $new_height);
  20. $imageTmp = imagecreatefromjpeg ($originalImage);
  21. imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  22.  
  23. return $imageResized;
  24. }
  25. ?>

Report this snippet  

You need to login to post a comment.