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;
}