/** * 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; }
public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND, $enlarge = false) { $box = new Box($width, $height); $img = static::getImagine()->open(Yii::getAlias($filename)); if (!$box->getWidth() && !$box->getHeight()) { return $img->copy(); } elseif ($img->getSize()->getWidth() <= $box->getWidth() || $img->getSize()->getHeight() <= $box->getHeight()) { if ($enlarge) { $ratio = max($box->getWidth() / $img->getSize()->getWidth(), $box->getHeight() / $img->getSize()->getHeight()); $enlargeBox = new Box($img->getSize()->getWidth() * $ratio, $img->getSize()->getHeight() * $ratio); $img->resize($enlargeBox); } else { return $img->copy(); } } $img = $img->thumbnail($box, $mode); // create empty image to preserve aspect ratio of thumbnail $thumb = static::getImagine()->create($box, new Color('FFF', 100)); // calculate points /** @var $img \Imagine\Image\ImageInterface */ $size = $img->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $width) { $startX = ceil($width - $size->getWidth()) / 2; } if ($size->getHeight() < $height) { $startY = ceil($height - $size->getHeight()) / 2; } $thumb->paste($img, new Point($startX, $startY)); return $thumb; }
/** * @param $img \Imagine\Gmagick\Imagine|\Imagine\Imagick\Imagine|\Imagine\Gd\Imagine * @param $width * @param $height * @param $method * @return null */ public function resize($img, $width, $height, $method) { $box = new Box($width, $height); $imgBox = $img->getSize(); if ($imgBox->getWidth() <= $box->getWidth() && $imgBox->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { return $img; } if ($method == 'resize') { $img = $img->thumbnail($box, ManipulatorInterface::THUMBNAIL_INSET); } elseif ($method == 'adaptiveResize') { $img = $img->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND); } elseif ($method == 'adaptiveResizeFromTop') { $fromWidth = $imgBox->getWidth(); $fromHeight = $imgBox->getHeight(); $toWidth = $box->getWidth(); $toHeight = $box->getHeight(); $fromPercent = $fromWidth / $fromHeight; $toPercent = $toWidth / $toHeight; if ($toPercent >= $fromPercent) { $resizeWidth = $toWidth; $resizeHeight = round($toWidth / $fromWidth * $fromHeight); $img = $img->resize(new Box($resizeWidth, $resizeHeight))->crop(new Point(0, 0), new Box($toWidth, $toHeight)); } else { $img = $img->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND); } } return $img; }
/** * Скропать изображения и вернуть объет для работы с ним. * * Если исходное изображение меньше по размерам - сначала его ресайзит до нужных размеров. * * @param string $tmpFileName Путь к исходному файлу * @param integer $paramWidth Ширина для кропа * @param integer $paramHeight Высота для кропа * * @return ImageInterface */ public static function cropImage($tmpFileName, $paramWidth, $paramHeight) { $newImage = Image::getImagine()->open($tmpFileName); $imageSizes = $newImage->getSize(); $width = $imageSizes->getWidth(); $height = $imageSizes->getHeight(); //Если меньше нужных размеров, то сначала пропорционально ресайзим if ($width < $paramWidth || $height < $paramHeight) { $newHeight = $height; $newWidth = $width; if ($width / $height > $paramWidth / $paramHeight) { $newHeight = $paramHeight; $newWidth = round($newHeight * $width / $height); } elseif ($width / $height <= $paramWidth / $paramHeight) { $newWidth = $paramWidth; $newHeight = round($newWidth * $height / $width); } $newImage->resize(new Box($newWidth, $newHeight))->save($tmpFileName, ['quality' => 80]); } $box = new Box($paramWidth, $paramHeight); if ($newImage->getSize()->getWidth() <= $box->getWidth() && $newImage->getSize()->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { return $newImage->copy(); } $newImage = $newImage->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND); // create empty image to preserve aspect ratio of thumbnail $thumb = Image::getImagine()->create(new Box($newImage->getSize()->getWidth(), $newImage->getSize()->getHeight()), new Color('FFF', 100)); $thumb->paste($newImage, new Point(0, 0)); return $thumb; }
protected function pasteCentered(\Imagine\Image\ImageInterface $image, \Imagine\Image\ImageInterface $original) { $originalSize = $original->getSize(); $x = $this->getPasteValue($this->size->getWidth(), $originalSize->getWidth()); $y = $this->getPasteValue($this->size->getHeight(), $originalSize->getHeight()); $pastePoint = new \Imagine\Image\Point($x, $y); $image->paste($original, $pastePoint); return $image; }
/** * Calculate the box variables by the given size and center * * @param \Imagine\Image\Box $size * @param Point $center * @return array */ public static function calculateBox(Box $size, Point $center) { $max_height_count = ceil($size->getHeight() / self::TILE_SIZE); $max_width_count = ceil($size->getWidth() / self::TILE_SIZE); $tile_height_start = floor($center->getY() / self::TILE_SIZE) - floor($max_height_count / 2); $tile_width_start = floor($center->getX() / self::TILE_SIZE) - floor($max_width_count / 2); $tile_height_stop = $tile_height_start + $max_height_count + 2; $tile_width_stop = $tile_width_start + $max_width_count + 2; $upper_y = $center->getY() - floor($size->getHeight() / 2) - $tile_height_start * self::TILE_SIZE; $upper_x = $center->getX() - floor($size->getWidth() / 2) - $tile_width_start * self::TILE_SIZE; return array('tiles' => ['start' => new Point($tile_width_start, $tile_height_start), 'stop' => new Point($tile_width_stop, $tile_height_stop)], 'crop' => new Point(round($upper_x + self::TILE_SIZE), round($upper_y + self::TILE_SIZE)), 'base' => new Box(($max_width_count + 2) * self::TILE_SIZE, ($max_height_count + 2) * self::TILE_SIZE)); }
/** * Creates a centered crop thumbnail * @param string $filename the image file path or path alias. * @param integer $targetWidth the width in pixels * @param integer $height the height in pixels * @return ImageInterface */ public static function cropCenter($filename, $targetWidth, $targetHeight) { // Box is Imagine Box instance // Point is Imagine Point instance $target = new Box($targetWidth, $targetHeight); $targetWidth = $target->getWidth(); $targetHeight = $target->getHeight(); $originalImage = self::getImagine()->open(Yii::getAlias($filename)); $orgSize = $originalImage->getSize(); $orgWidth = $orgSize->getWidth(); $orgHeight = $orgSize->getHeight(); if ($orgWidth > $orgHeight) { // Landscaped.. We need to crop image by horizontally $w = $orgWidth * ($targetHeight / $orgHeight); $h = $targetHeight; $cropBy = new Point(max($w - $targetWidth, 0) / 2, 0); } else { // Portrait.. $w = $targetWidth; // Use target box's width and crop vertically $h = $orgHeight * ($targetWidth / $orgWidth); $cropBy = new Point(0, max($h - $targetHeight, 0) / 2); } $tempBox = new Box($w, $h); $img = $originalImage->thumbnail($tempBox, 'outbound'); // Here is the magic.. return $img->crop($cropBy, $target); // Return "ready to save" final image instance }
/** * 设置缩略大小 * @param array $size */ function setThumbBox(array $size) { $this->thumbBox = new Box($size[0], $size[1]); //生成文件目录 $this->savePath = $this->dst . '/' . $this->thumbBox->getWidth() . ' x ' . $this->thumbBox->getHeight(); $this->filesystem->mkdir($this->savePath); }
public static function pad(\Imagine\Gd\Image $img, \Imagine\Image\Box $size, $fcolor = [255, 255, 255], $ftransparency = 0) { $tsize = $img->getSize(); $x = $y = 0; if ($size->getWidth() > $tsize->getWidth()) { $x = round(($size->getWidth() - $tsize->getWidth()) / 2); } if ($size->getHeight() > $tsize->getHeight()) { $y = round(($size->getHeight() - $tsize->getHeight()) / 2); } $pasteto = new \Imagine\Image\Point($x, $y); $imagine = new \Imagine\Gd\Imagine(); $palette = new \Imagine\Image\Palette\RGB(); $color = new \Imagine\Image\Palette\Color\RGB($palette, $fcolor, $ftransparency); $image = $imagine->create($size, $color); $image->paste($img, $pasteto); return $image; }
/** * @dataProvider getBoxSettings */ public function testGetBox($settings, Box $mediaSize, Box $expected) { $adapter = $this->getMock('Imagine\\Image\\ImagineInterface'); $media = $this->getMock('Sonata\\MediaBundle\\Model\\MediaInterface'); $media->expects($this->once())->method('getBox')->will($this->returnValue($mediaSize)); $resizer = new SquareResizer($adapter, 'foo'); $box = $resizer->getBox($media, $settings); $this->assertInstanceOf('Imagine\\Image\\Box', $box); $this->assertEquals($expected->getWidth(), $box->getWidth()); $this->assertEquals($expected->getHeight(), $box->getHeight()); }
/** * @dataProvider getBoxSettings */ public function testGetBox($settings, Box $mediaSize, Box $expected, $mode = ImageInterface::THUMBNAIL_OUTBOUND) { $adapter = $this->getMock('Imagine\\Image\\ImagineInterface'); $media = $this->getMock('Sonata\\MediaBundle\\Model\\MediaInterface'); $media->expects($this->once())->method('getBox')->will($this->returnValue($mediaSize)); $metadata = $this->getMock('Sonata\\MediaBundle\\Metadata\\MetadataBuilderInterface'); $resizer = new ScaleCropResizer($adapter, $mode, $metadata); $box = $resizer->getBox($media, $settings); $this->assertInstanceOf('Imagine\\Image\\Box', $box); $this->assertEquals($expected->getWidth(), $box->getWidth()); $this->assertEquals($expected->getHeight(), $box->getHeight()); }
/** * @dataProvider getBoxSettings */ public function testGetBox($mode, $settings, Box $mediaSize, Box $result) { $adapter = $this->getMock('Imagine\\Image\\ImagineInterface'); $media = $this->getMock('Sonata\\MediaBundle\\Model\\MediaInterface'); $media->expects($this->exactly(2))->method('getBox')->will($this->returnValue($mediaSize)); $metadata = $this->getMock('Sonata\\MediaBundle\\Metadata\\MetadataBuilderInterface'); $resizer = new SimpleResizer($adapter, $mode, $metadata); $box = $resizer->getBox($media, $settings); $this->assertInstanceOf('Imagine\\Image\\Box', $box); $this->assertEquals($result->getWidth(), $box->getWidth()); $this->assertEquals($result->getHeight(), $box->getHeight()); }
/** * @throws \Imagine\Exception\InvalidArgumentException * * @param HealthCareAbroad\MediaBundle\Entity\Media $media * @param array $settings * * @return \Imagine\Image\Box */ private function computeBox(Media $media, array $settings) { if ($this->mode !== ImageInterface::THUMBNAIL_INSET && $this->mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $size = new Box($settings['width'], $settings['height']); $ratios = array($settings['width'] / $size->getWidth(), $settings['height'] / $size->getHeight()); if ($this->mode === ImageInterface::THUMBNAIL_INSET) { $ratio = min($ratios); } else { $ratio = max($ratios); } return $size->scale($ratio); }
/** * * */ public function resize($source, $target, $width, $height, $crop = 'center') { $image = $this->imagine->open($source); $box = new Box($width, $height); $srcBox = $image->getSize(); $heightBasedWidth = $srcBox->getWidth() * ($box->getHeight() / $srcBox->getHeight()); if ($srcBox->getWidth() > $srcBox->getHeight() && $heightBasedWidth >= $width) { $width = ceil($heightBasedWidth); $height = $box->getHeight(); $cropPoint = new Point(max($width - $box->getWidth(), 0) / 2, 0); } else { $width = $box->getWidth(); $height = ceil($srcBox->getHeight() * ($box->getWidth() / $srcBox->getWidth())); if ($crop == 'top') { $cropPoint = new Point(0, 0); } if ($crop == 'center') { $cropPoint = new Point(0, max($height - $box->getHeight(), 0) / 2); } } $tempBox = new Box($width, $height); $image = $image->thumbnail($tempBox, ImageInterface::THUMBNAIL_INSET); $image->crop($cropPoint, $box)->save($target); }
/** * {@inheritdoc} */ public function thumbnail(BoxInterface $size, $mode = ImageInterface::THUMBNAIL_INSET, $filter = ImageInterface::FILTER_UNDEFINED) { if ($mode !== ImageInterface::THUMBNAIL_INSET && $mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $imageSize = $this->getSize(); $ratios = array($size->getWidth() / $imageSize->getWidth(), $size->getHeight() / $imageSize->getHeight()); $thumbnail = $this->copy(); $thumbnail->usePalette($this->palette()); $thumbnail->strip(); // if target width is larger than image width // AND target height is longer than image height if ($size->contains($imageSize)) { return $thumbnail; } if ($mode === ImageInterface::THUMBNAIL_INSET) { $ratio = min($ratios); } else { $ratio = max($ratios); } if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) { if (!$imageSize->contains($size)) { $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight())); } else { $imageSize = $thumbnail->getSize()->scale($ratio); $thumbnail->resize($imageSize, $filter); } $thumbnail->crop(new Point(max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))), $size); } else { if (!$imageSize->contains($size)) { $imageSize = $imageSize->scale($ratio); $thumbnail->resize($imageSize, $filter); } else { $imageSize = $thumbnail->getSize()->scale($ratio); $thumbnail->resize($imageSize, $filter); } } return $thumbnail; }
/** * Calculate the position of the blip on the map image. * * @param Point $center Point on the image. * @param Box $size Size of the image. * @param integer $zoom Zoom level of the map. * * @return Point */ public function calculatePosition(Point $center, Box $size, $zoom) { $topLeft = new Point($center->getX() - $size->getWidth() / 2, $center->getY() - $size->getHeight() / 2); $blipPoint = Geo::calculatePoint($this->latLng, $zoom); return new Point($blipPoint->getX() - $topLeft->getX() - $this->imageSize[0] / 2, $blipPoint->getY() - $topLeft->getY() - $this->imageSize[1] / 2); }
/** * @covers Imagine\Image\Box::getWidth * @covers Imagine\Image\Box::getHeight * * @dataProvider getSizes * * @param integer $width * @param integer $height */ public function testShouldAssignWidthAndHeight($width, $height) { $size = new Box($width, $height); $this->assertEquals($width, $size->getWidth()); $this->assertEquals($height, $size->getHeight()); }
/** * 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; }
/** * {@inheritdoc} */ public function thumbnail(BoxInterface $size, $mode = ImageInterface::THUMBNAIL_INSET) { if ($mode !== ImageInterface::THUMBNAIL_INSET && $mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $imageSize = $this->getSize(); $thumbnail = $this->copy(); // if target width is larger than image width // AND target height is longer than image height if ($size->contains($imageSize)) { return $thumbnail; } // if target width is larger than image width // OR target height is longer than image height if (!$imageSize->contains($size)) { $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight())); } try { if ($mode === ImageInterface::THUMBNAIL_INSET) { $thumbnail->gmagick->thumbnailimage($size->getWidth(), $size->getHeight(), true); } elseif ($mode === ImageInterface::THUMBNAIL_OUTBOUND) { $thumbnail->gmagick->cropthumbnailimage($size->getWidth(), $size->getHeight()); } } catch (\GmagickException $e) { throw new RuntimeException('Thumbnail operation failed', $e->getCode(), $e); } return $thumbnail; }
public function processImage($input, $output, $options = array()) { if ($this->debug) { $startTime = microtime(true); } if (!is_readable($input)) { $this->debugmessages[] = 'File not ' . (file_exists($input) ? 'readable' : 'found') . ": {$input} *** Skipping ***"; return false; } $this->width = $this->height = null; if (is_string($options)) { $options = parse_str($options); } // convert an options string to an array if needed $inputParams = array('options' => $options); $outputType = pathinfo($output, PATHINFO_EXTENSION); $outputIsJpg = strncasecmp('jp', $outputType, 2) === 0; try { /* initial dimensions */ $image = $this->imagine->open($input); $size = $image->getSize(); $origWidth = $inputParams['width'] = $size->getWidth(); $origHeight = $inputParams['height'] = $size->getHeight(); if (self::$maxsize && $origWidth * $origHeight > self::$maxsize) { // if we're using GD we need to check the image will fit in memory $this->debugmessages[] = "GD: {$input} may exceed available memory ** Skipping **"; return false; } /* source crop (start) */ if (isset($options['sw']) || isset($options['sh'])) { if (empty($options['sw']) || $options['sw'] > $origWidth) { $newWidth = $origWidth; } else { $newWidth = $options['sw'] < 1 ? round($origWidth * $options['sw']) : $options['sw']; // sw < 1 is a %, >= 1 in px } if (empty($options['sh']) || $options['sh'] > $origHeight) { $newHeight = $origHeight; } else { $newHeight = $options['sh'] < 1 ? round($origHeight * $options['sh']) : $options['sh']; } if ($newWidth !== $origWidth || $newHeight !== $origHeight) { // only if something will actually be cropped if (empty($options['sx'])) { $cropStartX = isset($options['sx']) ? $options['sx'] : (int) (($origWidth - $newWidth) / 2); // 0 or center } else { $cropStartX = $options['sx'] < 1 ? round($origWidth * $options['sx']) : $options['sx']; if ($cropStartX + $newWidth > $origWidth) { $cropStartX = $origWidth - $newWidth; } // crop box can't go past the right edge } if (empty($options['sy'])) { $cropStartY = isset($options['sy']) ? $options['sy'] : (int) (($origHeight - $newHeight) / 2); } else { $cropStartY = $options['sy'] < 1 ? round($origHeight * $options['sy']) : $options['sy']; if ($cropStartY + $newHeight > $origHeight) { $cropStartY = $origHeight - $newHeight; } } $scBox = new Box($newWidth, $newHeight); $origWidth = $newWidth; // update input dimensions to the new cropped size $origHeight = $newHeight; } } $origAR = $origWidth / $origHeight; // original image aspect ratio $origAspect = round($origAR, 2); /* input dimensions */ // use width/height if specified if (isset($options['w'])) { $width = $options['w']; } if (isset($options['h'])) { $height = $options['h']; } // override with any orientation-specific dimensions if ($origAspect > 1) { // landscape if (isset($options['wl'])) { $width = $options['wl']; } if (isset($options['hl'])) { $height = $options['hl']; } } elseif ($origAspect < 1) { // portrait if (isset($options['wp'])) { $width = $options['wp']; } if (isset($options['hp'])) { $height = $options['hp']; } } else { // square if (isset($options['ws'])) { $width = $options['ws']; } if (isset($options['hs'])) { $height = $options['hs']; } } // fill in a missing dimension $bothDims = true; if (empty($width)) { if (empty($height)) { $height = $origHeight; $width = $origWidth; } else { $width = $height * $origAR; } $bothDims = false; } if (empty($height)) { $height = $width / $origAR; $bothDims = false; } $newAR = $width / $height; /* scale */ if (empty($options['scale'])) { $requestedMP = $width * $height; // we'll need this for quality scaling } else { $requestedMP = $width * $options['scale'] * $height * $options['scale']; if (empty($options['aoe'])) { // if aoe is off, cap scale so image isn't enlarged $hScale = $origHeight / $height; $wScale = $origWidth / $width; $wRequested = $width * $options['scale']; $hRequested = $height * $options['scale']; $options['scale'] = $hScale > 1 && $wScale > 1 ? min($hScale, $wScale, $options['scale']) : 1; } $options['w'] = $width *= $options['scale']; $options['h'] = $height *= $options['scale']; } if (empty($options['zc']) || !$bothDims) { /* non-zc sizing */ $newAspect = round($newAR, 2); // ignore small differences if ($newAspect < $origAspect) { // Make sure AR doesn't change. Smaller dimension... if ($origWidth < $options['w'] && empty($options['aoe'])) { $options['w'] = $width = $origWidth; $options['h'] = $width / $newAR; } $height = $width / $origAR; } elseif ($newAspect > $origAspect) { // ...limits larger if ($origHeight < $options['h'] && empty($options['aoe'])) { $options['h'] = $height = $origHeight; $options['w'] = $height * $newAR; } $width = $height * $origAR; } $width = round($width); // clean up $height = round($height); /* far */ if (!empty($options['far']) && $bothDims) { $options['w'] = round($options['w']); $options['h'] = round($options['h']); if ($options['w'] > $width || $options['h'] > $height) { $farPoint = Reductionist::startPoint($options['far'], array($options['w'], $options['h']), array($width, $height)); $farBox = new Box($options['w'], $options['h']); $this->width = $options['w']; $this->height = $options['h']; } } } else { /* zc */ if (empty($options['aoe'])) { // if the crop box is bigger than the original image, scale it down if ($width > $origWidth) { $height = $origWidth / $newAR; $width = $origWidth; } if ($height > $origHeight) { $width = $origHeight * $newAR; $height = $origHeight; } } // make sure final image will cover the crop box $width = round($width); $height = round($height); $newWidth = round($height * $origAR); $newHeight = round($width / $origAR); if ($newWidth > $width) { // needs horizontal cropping $newHeight = $height; } elseif ($newHeight > $height) { // needs vertical cropping $newWidth = $width; } else { // no cropping needed, same AR $newWidth = $width; $newHeight = $height; } $cropStart = Reductionist::startPoint($options['zc'], array($newWidth, $newHeight), array($width, $height)); $cropBox = new Box($width, $height); $this->width = $width; $this->height = $height; $width = $newWidth; $height = $newHeight; } /* source crop (finish) */ if (isset($scBox)) { $scale = max($width / $origWidth, $height / $origHeight); if ($scale <= 0.5 && $this->gLib && $image->getFormat() === IMG_JPG) { $image->resize(new Box(round($inputParams['width'] * $scale), round($inputParams['height'] * $scale))); $scStart = new \Imagine\Image\Point(round($cropStartX * $scale), round($cropStartY * $scale)); $scBox = new Box(round($scBox->getWidth() * $scale), round($scBox->getHeight() * $scale)); } else { $scStart = new \Imagine\Image\Point($cropStartX, $cropStartY); } $image->crop($scStart, $scBox); if (abs($scBox->getWidth() - $width) == 1) { // snap a 1px rounding error to the source crop box $this->width = $width = $scBox->getWidth(); } if (abs($scBox->getHeight() - $height) == 1) { $this->height = $height = $scBox->getHeight(); } } /* resize, aoe */ if ($didScale = $width < $origWidth && $height < $origHeight || !empty($options['aoe'])) { $imgBox = new Box($width, $height); $image->resize($imgBox); } elseif (isset($options['qmax']) && empty($options['aoe']) && isset($options['q']) && $outputIsJpg) { // undersized input image. We'll increase q towards qmax depending on how much it's undersized $sizeRatio = $requestedMP / (isset($cropBox) ? $this->width * $this->height : $width * $height); if ($sizeRatio >= 2) { $options['q'] = $options['qmax']; } elseif ($sizeRatio > 1) { $options['q'] += round(($options['qmax'] - $options['q']) * ($sizeRatio - 1)); } } /* crop */ if (isset($cropBox)) { $image->crop($cropStart, $cropBox); } /* filters (start) */ if (!empty($options['fltr'])) { if (!is_array($options['fltr'])) { $options['fltr'] = array($options['fltr']); // in case somebody did fltr= instead of fltr[]= } $transformation = new \Imagine\Filter\Transformation($this->imagine); $filterlog = array($this->debug); foreach ($options['fltr'] as $fltr) { $filter = explode('|', $fltr); if ($filter[0] === 'usm') { // right now only unsharp mask is implemented, sort of $image->effects()->sharpen(); // radius, amount and threshold are ignored! } elseif ($filter[0] === 'wmt' || $filter[0] === 'wmi') { $doApply = true; $transformation->add(new Filter\Watermark($filter, $filterlog)); } } } /* bg */ if ($hasBG = isset($options['bg']) && !$outputIsJpg || isset($farBox)) { if (self::$palette === null) { self::$palette = new \Imagine\Image\Palette\RGB(); } if (isset($options['bg'])) { $bgColor = explode('/', $options['bg']); $bgColor[1] = isset($bgColor[1]) ? $bgColor[1] : 100; } else { $bgColor = array(array(255, 255, 255), 100); } $backgroundColor = self::$palette->color($bgColor[0], 100 - $bgColor[1]); if (isset($cropBox)) { $bgBox = $cropBox; } elseif (isset($farBox)) { $bgBox = $farBox; } elseif (isset($imgBox)) { $bgBox = $imgBox; } else { $bgBox = new Box($width, $height); } $image = $this->imagine->create($bgBox, self::$palette->color($bgColor[0], 100 - $bgColor[1]))->paste($this->gLib ? $image->getImage() : $image, isset($farPoint) ? $farPoint : new \Imagine\Image\Point(0, 0)); } /* filters (finish) */ if (isset($transformation) && !empty($doApply)) { // apply any filters try { $transformation->apply($image); } catch (\Exception $e) { $this->debugmessages[] = $e->getMessage(); } } /* debug info */ if ($this->debug) { $debugTime = microtime(true); $this->debugmessages[] = 'Input options:' . self::formatDebugArray($inputParams['options']); // print all options, stripping off array() $changed = array(); // note any options which may have been changed during processing foreach (array('w', 'h', 'scale', 'q') as $opt) { if (isset($inputParams['options'][$opt]) && $inputParams['options'][$opt] != $options[$opt]) { $changed[$opt] = $options[$opt]; } } if ($changed) { $this->debugmessages[] = 'Modified options:' . self::formatDebugArray($changed, true); } $this->debugmessages[] = "Original - w: {$inputParams['width']} | h: {$inputParams['height']} " . sprintf("(%2.2f MP)", $inputParams['width'] * $inputParams['height'] / 1000000.0); if (isset($image->prescalesize)) { $this->debugmessages[] = "JPEG prescale - w: {$image->prescalesize[0]} | h: {$image->prescalesize[1]} " . sprintf("(%2.2f MP)", $image->prescalesize[0] * $image->prescalesize[1] / 1000000.0); } if (isset($scBox)) { $this->debugmessages[] = "Source area - start: {$scStart} | box: {$scBox}"; } if (isset($wRequested)) { $this->debugmessages[] = "Requested - w: " . round($wRequested) . ' | h: ' . round($hRequested); } if (!isset($wRequested) || !$didScale) { $this->debugmessages[] = "New - w: {$width} | h: {$height}" . ($didScale ? '' : ' [Not scaled: same size or insufficient input resolution]'); } if (isset($farPoint)) { $this->debugmessages[] = "FAR - start: {$farPoint} | box: {$options['w']}x{$options['h']} px"; } if (isset($cropBox)) { $this->debugmessages[] = "ZC - start: {$cropStart} | box: {$cropBox}"; } if ($hasBG) { $this->debugmessages[] = "Background color: {$bgColor[0]} | opacity: {$bgColor[1]}"; } $debugTime = microtime(true) - $debugTime; } if (isset($filterlog[1])) { // add any filter debug output unset($filterlog[0]); $this->debugmessages = array_merge($this->debugmessages, $filterlog); } /* save */ $outputOpts = array('jpeg_quality' => empty($options['q']) ? $this->defaultQuality : (int) $options['q'], 'format' => empty($options['f']) ? $outputType : $options['f']); $image->save($output, $outputOpts); if (!$this->width) { $this->width = $width; } if (!$this->height) { $this->height = $height; } } catch (\Imagine\Exception\Exception $e) { $this->debugmessages[] = "Input file: {$input}"; $this->debugmessages[] = 'Input options: ' . self::formatDebugArray($inputParams['options']); $this->debugmessages[] = "*** Error *** {$e->getMessage()}"; return false; } /* debug info (timing) */ if ($this->debug) { $this->debugmessages[] = "Wrote {$output}"; $this->debugmessages[] = "Dimensions: {$this->width}x{$this->height} px"; $this->debugmessages[] = 'Execution time: ' . round((microtime(true) - $startTime - $debugTime) * 1000.0) . ' ms'; } return true; }
/** * Creates a thumbnail image. * * The function differs from `\Imagine\Image\ImageInterface::thumbnail()` function that * it keeps the aspect ratio of the image. * * @param string|resource|ImageInterface $pathOrResource the image file path or path alias. * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail * @param string $mode * @return ImageInterface */ public static function thumbnail($pathOrResource, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND) { $box = new Box($width, $height); if (!$pathOrResource instanceof ImageInterface) { /** @var ImageInterface $img */ $img = is_resource($pathOrResource) ? static::getImagine()->read($pathOrResource) : static::getImagine()->open(Alias::getAlias($pathOrResource)); } else { $img = $pathOrResource; } if ($img->getSize()->getWidth() <= $box->getWidth() && $img->getSize()->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { return $img->copy(); } $img = $img->thumbnail($box, $mode); // create empty image to preserve aspect ratio of thumbnail $thumb = static::getImagine()->create($box); // calculate points $size = $img->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $width) { $startX = ceil($width - $size->getWidth()) / 2; } if ($size->getHeight() < $height) { $startY = ceil($height - $size->getHeight()) / 2; } $thumb->paste($img, new Point($startX, $startY)); return $thumb; }
/** * Attempts to find the best way to crop. * Takes into account the image being a portrait or landscape. * * @param Imagine\Image\Box $size - The image's current size. * @param string $width - The image's new width. * @param string $height - The image's new height. * @return array */ protected function getOptimalCrop($size, $width, $height) { $heightRatio = $size->getHeight() / $height; $widthRatio = $size->getWidth() / $width; if ($heightRatio < $widthRatio) { $optimalRatio = $heightRatio; } else { $optimalRatio = $widthRatio; } $optimalHeight = round($size->getHeight() / $optimalRatio, 2); $optimalWidth = round($size->getWidth() / $optimalRatio, 2); return [$optimalWidth, $optimalHeight]; }
public function logoStripe($destination, $dest_width = null, $dest_height = null, $quality = 90, $vertical = true) { if (!$dest_width and !$dest_height) { throw new Exception("\$dest_width or \$dest_height is required.\n"); } $image = $this->imagine->open($this->file); // original size $srcBox = $image->getSize(); !$dest_width and $dest_height and $dest_width = $this->autoWidth($dest_height); !$dest_height and $dest_width and $dest_height = $this->autoHeight($dest_width); $box = new Box($dest_width, $dest_height); $new_filename = pathinfo($destination, PATHINFO_FILENAME); $new_extention = pathinfo($destination, PATHINFO_EXTENSION); $filename = $this->file->getFileName(); // extension exists - change destination filename if ($new_extention and $new_filename) { $filename = $new_filename . '.' . $new_extention; // remove filename from destination $destination = substr($destination, 0, -strlen($filename)); } $destination = rtrim($destination, '/') . '/'; if ($srcBox->getWidth() < $dest_width and $srcBox->getHeight() < $dest_height) { $dest = FCPATH . $destination . $this->file->getFileName(); // only if paths are different // or if we have a watermark, and want to overwrite original photo... if ($dest != $this->full_path) { // we use this, since we want to preserve watermark, quality. Can't simply copy() $image->save($destination . $filename, array('quality' => $quality)); @chmod($dest, 0666); } } else { //we scale on the smaller dimension if ($srcBox->getWidth() > $srcBox->getHeight()) { $width = $srcBox->getWidth() * ($box->getHeight() / $srcBox->getHeight()); $height = $box->getHeight(); //we center the crop in relation to the width $cropPoint = new Point(max($width - $box->getWidth(), 0) / 2, 0); } else { $width = $box->getWidth(); $height = $srcBox->getHeight() * ($box->getWidth() / $srcBox->getWidth()); //we center the crop in relation to the height $cropPoint = new Point(0, max($height - $box->getHeight(), 0) / 2); } $image = $image->thumbnail($box, \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND); $image->save($destination . $filename, array('quality' => $quality)); } $image_gray = $this->imagine->open($destination . $filename); $image_gray->effects()->grayscale(); if ($vertical) { $box_stripe = new Box($dest_width, $dest_height * 2); $offset_x = 0; $offset_y = $dest_height; } else { $box_stripe = new Box($dest_width * 2, $dest_height); $offset_x = $dest_width; $offset_y = 0; } $container = $this->imagine->create($box_stripe); $container->paste($image, new Point(0, 0)); // original image $container->paste($image_gray, new Point($offset_x, $offset_y)); // grayscale image $container->save($destination . $filename, array('quality' => $quality)); @chmod($destination . $filename, 0666); return $this->file->getFileName(); }
function pad(\Imagine\Gd\Imagine $img, \Imagine\Image\Box $size, $fcolor = '#000', $ftransparency = 100) { $tsize = $img->getSize(); $x = $y = 0; if ($size->getWidth() > $tsize->getWidth()) { $x = round(($size->getWidth() - $tsize->getWidth()) / 2); } elseif ($size->getHeight() > $tsize->getHeight()) { $y = round(($size->getHeight() - $tsize->getHeight()) / 2); } $pasteto = new \Imagine\Image\Point($x, $y); $imagine = new \Imagine\Gd\Imagine(); $color = new \Imagine\Image\Color($fcolor, $ftransparency); $image = $imagine->create($size, $color); $image->paste($img, $pasteto); return $image; }
/** * Create a thumbnail by scaling an image and cropping it to fit the exact dimensions * * @param ImageInterface $image The ImageInterface instance from Imagine * @param int $targetWidth The width in pixels * @param int $targetHeight The height in pixels * @return ImageInterface */ protected function thumbnailCropScale(ImageInterface $image, $targetWidth, $targetHeight) { $target = new Box($targetWidth, $targetHeight); $sourceSize = $image->getSize(); if ($sourceSize->getWidth() > $sourceSize->getHeight()) { $width = $sourceSize->getWidth() * ($target->getHeight() / $sourceSize->getHeight()); $height = $targetHeight; $cropPoint = new Point((int) (max($width - $target->getWidth(), 0) / 2), 0); } else { $height = $sourceSize->getHeight() * ($target->getWidth() / $sourceSize->getWidth()); $width = $targetWidth; $cropPoint = new Point(0, (int) (max($height - $target->getHeight(), 0) / 2)); } $box = new Box($width, $height); return $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND)->crop($cropPoint, $target); }
/** * Creates a thumbnail image. The function differs from `\Imagine\Image\ImageInterface::thumbnail()` function that * it keeps the aspect ratio of the image. * @param string $filename the image file path or path alias. * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail * @param string $mode * @return ImageInterface */ public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND) { $box = new Box($width, $height); $img = static::getImagine()->open(Yii::getAlias($filename)); if ($img->getSize()->getWidth() <= $box->getWidth() && $img->getSize()->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { return $img->copy(); } $img = $img->thumbnail($box, $mode); // create empty image to preserve aspect ratio of thumbnail //$thumb = static::getImagine()->create($box, new Color('FFF', 100)); $palette = new \Imagine\Image\Palette\RGB(); $thumb = static::getImagine()->create($box, $palette->color('#FFFFFF', 100)); // calculate points $size = $img->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $width) { $startX = ceil($width - $size->getWidth()) / 2; } if ($size->getHeight() < $height) { $startY = ceil($height - $size->getHeight()) / 2; } $thumb->paste($img, new Point($startX, $startY)); return $thumb; }
protected function deliverImage($file) { // operation parsen uit url $operations = array(); $params = $this->router->getParams(); if (isset($params['operations'])) { $matches = preg_split('#-/#', $params['operations']); foreach ($matches as $match) { $options = explode('/', trim($match, '/')); $operations[array_shift($options)] = $options; } } if (!empty($params['ext'])) { if ($params['ext'] == 'json') { $operations['exif'] = true; } else { $operations['format'] = array($params['ext']); } } // afbeelding serveren if (empty($operations)) { $this->deliverOther($file); } else { // aan de hand van opgegeven operaties afbeelding manipulaties uitvoeren try { $imagine = new Imagine(); $image = $imagine->open($file->getPath()); // kwaliteit $quality = isset($operations['quality']) ? $operations['quality'] : 100; // formaat (mime type) $format = isset($operations['format'][0]) ? $operations['format'][0] : null; if ($format === null) { switch ($file->mime_type) { default: case 'image/bmp': case 'image/jpg': case 'image/jpeg': case 'image/tiff': $format = 'jpg'; break; case 'image/png': $format = 'png'; break; case 'image/ico': case 'image/gif': $format = 'gif'; break; } } // schalen tot minimale afmetingen if (isset($operations['scale_crop'])) { $value = $operations['scale_crop'][0]; if (strpos($value, 'x') !== false) { list($width, $height) = explode('x', $value); } else { $width = $height = $value; } $size = $image->getSize(); if ($size->getHeight() > $size->getWidth()) { $height = $size->getHeight() * ($width / $size->getWidth()); } else { if ($size->getHeight() < $size->getWidth()) { $width = $size->getWidth() * ($height / $size->getHeight()); } } $operations['resize'] = array("{$width}x{$height}"); $operations['crop'] = $operations['scale_crop']; } // afmetingen wijzigen if (isset($operations['resize'])) { $value = $operations['resize'][0]; $width = $value; $height = null; if (strpos($value, 'x') !== false) { list($width, $height) = explode('x', $value); } $size = $image->getSize(); if (empty($width) && !empty($height)) { $size = $size->heighten($height); } else { if (!empty($width) && empty($height)) { $size = $size->widen($width); } else { $size = new Box($width, $height); } } $image->resize($size); } // bijsnijden if (isset($operations['crop'])) { $size = $image->getSize(); $value = $operations['crop'][0]; if (strpos($value, 'x') !== false) { list($width, $height) = explode('x', $value); } else { $width = $height = $value; } $size = $image->getSize(); if (empty($width) && !empty($height)) { $size = $size->heighten($height); } else { if (!empty($width) && empty($height)) { $size = $size->widen($width); } else { $size = new Box($width, $height); } } if (empty($operations['crop'][1])) { $operations['crop'][1] = 'center'; } if ($operations['crop'][1] == 'center') { $x = ($image->getSize()->getWidth() - $size->getWidth()) / 2; $y = ($image->getSize()->getHeight() - $size->getHeight()) / 2; } else { list($x, $y) = explode(',', $operations['crop'][1]); } $point = new Point($x, $y); $image->crop($point, $size); } // gamma instellen if (isset($operations['gamma'])) { $value = $operations['gamma'][0]; $image->effects()->gamma($value); } // grijs kleuren if (isset($operations['greyscale'])) { $image->effects()->grayscale(); } // kleuren if (isset($operations['colorize'])) { $value = $operations['colorize'][0]; $value = $image->palette()->color($value); $image->effects()->colorize($value); } // vervagen if (isset($operations['blur'])) { $value = $operations['blur'][0]; $image->effects()->blur($value); } // schalen if (isset($operations['scale'])) { $value = $operations['scale'][0]; $size = $image->getSize(); $size = $size->scale($value); $image->resize($size); } // roteren if (isset($operations['rotate'])) { $value = $operations['rotate'][0]; $image->rotate($value); } // websafe kleuren if (isset($operations['web'])) { $transformation = new WebOptimization(); $image = $transformation->apply($image); } // toon exif data if (isset($operations['exif'])) { $this->response->setContentType('application/json'); $this->response->setJsonContent(exif_read_data($file->getPath())); $this->response->send(); } else { $image->show($format, array('quality' => $quality)); } } catch (\Exception $e) { $this->deliverOther($file); } } }
/** * Create thumbnail * * @param $image \Imagine\Imagick\Image * @param $options * @param string $filter * @return ImageInterface * @throws InvalidArgumentException */ private function thumbnail($image, $options, $filter = ImageInterface::FILTER_UNDEFINED) { $width = $options['box'][0]; $height = $options['box'][1]; $size = $box = new Box($width, $height); $mode = $options['mode']; if ($mode !== ImageInterface::THUMBNAIL_INSET && $mode !== ImageInterface::THUMBNAIL_OUTBOUND) { throw new InvalidArgumentException('Invalid mode specified'); } $imageSize = $image->getSize(); $ratios = array($size->getWidth() / $imageSize->getWidth(), $size->getHeight() / $imageSize->getHeight()); $image->strip(); // if target width is larger than image width // AND target height is longer than image height if (!$size->contains($imageSize)) { if ($mode === ImageInterface::THUMBNAIL_INSET) { $ratio = min($ratios); } else { $ratio = max($ratios); } if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) { if (!$imageSize->contains($size)) { $size = new Box(min($imageSize->getWidth(), $size->getWidth()), min($imageSize->getHeight(), $size->getHeight())); } else { $imageSize = $image->getSize()->scale($ratio); $image->resize($imageSize, $filter); } if ($ratios[0] > $ratios[1]) { $cropPoint = new Point(0, 0); } else { $cropPoint = new Point(max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))); } $image->crop($cropPoint, $size); } else { if (!$imageSize->contains($size)) { $imageSize = $imageSize->scale($ratio); $image->resize($imageSize, $filter); } else { $imageSize = $image->getSize()->scale($ratio); $image->resize($imageSize, $filter); } } } // create empty image to preserve aspect ratio of thumbnail $palette = new RGB(); $color = $palette->color('#000', 0); //transparent png with imagick $thumb = Image::getImagine()->create($box, $color); // calculate points $size = $image->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $width) { $startX = ceil($width - $size->getWidth()) / 2; } if ($size->getHeight() < $height) { $startY = ceil($height - $size->getHeight()) / 2; } $thumb->paste($image, new Point($startX, $startY)); return $thumb; }
/** * Gets crop point * * @param \Imagine\Image\Box $resizeSize * @param \Imagine\Image\Box $cropSize * @param $transform * @return \Imagine\Image\Point */ private function _getCropPoint($resizeSize, $cropSize, $transform) { // find the "area of opportunity", the difference between the resized image size and the crop size $wDiff = $resizeSize->getWidth() - $cropSize->getWidth(); $hDiff = $resizeSize->getHeight() - $cropSize->getHeight(); // get default crop position from the settings $position = $this->getSetting('position', $transform); // get the offsets, left and top, now as an int, representing the % offset list($leftOffset, $topOffset) = explode(' ', $position); // calculate and return the point return new \Imagine\Image\Point(floor($wDiff * ($leftOffset / 100)), floor($hDiff * ($topOffset / 100))); }
/** * @param FlowResource $originalResource * @param array $adjustments * @return array resource, width, height as keys * @throws ImageFileException * @throws InvalidConfigurationException * @throws \TYPO3\Flow\Resource\Exception */ public function processImage(FlowResource $originalResource, array $adjustments) { $additionalOptions = array(); $adjustmentsApplied = false; // TODO: Special handling for SVG should be refactored at a later point. if ($originalResource->getMediaType() === 'image/svg+xml') { $originalResourceStream = $originalResource->getStream(); $resource = $this->resourceManager->importResource($originalResourceStream, $originalResource->getCollectionName()); fclose($originalResourceStream); $resource->setFilename($originalResource->getFilename()); return ['width' => null, 'height' => null, 'resource' => $resource]; } $resourceUri = $originalResource->createTemporaryLocalCopy(); $resultingFileExtension = $originalResource->getFileExtension(); $transformedImageTemporaryPathAndFilename = $this->environment->getPathToTemporaryDirectory() . uniqid('ProcessedImage-') . '.' . $resultingFileExtension; if (!file_exists($resourceUri)) { throw new ImageFileException(sprintf('An error occurred while transforming an image: the resource data of the original image does not exist (%s, %s).', $originalResource->getSha1(), $resourceUri), 1374848224); } $imagineImage = $this->imagineService->open($resourceUri); if ($this->imagineService instanceof \Imagine\Imagick\Imagine && $originalResource->getFileExtension() === 'gif' && $this->isAnimatedGif(file_get_contents($resourceUri)) === true) { $imagineImage->layers()->coalesce(); $layers = $imagineImage->layers(); $newLayers = array(); foreach ($layers as $index => $imagineFrame) { $imagineFrame = $this->applyAdjustments($imagineFrame, $adjustments, $adjustmentsApplied); $newLayers[] = $imagineFrame; } $imagineImage = array_shift($newLayers); $layers = $imagineImage->layers(); foreach ($newLayers as $imagineFrame) { $layers->add($imagineFrame); } $additionalOptions['animated'] = true; } else { $imagineImage = $this->applyAdjustments($imagineImage, $adjustments, $adjustmentsApplied); } if ($adjustmentsApplied === true) { $imagineImage->save($transformedImageTemporaryPathAndFilename, $this->getOptionsMergedWithDefaults($additionalOptions)); $imageSize = $imagineImage->getSize(); // TODO: In the future the collectionName of the new resource should be configurable. $resource = $this->resourceManager->importResource($transformedImageTemporaryPathAndFilename, $originalResource->getCollectionName()); if ($resource === false) { throw new ImageFileException('An error occurred while importing a generated image file as a resource.', 1413562208); } unlink($transformedImageTemporaryPathAndFilename); $pathInfo = UnicodeFunctions::pathinfo($originalResource->getFilename()); $resource->setFilename(sprintf('%s-%ux%u.%s', $pathInfo['filename'], $imageSize->getWidth(), $imageSize->getHeight(), $pathInfo['extension'])); } else { $originalResourceStream = $originalResource->getStream(); $resource = $this->resourceManager->importResource($originalResourceStream, $originalResource->getCollectionName()); fclose($originalResourceStream); $resource->setFilename($originalResource->getFilename()); $imageSize = $this->getImageSize($originalResource); $imageSize = new Box($imageSize['width'], $imageSize['height']); } $this->imageSizeCache->set($resource->getCacheEntryIdentifier(), array('width' => $imageSize->getWidth(), 'height' => $imageSize->getHeight())); $result = array('width' => $imageSize->getWidth(), 'height' => $imageSize->getHeight(), 'resource' => $resource); return $result; }