yii\imagine\BaseImage::crop PHP Method

crop() public static method

For example, ~~~ $obj->crop('path\to\image.jpg', 200, 200, [5, 5]); $point = new \Imagine\Image\Point(5, 5); $obj->crop('path\to\image.jpg', 200, 200, $point); ~~~
public static crop ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, array $start = [0, 0] ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$width integer the crop width
$height integer the crop height
$start array the starting point. This must be an array with two elements representing `x` and `y` coordinates.
return Imagine\Image\ImageInterface
    public static function crop($image, $width, $height, array $start = [0, 0])
    {
        if (!isset($start[0], $start[1])) {
            throw new InvalidParamException('$start must be an array of two elements.');
        }
        return static::ensureImageInterfaceInstance($image)->copy()->crop(new Point($start[0], $start[1]), new Box($width, $height));
    }

Usage Example

Example #1
0
 private function saveOriginal()
 {
     if (($ext = pathinfo($this->file, PATHINFO_EXTENSION)) !== '') {
         $ext = strtolower($ext);
     }
     FileHelper::createDirectory($this->folder);
     $path = $this->folder . DIRECTORY_SEPARATOR;
     $imageOriginal = $path . $this->fileName . '.original.' . $ext;
     // Neu anh la anh moi upload
     if (is_object($this->file)) {
         $this->file->saveAs($imageOriginal);
     }
     // Get data image crop
     $data = $this->getData();
     // Crop image and save image
     BaseImage::crop($imageOriginal, $data['width'], $data['height'], [$data['x'], $data['y']])->rotate($data['rotate'])->save($path . $this->fileName . '.' . $ext);
 }