public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
{
if (!$allow_enlarge) {
// this logic is slightly different to resize(),
// it will only reset dimensions to the original
// if that particular dimenstion is larger
if ($width > $this->getSourceWidth()) {
$width = $this->getSourceWidth();
}
if ($height > $this->getSourceHeight()) {
$height = $this->getSourceHeight();
}
}
$ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
$ratio_dest = $width / $height;
if ($ratio_dest < $ratio_source) {
$this->resizeToHeight($height, $allow_enlarge);
$excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();
$this->source_w = $this->getSourceWidth() - $excess_width;
$this->source_x = $this->getCropPosition($excess_width, $position);
$this->dest_w = $width;
} else {
$this->resizeToWidth($width, $allow_enlarge);
$excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();
$this->source_h = $this->getSourceHeight() - $excess_height;
$this->source_y = $this->getCropPosition($excess_height, $position);
$this->dest_h = $height;
}
return $this;
}
/** * 修剪图片的大小 * @param $originName * @param $targetName * @param $width * @param $height * @param bool $allow_enlarge 如果是true则可以将小于原始大小的图片放大 */ public static function resizeImage($originName, $targetName, $width, $height, $allow_enlarge = false, $quality = 100) { if (file_exists($originName)) { $image = new ImageResize($originName); $image->crop($width, $height, $allow_enlarge); $image->save($targetName, null, $quality); } }