demianturner / sgl-docs-tickets-migration-test

0 stars 0 forks source link

Image crop strategy #1538

Closed demianturner closed 11 years ago

demianturner commented 11 years ago

This code scales down then crops an image so that thumbnails are always the same size e.g. 55 x 55 no matter the aspect ratio of the uploaded file.

I saved this file as lib/SGL/ImageTransform/CropStrategy.php

{{{ class SGL_ImageTransform_CropStrategy extends SGL_ImageTransformStrategy { function transform() {

    $newWidth  = null;
    $newHeight = null;

    // get the current size of the file
    $aSize   = $this->driver->getImageSize();
    $width = $aSize[0];
    $height = $aSize[1];

    // get the required size of the file
    if (isset($this->aParams['width'])) {
        $newWidth = $this->aParams['width'];
    }
    if (isset($this->aParams['height'])) {
        $newHeight = $this->aParams['height'];
    }

    // Set the crop from coordinates (default top left)
    $x = 0; 
    $y = 0;

    // Check which way the fie should be scaled first, before cropping
    if ( $width > $height ) {
        $percentChange = $newHeight / $height;
        $scaleWidth = round($width * $percentChange);
        $scaleHeight = round($height * $percentChange);
    } else if ( $width < $height ) {
        $percentChange = $newWidth / $width;
        $scaleWidth = round($width * $percentChange);
        $scaleHeight = round($height * $percentChange);
    } else {
        $scaleWidth = $newWidth;
        $scaleHeight = $newHeight;
    }

    $ret = $this->driver->scaleByXY($scaleWidth + 1, $scaleHeight + 1);

    $ret = $this->driver->crop($newWidth, $newHeight, $x, $y);

    return $ret;

}

}

Then used:

[default_small] crop = width:120,height:120

in modules/media/image.ini

not really sure how to submit this to Seagull project. }}}

demianturner commented 11 years ago

[lakiboy] Sorry for long reply. The logic looks good, but can u please submit a patch like all other folks do?

Then, please, follow our coding standards (i.e. PEAR CS). And put example parameter into modules/media/image.ini with others.

Dmitri

demianturner commented 11 years ago

[lakiboy] added crop strategy in [3571]