<?php
 
/* PIMG module: resizes an image keeping it's aspect ratio and fits it out an user defined rectangle */
 
class pimg_fitout
 
{
 
    /* Resources */
 
    private $pimg;
 
    
 
    // PIMG constructor
 
    function __construct($pimg)
 
    {
 
        $this -> pimg = $pimg;
 
    }
 
    
 
    
 
    
 
    /*
 
        Resizes an image keeping it's aspect ratio and fits it out an user defined rectangle
 
        @param: $width - rectangle width
 
        @param: $height - rectangle height
 
        @result: a pointer to the caller pimg class for furthur usage
 
    */
 
    function init($width, $height)
 
    {
 
        /* INPUT VALIDATORS */
 
        if ($width <= 0)
 
            $this -> pimg -> setDebug('Fitout destination width must be > 0', 'error', __CLASS__);
 
        if ($height <= 0)
 
            $this -> pimg -> setDebug('Fitout destination height must be > 0', 'error', __CLASS__);
 
        
 
        // Callculate new width and height
 
        $newWidth = $width;
 
        $newHeight = $this -> pimg -> height() * $newWidth / $this -> pimg -> width();
 
        
 
        if ($newHeight < $height)
 
        {
 
            $newHeight = $height;
 
            $newWidth = $this -> pimg -> width() * $newHeight / $this -> pimg -> height();
 
        }
 
        
 
        // Resize
 
        return $this -> pimg -> stretch($newWidth, $newHeight);
 
    }
 
}
 
?>
 
 |