public function resize(\Symforce\AdminBundle\Entity\File $file, $_crop, array $config) { $crop = json_decode($_crop, true); $imagine = $this->getImagine(); $box_size = new \Imagine\Image\Box($config[0][0] / $crop['width'], $config[0][1] / $crop['height']); $crop_point = new \Imagine\Image\Point($box_size->getWidth() * $crop['left'], $box_size->getHeight() * $crop['top']); $crop_size = new \Imagine\Image\Box($config[0][0], $config[0][1]); while ($crop_point->getX() + $crop_size->getWidth() > $box_size->getWidth()) { if ($crop_point->getX() < 1) { break; } $crop_point = new \Imagine\Image\Point($crop_point->getX() - 1, $crop_point->getY()); } while ($crop_point->getY() + $crop_size->getHeight() > $box_size->getHeight()) { if ($crop_point->getY() < 1) { break; } $crop_point = new \Imagine\Image\Point($crop_point->getX(), $crop_point->getY() - 1); } $options = array('quality' => 100); $img = $imagine->load(stream_get_contents($file->getContent())); $img->resize($box_size)->crop($crop_point, $crop_size); $data = $img->get($file->getExt(), $options); $stream = fopen('php://memory', 'r+'); fwrite($stream, $data); rewind($stream); $file->setContent($stream); if ($config[1][0] && $config[1][1]) { $small_box = new \Imagine\Image\Box($config[1][0], $config[1][1]); $img = $imagine->load($data)->resize($small_box); $small_data = $img->get($file->getExt(), $options); $small_stream = fopen('php://memory', 'r+'); fwrite($small_stream, $small_data); rewind($small_stream); $file->setPreview($small_stream); } $file->setUpdated(new \DateTime('now')); }
/** * Apply watermark to image * * @param $imageInstance * @param $effects */ private function _applyWatermark($imageInstance, $watermark) { if (!isset($watermark['image'])) { throw new Exception(Craft::t('Watermark image property not set')); } if (!(isset($watermark['width']) && isset($watermark['width']))) { throw new Exception(Craft::t('Watermark image size is not set')); } $paths = new Imager_ImagePathsModel($watermark['image']); $watermarkInstance = $this->imagineInstance->open($paths->sourcePath . $paths->sourceFilename); $watermarkInstance->resize(new \Imagine\Image\Box($watermark['width'], $watermark['height']), \Imagine\Image\ImageInterface::FILTER_UNDEFINED); if (isset($watermark['position'])) { $position = $watermark['position']; if (isset($position['top'])) { $posY = intval($position['top']); } else { if (isset($position['bottom'])) { $posY = $imageInstance->getSize()->getHeight() - intval($watermark['height']) - intval($position['bottom']); } else { $posY = $imageInstance->getSize()->getHeight() - intval($watermark['height']) - 10; } } if (isset($position['left'])) { $posX = intval($position['left']); } else { if (isset($position['right'])) { $posX = $imageInstance->getSize()->getWidth() - intval($watermark['width']) - intval($position['right']); } else { $posX = $imageInstance->getSize()->getWidth() - intval($watermark['width']) - 10; } } } else { $posY = $imageInstance->getSize()->getHeight() - intval($watermark['height']) - 10; $posX = $imageInstance->getSize()->getWidth() - intval($watermark['width']) - 10; } $positionPoint = new \Imagine\Image\Point($posX, $posY); if ($this->imageDriver == 'imagick') { $watermarkImagick = $watermarkInstance->getImagick(); if (isset($watermark['opacity'])) { $watermarkImagick->evaluateImage(\Imagick::EVALUATE_MULTIPLY, floatval($watermark['opacity']), \Imagick::CHANNEL_ALPHA); } if (isset($watermark['blendMode']) && isset(ImagerService::$compositeKeyTranslate[$watermark['blendMode']])) { $blendMode = ImagerService::$compositeKeyTranslate[$watermark['blendMode']]; } else { $blendMode = \Imagick::COMPOSITE_ATOP; } $imageInstance->getImagick()->compositeImage($watermarkImagick, $blendMode, $positionPoint->getX(), $positionPoint->getY()); } else { // it's GD :( $imageInstance->paste($watermarkInstance, $positionPoint); } }