/** * Put image to square * * @param string $path image file * @param int $width square width * @param int $height square height * @param int $align reserved * @param int $valign reserved * @param string $bgcolor square background color in #rrggbb format * @param string $destformat image destination format * @return string|false * @author Dmitry (dio) Levashov * @author Alexey Sukhotin **/ 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': if ($s['mime'] == 'image/jpeg') { $img = imagecreatefromjpeg($path); } elseif ($s['mime'] == 'image/png') { $img = imagecreatefrompng($path); } elseif ($s['mime'] == 'image/gif') { $img = imagecreatefromgif($path); } elseif ($s['mime'] == 'image/xbm') { $img = imagecreatefromxbm($path); } if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) { if ($bgcolor == 'transparent') { list($r, $g, $b) = array(0, 0, 255); } else { list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); } $bgcolor1 = imagecolorallocate($tmp, $r, $g, $b); if ($bgcolor == 'transparent') { $bgcolor1 = imagecolortransparent($tmp, $bgcolor1); } imagefill($tmp, 0, 0, $bgcolor1); if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) { return false; } if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') { $result = imagejpeg($tmp, $path, 100); } else { if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') { $result = imagegif($tmp, $path, 7); } else { $result = imagepng($tmp, $path, 7); } } imagedestroy($img); imagedestroy($tmp); return $result ? $path : false; } break; } return false; }
/** * Put image to square * * @param string $path image file * @param int $width square width * @param int $height square height * @param int $align reserved * @param int $valign reserved * @param string $bgcolor square background color in #rrggbb format * @param string $destformat image destination format * @return string|false * @author Dmitry (dio) Levashov * @author Alexey Sukhotin **/ 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; }