elFinder\elFinderVolumeDriver::imgSquareFit PHP Method

imgSquareFit() protected method

Put image to square
Author: Dmitry (dio) Levashov
Author: Alexey Sukhotin
protected imgSquareFit ( string $path, integer $width, integer $height, integer $align = 'center', integer $valign = 'middle', string $bgcolor = '#0000ff', string $destformat = null ) : string | false
$path string image file
$width integer square width
$height integer square height
$align integer reserved
$valign integer reserved
$bgcolor string square background color in #rrggbb format
$destformat string image destination format
return string | false
    protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null)
    {
        if (($s = @getimagesize($path)) == false) {
            return false;
        }
        $result = false;
        /* Coordinates for image over square aligning */
        $y = ceil(abs($height - $s[1]) / 2);
        $x = ceil(abs($width - $s[0]) / 2);
        switch ($this->imgLib) {
            case 'imagick':
                try {
                    $img = new imagick($path);
                } catch (Exception $e) {
                    return false;
                }
                $img1 = new Imagick();
                $img1->newImage($width, $height, new ImagickPixel($bgcolor));
                $img1->setImageColorspace($img->getImageColorspace());
                $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
                $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                $result = $img1->writeImage($path);
                return $result ? $path : false;
                break;
            case 'gd':
                $img = self::gdImageCreate($path, $s['mime']);
                if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                    self::gdImageBackground($tmp, $bgcolor);
                    if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
                        return false;
                    }
                    $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
                    imagedestroy($img);
                    imagedestroy($tmp);
                    return $result ? $path : false;
                }
                break;
        }
        return false;
    }