aspect ratio friendly image resize


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



Copy this code and paste it in your HTML
  1. $file_sizes = array(
  2. array(100, 75),
  3. array(160, 120),
  4. array(300, 200),
  5. array(200, 150),
  6. array(620, 400),
  7. array(1024, 768)
  8. );
  9. if($file->getWidth() / $file->getHeight() >= 1.5)
  10. {
  11. foreach($file_sizes as $key => $size)
  12. {
  13. $small_file_path = '/home/rocko/Desktop/kingo-disko/test/photo-test/res1_st'.$key.'.jpg';
  14. $line = $file->getHeight() * $size[0] / $size[1];
  15. $file->crop('center', 'center', $line, $file->getHeight())->resize($size[0], $size[1])->saveToFile($small_file_path);
  16. }
  17. }
  18. else
  19. {
  20. foreach($file_sizes as $key => $size)
  21. {
  22. $small_file_path = '/home/rocko/Desktop/kingo-disko/test/photo-test/res1_st'.$key.'.jpg';
  23. $line = $file->getWidth() * $size[1] / $size[0];
  24. $file->crop('center', 'center', $file->getWidth(), $line)->resize($size[0], $size[1])->saveToFile($small_file_path);
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.