Find new proportional dimensions of an image in PHP -
i have site , want upload image , resize because have put div dimension.
for example max-width 200px , max-height 100px
i want upload image , check width , height, if bigger max-width or max-height want find size of image stay inside div.
how can resize proportionally size of image? want find new width , new height in base of div 200px*100px
this script:
if(move_uploaded_file($tmp, $path.$actual_image_name)) { list($width, $height, $type, $attr) = getimagesize($tmp); if($width>200){ //too large want resize } if($height>100){ //too height want resize } echo(base_url().$path.$actual_image_name); }
you can use function below stay inside bounding box. eg 200x200. send in filelocation , max width , height. return array $ar[0] new width , $ar[1] new height.
its written out in full can understand math.
<?php function returnsize($maxw,$maxh,$img) { $maxw = ($maxw>0 && is_numeric($maxw)) ? $maxw : 0; $maxh = ($maxh>0 && is_numeric($maxh)) ? $maxh : 0; // file , new size if (!file_exists($img)) { $size[0]=0; $size[1]=0; return $size; } $size = getimagesize($img); if ($maxw>0 && $maxh>0) { if ($size[0]>$maxw) { $scalew = $maxw / $size[0]; } else { $scalew = 1; } if ($size[1]>$maxh) { $scaleh = $maxh / $size[1]; } else { $scaleh = 1; } if ($scalew > $scaleh) { $filew = $size[0] * $scaleh; $fileh = $size[1] * $scaleh; } else { $filew = $size[0] * $scalew; $fileh = $size[1] * $scalew; } } else if ($maxw>0) { if ($size[0]>$maxw) { $scalew = $maxw / $size[0]; } else { $scalew = 1; } $filew = $size[0] * $scalew; $fileh = $size[1] * $scalew; } else if ($maxh>0) { if ($size[1]>$maxh) { $scaleh = $maxh / $size[1]; } else { $scaleh = 1; } $filew = $size[0] * $scaleh; $fileh = $size[1] * $scaleh; } else { $filew = $size[0]; $fileh = $size[1]; } $size[0] = $filew; $size[1] = $fileh; return $size; } ?>
Comments
Post a Comment