Image Size Restrainer


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

This function will return the height and width of an image that will change if the height/width is greater than the maximum supplied width and height.


Copy this code and paste it in your HTML
  1. function sizeme($img){
  2. $maxx=400;
  3. $maxy=300;
  4. list($width,$height)=getimagesize($img);
  5. if($width>$maxx){
  6. $newwidth = $maxx;
  7. $newheight = round($newwidth/$width*$height);
  8. } else {
  9. $newwidth=$width;
  10. $newheight=$height;
  11. }
  12. if($newheight>$maxy){
  13. $newheight1=$maxy;
  14. $newwidth1=round($newheight1/$newheight*$newwidth);
  15. } else {
  16. $newheight1=$newheight;
  17. $newwidth1=$newwidth;
  18. }
  19. $out=array('width'=>$newwidth1,"height"=>$newheight1);
  20. return $out;
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.