app/maguttiCms/Tools/ImgHelper.php
// if one dimension is missing, the other is calculated automatically, keeping aspect ratio
if one dimension is missing, the other is calculated automatically, keeping aspect ratio
if one dimension is missing the other is calculated automatically keeping aspect ratio
private function resample($obj, $args)
    {
        $c = $this->arg($args, 'c');
        $p = $this->arg($args, 'p');
        $w_original = $obj->width();
        $h_original = $obj->height();
        $r_original = $w_original / $h_original;
        $w_final = isset($args['w']) ? preg_replace('/([A-Za-z])+/', '', $args['w']) : null;
        $h_final = isset($args['h']) ? preg_replace('/([A-Za-z])+/', '', $args['h']) : null;
        if (!$w_final || !$h_final) {
            $r_final = $r_original;
        } else {
            $r_final = $w_final / $h_final;
        }
        if (!$h_final) {
            $h_final = round($w_final / $r_final);
        }
        if (!$w_final) {
            $w_final = round($h_final * $r_final);
        }
        switch ($c) {
            case 'fill':
                $obj->resize($w_final, $h_final);
                break;
            case 'best':
                if ($r_original >= $r_final) {
                    $obj->resize($w_final, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } else {
                    $obj->resize(null, $h_final, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                }
                break;
            case 'contain':
                if ($r_original >= $r_final) {
                    $obj->resize($w_final, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } else {
                    $obj->resize(null, $h_final, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                }
                $obj->resizeCanvas($w_final, $h_final, $p);
                break;
            default:
                $obj->fit($w_final, $h_final, function () {
                }, $p);
                break;
        }
        return $obj;
    }
private function resample ( $ obj , $ args ) { $ c = $ this -> arg ( $ args , 'c' ) ; $ p = $ this -> arg ( $ args , 'p' ) ; $ w_original = $ obj -> width ( ) ; $ h_original = $ obj -> height ( ) ; $ r_original = $ w_original / $ h_original ; $ w_final = isset ( $ args [ 'w' ] ) ? preg_replace ( '/([A-Za-z])+/' , '' , $ args [ 'w' ] ) : null ; $ h_final = isset ( $ args [ 'h' ] ) ? preg_replace ( '/([A-Za-z])+/' , '' , $ args [ 'h' ] ) : null ; if ( ! $ w_final || ! $ h_final ) { $ r_final = $ r_original ; } else { $ r_final = $ w_final / $ h_final ; } if ( ! $ h_final ) { $ h_final = round ( $ w_final / $ r_final ) ; } if ( ! $ w_final ) { $ w_final = round ( $ h_final * $ r_final ) ; } switch ( $ c ) { case 'fill' : $ obj -> resize ( $ w_final , $ h_final ) ; break ; case 'best' : if ( $ r_original >= $ r_final ) { $ obj -> resize ( $ w_final , null , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; } else { $ obj -> resize ( null , $ h_final , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; } break ; case 'contain' : if ( $ r_original >= $ r_final ) { $ obj -> resize ( $ w_final , null , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; } else { $ obj -> resize ( null , $ h_final , function ( $ constraint ) { $ constraint -> aspectRatio ( ) ; } ) ; } $ obj -> resizeCanvas ( $ w_final , $ h_final , $ p ) ; break ; default : $ obj -> fit ( $ w_final , $ h_final , function ( ) { } , $ p ) ; break ; } return $ obj ; }
if one dimension is missing, the other is calculated automatically, keeping aspect ratio
if one dimension is missing the other is calculated automatically keeping aspect ratio
//determine original and final width, height and ratio