Eventviva\ImageResize::crop PHP Méthode

crop() public méthode

Crops image according to the given width, height and crop position
public crop ( integer $width, integer $height, boolean $allow_enlarge = false, integer $position = self::CROPCENTER ) : static
$width integer
$height integer
$allow_enlarge boolean
$position integer
Résultat static
    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;
    }

Usage Example

Exemple #1
0
 /**
  * 修剪图片的大小
  * @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);
     }
 }
All Usage Examples Of Eventviva\ImageResize::crop