Zend - The PHP Company




Resizing

Add Code


Convert height and width to your specific height and width  

Type: code fragment
Added by: fphilipe
Entered: 10/05/2004
Last modified: 05/12/2003
Rating: - (fewer than 3 votes)
Views: 6093
Convert height and width to your specific height and width. This code returns the relative height to the width and otherwise too.


<?
// this function scales an image if it's bigger then the maxwidth or the maxheigth
function scale($img,$maxwidth,$maxheight) {
    
$imginfo getimagesize($img);
    
$imgwidth $imginfo[0];
    
$imgheight $imginfo[1];
    if (
$imgwidth $maxwidth) {
        
$ration $maxwidth/$imgwidth;
        
$newwidth round($imgwidth*$ration);
        
$newheight round($imgheight*$ration);
        if (
$newheight $maxheight) {
            
$ration $maxheight/$newheight;
            
$newwidth round($newwidth*$ration);
            
$newheight round($newheight*$ration);
            return array(
"image" => $img"width" => $newwidth"height" => $newheight);
        } else {
            return array(
"image" => $img"width" => $newwidth"height" => $newheight);
        }
    } else if (
$imgheight $maxheight) {
        
$ration $maxheight/$imgheight;
        
$newwidth round($imgwidth*$ration);
        
$newheight round($imgheight*$ration);
        if (
$newwidth $maxwidth) {
            
$ration $maxwidth/$newwidth;
            
$newwidth round($newwidth*$ration);
            
$newheight round($newheight*$ration);
            return array(
"image" => $img"width" => $newwidth"height" => $newheight);
        } else {
            return array(
"image" => $img"width" => $newwidth"height" => $newheight);
        }
    } else {
        return array(
"image" => $img"width" => $newwidth"height" => $newheight);
    }
}
?>


Usage Example




Rate This Script





Search



This Category All Categories