/** * Crop image if exceeds boundaries * * @param ImageInterface $image * @param $settings * @return ImageInterface */ private function cropImage(ImageInterface $image, $settings) { $neededSize = new Box($settings['width'], $settings['height']); $currentSize = $image->getSize(); if ($neededSize->contains($currentSize)) { return $image; } $point = new Point($currentSize->getWidth() > $neededSize->getWidth() ? round(($currentSize->getWidth() - $neededSize->getWidth()) / 2) : 0, $currentSize->getHeight() > $neededSize->getHeight() ? round(($currentSize->getHeight() - $neededSize->getHeight()) / 2) : 0); $image->crop($point, $neededSize); return $image; }
/** * {@inheritdoc} */ public function crop(ImageInterface $image, $x, $y, $width, $height) { $point = new Point($x, $y); $box = new Box($width, $height); $image->crop($point, $box); return $image; }
/** * Generate the static map image and add blips to it if any are found * * @return ImageInterface The resulting image * * @return void */ public function generate() { $box = Geo::calculateBox($this->size, $this->centerPoint); $this->resultImage = $this->imagine->create($box['base']); $jj = 0; $xStart = $box['tiles']['start']->getX(); $xStop = $box['tiles']['stop']->getX(); $yStart = $box['tiles']['start']->getY(); $yStop = $box['tiles']['stop']->getY(); for ($i = $yStart - 1; $i < $yStop; $i++) { $ii = 0; for ($j = $xStart - 1; $j < $xStop; $j++) { $this->addTile($this->tiles->getTile($j, $i), new Point($ii * Geo::TILE_SIZE, $jj * Geo::TILE_SIZE)); $ii++; } $jj++; } $this->loader->run(); $this->resultImage->crop($box['crop'], $this->size); foreach ($this->blips as $blip) { $this->drawBlip($blip); } $this->loader->run(); return $this->resultImage; }
/** * Crops the image to the specified coordinates. * * @param int $x1 * @param int $x2 * @param int $y1 * @param int $y2 * * @return Image */ public function crop($x1, $x2, $y1, $y2) { $width = $x2 - $x1; $height = $y2 - $y1; if ($this->_isAnimatedGif) { // Create a new image instance to avoid object references messing up our dimensions. $newSize = new \Imagine\Image\Box($width, $height); $startingPoint = new \Imagine\Image\Point($x1, $y1); $gif = $this->_instance->create($newSize); $gif->layers()->remove(0); foreach ($this->_image->layers() as $layer) { $croppedLayer = $layer->crop($startingPoint, $newSize); $gif->layers()->add($croppedLayer); } $this->_image = $gif; } else { $this->_image->crop(new \Imagine\Image\Point($x1, $y1), new \Imagine\Image\Box($width, $height)); } return $this; }
/** * Crops the image to the specified coordinates. * * @param int $x1 * @param int $x2 * @param int $y1 * @param int $y2 * * @return Image */ public function crop($x1, $x2, $y1, $y2) { $width = $x2 - $x1; $height = $y2 - $y1; if ($this->_isAnimatedGif) { $this->_image->layers()->coalesce(); // Create a new image instance to avoid object references messing up our dimensions. $newSize = new \Imagine\Image\Box($width, $height); $startingPoint = new \Imagine\Image\Point($x1, $y1); $gif = $this->_instance->create($newSize); $gif->layers()->remove(0); foreach ($this->_image->layers() as $layer) { $croppedLayer = $layer->crop($startingPoint, $newSize); $gif->layers()->add($croppedLayer); // Let's update dateUpdated in case this is going to take awhile. if ($index = craft()->assetTransforms->getActiveTransformIndexModel()) { craft()->assetTransforms->storeTransformIndexData($index); } } $this->_image = $gif; } else { $this->_image->crop(new \Imagine\Image\Point($x1, $y1), new \Imagine\Image\Box($width, $height)); } return $this; }
/** * Crops an image. * * ~~~ * * @param integer $width the crop width * @param integer $height the crop height * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates. * @return ImageInterface * @throws InvalidParamException if the `$start` parameter is invalid */ public function crop($width, $height, array $start = [0, 0]) { if (!isset($start[0], $start[1])) { throw new InvalidParamException('$start must be an array of two elements.'); } $this->image->crop(new Point($start[0], $start[1]), new Box($width, $height)); return $this; }
/** * {@inheritdoc} */ public function execute(ImageInterface &$image, $parameters) { $retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1; $x = isset($parameters['x']) ? intval($parameters['x']) * $retina : 0; $y = isset($parameters['y']) ? intval($parameters['y']) * $retina : 0; $width = isset($parameters['w']) ? intval($parameters['w']) : 0; $height = isset($parameters['h']) ? intval($parameters['h']) : 0; $point = new Point($x, $y); $box = new Box($width, $height); $image->crop($point, $box); }
/** * Performs resize of the image. Imagine component is used. * * @param ImageInterface $imagine * @param $resize * @param bool $crop * * @return BoxInterface */ private function performResize($imagine, $resize, $crop = true) { $box = $imagine->getSize(); list($width, $height) = Utils::getDimension($resize); $box = $box->scale(max($width / $box->getWidth(), $height / $box->getHeight())); $imagine->resize($box); if ($crop) { $point = new Point(($box->getWidth() - $width) / 2, ($box->getHeight() - $height) / 2); $imagine->crop($point, new Box($width, $height)); } return $box; }
/** * {@inheritdoc} */ public function execute(ImageInterface $image, $parameters) { @trigger_error('CropTransformation is deprecated since version 1.4. Use the scale config instead', E_USER_DEPRECATED); $retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1; $x = isset($parameters['x']) ? intval($parameters['x']) * $retina : 0; $y = isset($parameters['y']) ? intval($parameters['y']) * $retina : 0; $width = isset($parameters['w']) ? intval($parameters['w']) : 0; $height = isset($parameters['h']) ? intval($parameters['h']) : 0; $point = new Point($x, $y); $box = new Box($width, $height); $image->crop($point, $box); return $image; }
/** * @throws \yii\base\InvalidConfigException */ public function transform(ImageInterface $image, ImagineInterface $imagine) { $this->imageWidth = $image->getSize()->getWidth(); $this->imageHeight = $image->getSize()->getHeight(); $x = $this->parseX($this->x); $y = $this->parseY($this->y); $width = $this->width; $height = $this->height; if ($x + $width > $image->getSize()->getWidth()) { $width -= $x + $width - $image->getSize()->getWidth(); } if ($y + $height > $image->getSize()->getHeight()) { $height -= $y + $height - $image->getSize()->getHeight(); } $image->crop(new Point($x, $y), new Box($width, $height)); }
/** * resizes image using a focal point * @param ImageInterface $image an Imagine image * @param BoxInterface $size size of resized imaga * @param FocalPoint $focal focal point to use for size * @param string $filter Imagine filter to use * @return ImageInterface resized image */ public function resize(ImageInterface $image, BoxInterface $size, FocalPointInterface $focal, $filter = ImageInterface::FILTER_UNDEFINED) { $origSize = $image->getSize(); $origWidth = $origSize->getWidth(); $origHeight = $origSize->getHeight(); $width = $size->getWidth(); $height = $size->getHeight(); $ratioHeight = $origHeight / $height; $ratioWidth = $origWidth / $width; if ($ratioHeight < $ratioWidth) { list($cropX1, $cropX2) = $this->calculateCrop($origWidth, $width, $focal->getX(), $ratioHeight); $cropY1 = 0; $cropY2 = $origHeight; } else { list($cropY1, $cropY2) = $this->calculateCrop($origHeight, $height, -$focal->getY(), $ratioWidth); $cropX1 = 0; $cropX2 = $origWidth; } $image->crop(new Point($cropX1, $cropY1), new Box($cropX2 - $cropX1, $cropY2 - $cropY1))->resize(new Box($width, $height, $filter)); return $image; }
/** * {@inheritdoc} */ public function focus(ImageInterface $image, $x, $y, $width, $height) { $imageSize = $image->getSize(); $currentRatio = $imageSize->getWidth() / $imageSize->getHeight(); $targetRatio = $currentRatio; if ($width !== null && $height !== null) { $targetRatio = $width / $height; } if ($targetRatio < $currentRatio) { $height = $imageSize->getHeight(); $width = $targetRatio * $height; $cropY = 0; switch ($x) { case static::FOCUS_LEFT: $cropX = 0; break; case static::FOCUS_RIGHT: $cropX = $imageSize->getWidth() - $width; break; default: $cropX = ($imageSize->getWidth() - $width) / 2; } } else { $width = $imageSize->getWidth(); $height = $width / $targetRatio; $cropX = 0; switch ($y) { case static::FOCUS_TOP: $cropY = 0; break; case static::FOCUS_BOTTOM: $cropY = $imageSize->getHeight() - $height; break; default: $cropY = ($imageSize->getHeight() - $height) / 2; } } return $image->crop(new Point($cropX, $cropY), new Box($width, $height)); }
/** * Save image * @return ImageInterface */ public function save() { if (file_exists($this->_pathObjectFile)) { $this->_url = $this->_urlObjectFile; return true; } elseif (file_exists($this->_pathObjectFileOrigin)) { $this->_image = Image::getImagine()->open($this->_pathObjectFileOrigin); } elseif (file_exists($this->_pathObjectPlaceholder)) { $this->_url = $this->_urlObjectPlaceholder; return true; } elseif (file_exists($this->_pathObjectPlaceholderOrigin)) { $this->_image = Image::getImagine()->open($this->_pathObjectPlaceholderOrigin); $this->_pathObjectFile = $this->_pathObjectPlaceholder; $this->_urlObjectFile = $this->_urlObjectPlaceholder; } elseif (file_exists($this->_pathRootPlaceholder)) { $this->_url = $this->_urlRootPlaceholder; return true; } elseif (file_exists($this->_pathRootPlaceholderOrigin)) { $this->_image = Image::getImagine()->open($this->_pathRootPlaceholderOrigin); $this->_pathObjectFile = $this->_pathRootPlaceholder; $this->_urlObjectFile = $this->_urlRootPlaceholder; } if ($this->_image) { if (!$this->width || !$this->height) { $ratio = $this->_image->getSize()->getWidth() / $this->_image->getSize()->getHeight(); if ($this->width) { $this->height = ceil($this->width / $ratio); } else { $this->width = ceil($this->height * $ratio); } $box = new Box($this->width, $this->height); $this->_image->resize($box); $this->_image->crop(new Point(0, 0), $box); } else { $box = new Box($this->width, $this->height); $size = $this->_image->getSize(); if ($size->getWidth() <= $box->getWidth() && $size->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { } else { $this->_image = $this->_image->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND); // calculate points $size = $this->_image->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $this->width) { $startX = ceil($this->width - $size->getWidth()) / 2; } if ($size->getHeight() < $this->height) { $startY = ceil($this->height - $size->getHeight()) / 2; } // create empty image to preserve aspect ratio of thumbnail $thumb = Image::getImagine()->create($box, new Color('FFF', 100)); $thumb->paste($this->_image, new Point($startX, $startY)); $this->_image = $thumb; } } $this->_image->save($this->_pathObjectFile); $this->_url = $this->_urlObjectFile; return true; } return false; }
/** * Applies this adjustment to the given Imagine Image object * * @param ImagineImageInterface $image * @return ImagineImageInterface * @internal Should never be used outside of the media package. Rely on the ImageService to apply your adjustments. */ public function applyToImage(ImagineImageInterface $image) { $point = new Point($this->x, $this->y); $box = new Box($this->width, $this->height); return $image->crop($point, $box); }
protected function operationCrop(ImageInterface $image, int $x, int $y, int $width, int $height) { $image->crop(new Point($x, $y), new Box($width, $height)); }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { return $image->crop($this->start, $this->size); }
private function preProcessCropArea(ImageInterface $source, $area) { $data = $this->getCropArea($area); if (!is_null($data)) { $point = new Point($data['x1'], $data['y1']); $box = new Box($data['x2'] - $data['x1'], $data['y2'] - $data['y1']); $source->crop($point, $box); } }
/** * @param ImagineImageInterface $image * * @return ImagineImageInterface */ public function updateFile(ImagineImageInterface $image) { $image->crop(new Point($this->x, $this->y), new Box($this->w, $this->h)); return $image; }
/** * Crops an image. * * For example, * * ~~~ * $obj->crop($Image, 200, 200, [5, 5]); * * $point = new \Imagine\Image\Point(5, 5); * $obj->crop($Image, 200, 200, $point); * ~~~ * * @param Imagine\Image\ImageInterface $Image the image * @param integer $width the crop width * @param integer $height the crop height * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates. * @return \Imagine\Image\ImageInterface * @throws \yii\base\InvalidParamException if the `$start` parameter is invalid */ public static function crop($Image, $width, $height, array $start = [0, 0]) { if (!isset($start[0], $start[1])) { throw new \yii\base\InvalidParamException('$start must be an array of two elements.'); } return $Image->crop(new Imagine\Image\Point($start[0], $start[1]), new Imagine\Image\Box($width, $height)); }
private function pasteImage(ImageInterface $image, ImageInterface $imageToPaste, PointInterface $pastePoint) { if (!$this->boxContains($image->getSize(), $imageToPaste->getSize(), $pastePoint)) { $rectangle = Rectangle::createWithSize($image->getSize())->intersection(Rectangle::create($pastePoint, $imageToPaste->getSize())); if ($rectangle !== null) { $croppingPoint = new Point($rectangle->getStartingPoint()->getX() - $pastePoint->getX(), $rectangle->getStartingPoint()->getY() - $pastePoint->getY()); $imageToPaste->crop($croppingPoint, $rectangle->getSize()); $image->paste($imageToPaste, $this->ensureNonNegativePoint($pastePoint)); } } else { $image->paste($imageToPaste, $pastePoint); } }
/** * @param ImageInterface $image * @param $requestWidth * @param $requestHeight */ protected function cropImageCenter($image, $requestWidth, $requestHeight) { $imageSize = $image->getSize(); $centerLeft = intval(($imageSize->getWidth() - $requestWidth) / 2); if ($centerLeft < 0) { $centerLeft = 0; } $centerTop = intval(($imageSize->getHeight() - $requestHeight) / 2); if ($centerTop < 0) { $centerTop = 0; } $centerPoint = new Point($centerLeft, $centerTop); $image->crop($centerPoint, new Box($requestWidth, $requestHeight)); }
/** * @param \Imagine\Image\ImageInterface $image * @param array $options * @return \Imagine\Image\ImageInterface */ protected function applyCropFilter(ImageInterface $image, $options) { // Defaults $start = new Point(0, 0); $size = new Box(120, 120); if (isset($options['start']) && count($options['start']) == 2) { list($x, $y) = $options['start']; $start = new Point($x, $y); } if (isset($options['size']) && count($options['size']) == 2) { list($width, $height) = $options['size']; $size = new Box($width, $height); } return $image->crop($start, $size); }
/** * @param \Imagine\Image\ImageInterface $image * @param array $commandOptions array('start' => array('x' => 123, 'y' => 456), 'size' => array('width' => 123, 'height => 456)) * @return \Imagine\Image\ImageInterface */ protected function cropCommand(\Imagine\Image\ImageInterface $image, array $commandOptions) { if (!isset($commandOptions['start'])) { } if (!isset($commandOptions['size'])) { } $startPoint = $this->parsePoint($commandOptions['start']); $dimensions = $this->parseBox($commandOptions['size']); return $image->crop($startPoint, $dimensions); }