/** * Crop and resize an image * * @param string $source Path to source image * @param string $destination Path to destination * If not set, will modify the source image * @param array $params An array of cropping/resizing parameters * - INT 'w' represents the width of the new image * With upscaling disabled, this is the maximum width * of the new image (in case the source image is * smaller than the expected width) * - INT 'h' represents the height of the new image * With upscaling disabled, this is the maximum height * - INT 'x1', 'y1', 'x2', 'y2' represent optional cropping * coordinates. The source image will first be cropped * to these coordinates, and then resized to match * width/height parameters * - BOOL 'square' - square images will fill the * bounding box (width x height). In Imagine's terms, * this equates to OUTBOUND mode * - BOOL 'upscale' - if enabled, smaller images * will be upscaled to fit the bounding box. * @return bool */ public function resize($source, $destination = null, array $params = []) { if (!isset($destination)) { $destination = $source; } try { $image = $this->imagine->open($source); $width = $image->getSize()->getWidth(); $height = $image->getSize()->getHeight(); $params = $this->normalizeResizeParameters($width, $height, $params); $max_width = elgg_extract('w', $params); $max_height = elgg_extract('h', $params); $x1 = (int) elgg_extract('x1', $params, 0); $y1 = (int) elgg_extract('y1', $params, 0); $x2 = (int) elgg_extract('x2', $params, 0); $y2 = (int) elgg_extract('y2', $params, 0); if ($x2 > $x1 && $y2 > $y1) { $crop_start = new Point($x1, $y1); $crop_size = new Box($x2 - $x1, $y2 - $y1); $image->crop($crop_start, $crop_size); } $target_size = new Box($max_width, $max_height); $thumbnail = $image->resize($target_size); $thumbnail->save($destination, ['jpeg_quality' => elgg_extract('jpeg_quality', $params, self::JPEG_QUALITY)]); unset($image); unset($thumbnail); } catch (Exception $ex) { _elgg_services()->logger->error($ex->getMessage()); return false; } return true; }
/** * {@inheritDoc} */ public function getImage($relativePath, $filter) { $eventManager = $this->getEventManager(); $eventManager->trigger(__FUNCTION__, $this, ['relativePath' => $relativePath, 'filter' => $filter]); $filterOptions = $this->filterManager->getFilterOptions($filter); $binary = $this->loaderManager->loadBinary($relativePath, $filter); if (isset($filterOptions['format'])) { $format = $filterOptions['format']; } else { $format = $binary->getFormat() ?: 'png'; } $imageOutputOptions = []; if (isset($filterOptions['quality'])) { $imageOutputOptions['quality'] = $filterOptions['quality']; } if ($format === 'gif' && $filterOptions['animated']) { $imageOutputOptions['animated'] = $filterOptions['animated']; } if ($this->cacheManager->isCachingEnabled($filter, $filterOptions) && $this->cacheManager->cacheExists($relativePath, $filter, $format)) { $imagePath = $this->cacheManager->getCachePath($relativePath, $filter, $format); $filteredImage = $this->imagine->open($imagePath); } else { $image = $this->imagine->load($binary->getContent()); $filteredImage = $this->filterManager->getFilter($filter)->apply($image); if ($this->cacheManager->isCachingEnabled($filter, $filterOptions)) { $this->cacheManager->createCache($relativePath, $filter, $filteredImage, $format, $imageOutputOptions); } } $args = ['relativePath' => $relativePath, 'filter' => $filter, 'filteredImage' => $filteredImage, 'format' => $format]; $eventManager->trigger(__FUNCTION__ . '.post', $this, $args); return ['image' => $filteredImage, 'format' => $format, 'imageOutputOptions' => $imageOutputOptions]; }
/** * @param ImageResourceInterface $resource * @return boolean */ public function process(ImageResourceInterface $resource) { $image = $this->imageService->open($resource->getPath()); $mode = ImagineImageInterface::THUMBNAIL_INSET; $image->thumbnail($this->imageBox, $mode)->save($this->pathFilter->filter($resource->getPath()), array('format' => $resource->getExt(), 'quality' => '100')); return true; }
/** * {@inheritDoc} */ public function find($path) { if (false !== strpos($path, '/../') || 0 === strpos($path, '../')) { throw new NotFoundHttpException(sprintf("Source image was searched with '%s' out side of the defined root path", $path)); } $file = $this->rootPath . '/' . ltrim($path, '/'); $info = $this->getFileInfo($file); $absolutePath = $info['dirname'] . DIRECTORY_SEPARATOR . $info['basename']; $name = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename']; $targetFormat = null; // set a format if an extension is found and is allowed if (isset($info['extension']) && (empty($this->formats) || in_array($info['extension'], $this->formats))) { $targetFormat = $info['extension']; } if (empty($targetFormat) || !file_exists($absolutePath)) { // attempt to determine path and format $absolutePath = null; foreach ($this->formats as $format) { if ($targetFormat !== $format && file_exists($name . '.' . $format)) { $absolutePath = $name . '.' . $format; break; } } if (!$absolutePath) { if (!empty($targetFormat) && is_file($name)) { $absolutePath = $name; } else { throw new NotFoundHttpException(sprintf('Source image not found in "%s"', $file)); } } } return $this->imagine->open($absolutePath); }
/** * @param ThumbId $thumbId * @param Photo $photo * @param PhotoThumbSize $thumbSize * @param HttpUrl $thumbHttpUrl * @return PhotoThumb */ public function generate(ThumbId $thumbId, Photo $photo, PhotoThumbSize $thumbSize, HttpUrl $thumbHttpUrl) { $photoFile = $photo->photoFile() ? $photo->photoFile() : $this->downloadPhoto($photo->getPhotoHttpUrl()); $thumbFile = $this->thumbGeneratorConfig->tempPath() . '/' . $thumbId->id() . '.' . self::CONVERSION_FORMAT; $target = new Box($thumbSize->width(), $thumbSize->height()); $originalImage = $this->imagine->open($photoFile->filePath()); $img = $originalImage->thumbnail($target, ImageInterface::THUMBNAIL_OUTBOUND); $img->save($thumbFile); return new PhotoThumb($thumbId, new PhotoId($photo->id()), $thumbHttpUrl, $thumbSize, new PhotoFile($thumbFile)); }
/** * Sets ImageRenderer as Renderer when ImageModel is used * * @param ViewEvent $e * @return ImageRenderer|null * @throws Exception\RuntimeException */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof ImageModel) { if (!$model->getImage() instanceof ImageInterface) { if (!$model->getImagePath()) { throw new Exception\RuntimeException('You must provide Imagine\\Image\\ImageInterface or path of image'); } $model->setImage($this->imagine->open($model->getImagePath())); } return new ImageRenderer(); } }
/** * {@inheritDoc} */ function load(array $options = array()) { if (false == isset($options['image'])) { throw new \InvalidArgumentException('Option "image" is required.'); } if (false == is_readable($options['image'])) { throw new \InvalidArgumentException('Expected image file exists and readable.'); } $x = isset($options['x']) ? $options['x'] : 0; $y = isset($options['y']) ? $options['y'] : 0; $image = $this->imagine->open($options['image']); return new PasteFilter($image, $x, $y); }
/** * {@inheritDoc} */ public function load(array $options = []) { if (!isset($options['image'])) { throw new Exception\InvalidArgumentException('Option "image" is required.'); } $x = isset($options['x']) ? $options['x'] : 0; $y = isset($options['y']) ? $options['y'] : 0; $path = $this->resolver->resolve($options['image']); if (!$path) { throw new Exception\RuntimeException(sprintf('Could not resolve %s', $options['image'])); } $image = $this->imagine->open($path); return new PasteFilter($image, $x, $y); }
/** * Forces image caching and returns path to cached image. * * @param string $basePath Deprecated parameter * @param string $path * @param string $filter * @param boolean $force * @param string $saveAs * * @return string|null * * @throws RuntimeException */ public function cacheImage($basePath, $path, $filter, $force = false, $saveAs = null) { $path = '/' . ltrim($path, '/'); $saveAs = $saveAs ? '/' . ltrim($saveAs, '/') : $path; // if cache path cannot be determined, return 404 if (!($cachedPath = $this->cachePathResolver->getCachedPath($saveAs, $filter))) { return null; } // if the file has already been cached, just return path if (!$force && is_file($cachedPath)) { return $cachedPath; } if (!($sourcePath = $this->cachePathResolver->getRealPath($path, $filter))) { return null; } $this->ensureDirectoryExists($cachedPath); try { $image = $this->imagine->open($sourcePath); } catch (RuntimeException $e) { try { // Make sure source path is an image new ImageFile($sourcePath, false); // Do not pollute the space (don't copy anything; symlink is just fine) $this->filesystem->symlink($sourcePath, $cachedPath); } catch (RuntimeException $e) { return null; } catch (IOException $e) { // In case we were not able to create symlink we should return source path. // This means we'll be back here, but at least we'll not be polluting space with useless copies. return $sourcePath; } return $cachedPath; } $options = ['quality' => $this->filterManager->getOption($filter, 'quality', $this->defaultQuality), 'format' => $this->filterManager->getOption($filter, 'format', null)]; // Important! optipng filter returns an instance of ImageAssetWrapper. /** @var ImageInterface|ImageAssetWrapper $image */ $image = $this->filterManager->getFilter($filter)->apply($image); /** @var resource $context */ if ($context = $this->findStreamContext($cachedPath, $filter)) { $tmpPath = tempnam(sys_get_temp_dir(), 'avalanche-cache-manager-proxy-'); $image->save($tmpPath, $options); copy($tmpPath, $cachedPath, $context); unlink($tmpPath); } else { $image->save($cachedPath, $options); } $this->ensureFilePermissions($cachedPath); return $cachedPath; }
/** * Resize an image using the computed settings. * * @param FileInterface $file * @param Style $style * @return string */ public function resize(FileInterface $file, Style $style) { $filePath = tempnam(sys_get_temp_dir(), 'STP') . '.' . $file->getFilename(); list($width, $height, $option) = $this->parseStyleDimensions($style); $method = "resize" . ucfirst($option); if ($method == 'resizeCustom') { $this->resizeCustom($file, $style->dimensions)->save($filePath, $style->convertOptions); return $filePath; } $image = $this->imagine->open($file->getRealPath()); if ($style->autoOrient) { $image = $this->autoOrient($file->getRealPath(), $image); } $this->{$method}($image, $width, $height)->save($filePath, $style->convertOptions); return $filePath; }
/** * Process an Image * * @param Image $image * * @return ImagineInterface */ public function process(Image $image) { $processors = $image->getSalts(); $image = $this->imagine->open($image->getOriginalImagePath()); // Apply each method one after the other foreach ($processors as $method => $arguments) { if (empty($arguments) or isset($arguments[0])) { $image = $this->executeMethod($image, $method, $arguments); } else { foreach ($arguments as $submethod => $arguments) { $this->executeSubmethod($image, $method, $submethod, $arguments); } } } return $image; }
/** * {@inheritdoc} */ protected function doTransform(MediaInterface $media) { parent::doTransform($media); if ($media->getBinaryContent() instanceof UploadedFile) { $fileName = $media->getBinaryContent()->getClientOriginalName(); } elseif ($media->getBinaryContent() instanceof File) { $fileName = $media->getBinaryContent()->getFilename(); } else { // Should not happen, FileProvider should throw an exception in that case return; } if (!in_array(strtolower(pathinfo($fileName, PATHINFO_EXTENSION)), $this->allowedExtensions) || !in_array($media->getBinaryContent()->getMimeType(), $this->allowedMimeTypes)) { return; } try { $image = $this->imagineAdapter->open($media->getBinaryContent()->getPathname()); } catch (\RuntimeException $e) { $media->setProviderStatus(MediaInterface::STATUS_ERROR); return; } $size = $image->getSize(); $media->setWidth($size->getWidth()); $media->setHeight($size->getHeight()); $media->setProviderStatus(MediaInterface::STATUS_OK); }
private function convertSvgToImage($content) { $temporaryFilePath = $this->createTemporaryFile($content); $image = $this->imagine->open($temporaryFilePath); unlink($temporaryFilePath); return $image->get('png'); }
/** * Generate Badge images * * @param Bundle $bundle */ public function generate(Bundle $bundle) { // Open bg badge image $image = $this->imagine->open($this->getResourceDir() . '/images/' . $this->type[self::LONG]); $imageShort = $this->imagine->open($this->getResourceDir() . '/images/' . $this->type[self::SHORT]); // Bundle Title $bundleName = $this->shorten($bundle->getName(), 16); $image->draw()->text($bundleName, $this->setFont($this->imagine, $this->font, 14, '085066'), new Point(75, 10)); // Score points $score = $bundle->getScore() ?: 'N/A'; $image->draw()->text($score, $this->setFont($this->imagine, $this->font, 18), $this->getPositionByType($score, self::LONG)); $imageShort->draw()->text($score, $this->setFont($this->imagine, $this->font, 16), $this->getPositionByType($score, self::SHORT)); // Recommend $recommenders = $bundle->getNbRecommenders(); if ($recommenders) { $recommendationsText = $recommenders . ' recommendations'; } else { $recommendationsText = 'No recommendations'; } $image->draw()->text($recommendationsText, $this->setFont($this->imagine, $this->font, 9), new Point(92, 33)); // Check or create dir for generated badges $this->createBadgesDir(); // Remove existing badge $this->filesystem->remove($this->getBadgeFile($bundle)); $this->filesystem->remove($this->getBadgeFile($bundle, self::SHORT)); // Save badge $image->save($this->getBadgeFile($bundle)); $imageShort->save($this->getBadgeFile($bundle, self::SHORT)); // Set write permission for father files update chmod($this->getBadgeFile($bundle), 0777); chmod($this->getBadgeFile($bundle, self::SHORT), 0777); }
/** * @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; }
/** * Execute the job. * * @param \Imagine\Image\ImagineInterface $imagine * * @return void */ public function handle(ImagineInterface $imagine) { $data = $this->getFilteredOptions($this->options); $path = $data['path']; $source = Str::replace('{filename}.{extension}', $data); $destination = Str::replace($data['format'], $data); $this->handleImageManipulation($imagine->open("{$path}/{$source}"), $data)->save("{$path}/{$destination}"); }
/** * @param File|null $image * @param $image_type * @return \Imagine\Image\ImageInterface */ public function getImageThumb(File $image = null, $image_type, $image_area = null) { if (isset($this->image_types[$image_type])) { $source = $this->imagine->open($image->getRealPath()); $this->preProcessSourceImg($source); $this->preProcessCropArea($source, $image_area); $type = $this->image_types[$image_type]; $transformer_class = self::TRANSFORMER_CLASS_PATH . "\\" . Inflector::classify($type['transform']) . 'Transformer'; $width = isset($type['width']) ? $type['width'] : null; $height = isset($type['height']) ? $type['height'] : null; /** @var $transformer TransformerInterface */ $transformer = new $transformer_class($this->imagine, $source, $width, $height); $retval = $transformer->getTransformed(); } else { throw new ResourceNotFoundException("Can't show image!"); } return $retval->strip(); }
/** * @param BinaryInterface $binary * @param array $config * * @throws \InvalidArgumentException * * @return Binary */ public function apply(BinaryInterface $binary, array $config) { $config = array_replace(array('filters' => array(), 'quality' => 100, 'animated' => false), $config); if ($binary instanceof FileBinaryInterface) { $image = $this->imagine->open($binary->getPath()); } else { $image = $this->imagine->load($binary->getContent()); } foreach ($config['filters'] as $eachFilter => $eachOptions) { if (!isset($this->loaders[$eachFilter])) { throw new \InvalidArgumentException(sprintf('Could not find filter loader for "%s" filter type', $eachFilter)); } $prevImage = $image; $image = $this->loaders[$eachFilter]->load($image, $eachOptions); // If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't // See https://github.com/liip/LiipImagineBundle/pull/682 if ($prevImage !== $image && method_exists($prevImage, '__destruct')) { $prevImage->__destruct(); } } $options = array('quality' => $config['quality']); if (isset($config['jpeg_quality'])) { $options['jpeg_quality'] = $config['jpeg_quality']; } if (isset($config['png_compression_level'])) { $options['png_compression_level'] = $config['png_compression_level']; } if (isset($config['png_compression_filter'])) { $options['png_compression_filter'] = $config['png_compression_filter']; } if ($binary->getFormat() === 'gif' && $config['animated']) { $options['animated'] = $config['animated']; } $filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat(); $filteredContent = $image->get($filteredFormat, $options); $filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent); // We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't // See https://github.com/liip/LiipImagineBundle/pull/682 if (method_exists($image, '__destruct')) { $image->__destruct(); } return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); }
/** * Creates an image object * * @param array|\Imagine\Image\ImageInterface $source * @throws \InvalidArgumentException On unsupported source type * @return \Imagine\Image\ImageInterface */ public function create($source) { if ($source instanceof ImageInterface) { return $source; } if (isset($source['file'])) { return $this->imagine->open($source['file']); } if (isset($source['data'])) { return $this->imagine->load($source['data']); } if (isset($source['resource'])) { return $this->imagine->read($source['resource']); } if (isset($source['width']) && isset($source['height'])) { return $this->imagine->create(new Box($source['width'], $source['height'])); } throw new InvalidArgumentException(); }
/** * Run this controller. Generate some images. * * @return void */ public function __invoke() { $upload_settings = $this->settings->read('uploads'); if (!file_exists($this->site_directory . 'uploads/')) { mkdir($this->site_directory . 'uploads/', 0775, true); } foreach (new \DirectoryIterator($this->data_directory . 'uploads/') as $file) { if (in_array($file->getExtension(), ['jpg', 'jpeg', 'gif', 'png'])) { $source_image = $this->imagine->open($this->data_directory . 'uploads/' . $file); $source_image->save($this->site_directory . 'uploads/' . $file); if (is_array($upload_settings)) { foreach ($upload_settings as $name => $settings) { list($width, $height) = $settings; $this->thumbnail('uploads/' . $file, $name, $width, $height); } } } } }
/** * Return EXIF metadata for a file by it's path * * @param $filePath * * @return array */ public function getExifMetadata($filePath) { try { $exifReader = new \Imagine\Image\Metadata\ExifMetadataReader(); $this->_instance->setMetadataReader($exifReader); $exif = $this->_instance->open($filePath)->metadata(); return $exif->toArray(); } catch (\Imagine\Exception\NotSupportedException $exception) { Craft::log($exception->getMessage(), LogLevel::Error); return array(); } }
/** * This action applies a given filter to a given image, saves the image and * outputs it to the browser at the same time * * @param string $path * @param string $filter * * @return Response */ public function filter($path, $filter) { $cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter); // if cache path cannot be determined, return 404 if (null === $cachedPath) { throw new NotFoundHttpException('Image doesn\'t exist'); } ob_start(); try { $format = $this->filterManager->getOption($filter, "format", "png"); $this->imagine->open($cachedPath)->show($format); $type = 'image/' . $format; $length = ob_get_length(); $content = ob_get_clean(); // TODO: add more media headers $response = new Response($content, 201, array('content-type' => $type, 'content-length' => $length)); // Cache $cacheType = $this->filterManager->getOption($filter, "cache_type", false); if (false == $cacheType) { return $response; } $cacheType === "public" ? $response->setPublic() : $response->setPrivate(); $cacheExpires = $this->filterManager->getOption($filter, "cache_expires", "1 day"); $expirationDate = new \DateTime("+" . $cacheExpires); $maxAge = $expirationDate->format("U") - time(); if ($maxAge < 0) { throw new \InvalidArgumentException("Invalid cache expiration date"); } $response->setExpires($expirationDate); $response->setMaxAge($maxAge); return $response; } catch (\Exception $e) { ob_end_clean(); throw $e; } }
public function getImagineImage(ImagineInterface $imagine, FontCollection $fontCollection, $width, $height) { $watermarkImage = $imagine->open($this->getPath()); if ($this->getRelativeSize() !== null) { $y = (int) $height * $this->getRelativeSize() / 100; $factor = $y / $watermarkImage->getSize()->getHeight(); $x = $watermarkImage->getSize()->getWidth() * $factor; $actualWidth = $width - abs($this->positionX); if ($x > $actualWidth) { $factor = $actualWidth / $x; $x = $actualWidth; $y *= $factor; } $watermarkImage->resize(new \Imagine\Image\Box($x, $y)); } return $watermarkImage; }
/** * This action applies a given filter to a given image, saves the image and * outputs it to the browser at the same time * * @param string $path * @param string $filter * * @return Response */ public function filter($path, $filter) { $path = '/' . ltrim($path, '/'); //TODO: find out why I need double urldecode to get a valid path $browserPath = urldecode(urldecode($this->cachePathResolver->getBrowserPath($path, $filter))); $basePath = $this->request->getBaseUrl(); if (!empty($basePath) && 0 === strpos($browserPath, $basePath)) { $browserPath = substr($browserPath, strlen($basePath)); } // if cache path cannot be determined, return 404 if (null === $browserPath) { throw new NotFoundHttpException('Image doesn\'t exist'); } $realPath = $this->webRoot . $browserPath; $sourcePath = $this->sourceRoot . $path; if (!file_exists($sourcePath)) { throw new NotFoundHttpException(sprintf('Source image not found in "%s"', $sourcePath)); } $ext = pathinfo($sourcePath, PATHINFO_EXTENSION); if ($ext == 'gif' && $this->isAnimatedGif(fopen($sourcePath, 'r'))) { ob_start(); echo stream_get_contents(fopen($sourcePath, 'r')); return new Response(ob_get_clean(), 201, array('content-type' => 'image/gif')); } // if the file has already been cached, we're probably not rewriting // correctly, hence make a 301 to proper location, so browser remembers if (file_exists($realPath)) { return new Response('', 301, array('location' => $this->request->getBasePath() . $browserPath)); } $dir = pathinfo($realPath, PATHINFO_DIRNAME); if (!is_dir($dir)) { if (!$this->filesystem->mkdir($dir)) { throw new \RuntimeException(sprintf('Could not create directory %s', $dir)); } } ob_start(); try { // TODO: get rid of hard-coded quality and format $this->filterManager->get($filter)->apply($this->imagine->open($sourcePath))->save($realPath, array('quality' => $this->filterManager->getOption($filter, "quality", 100)))->show($this->filterManager->getOption($filter, "format", "png")); // TODO: add more media headers return new Response(ob_get_clean(), 201, array('content-type' => 'image/' . $this->filterManager->getOption($filter, "format", "png"))); } catch (\Exception $e) { ob_end_clean(); throw $e; } }
/** * {@inheritDoc} */ public function process(MediaInterface $media, VariantInterface $variant, \SplFileInfo $source = NULL) { $options = $variant->getOptions(); $result = $source; list($originalWidth, $originalHeight) = getimagesize($source->getPathName()); $width = $originalWidth; $height = $originalHeight; if (is_array($options) && !empty($options)) { if ($this->imagine == NULL) { throw new ProviderProcessException(sprintf('Cannot process image "%s": Imagine library not installed or misconfigured', $media), $this, $media, $variant); } $options = $this->processOptions($options, $variant->getName(), $media->getContext()); $destFile = sprintf('%s%s-temp-%s.%s', $this->tempDir, date('Y-m-d-h-i-s'), $source->getBasename('.' . $source->getExtension()), $options['format']); /** * @var \Imagine\Image\ImageInterface $image */ $image = $this->imagine->open($source); if ($options['enlarge'] === TRUE || $originalWidth >= $options['width'] && $originalHeight >= $options['height']) { $width = $options['width']; $height = $options['height']; if ($options['resize'] == 'proportional') { //calculate missing dimension if ($width === NULL) { $width = round($originalWidth * $height / $originalHeight); } elseif ($height === NULL) { $height = round($width * $originalHeight / $originalWidth); } } $box = new \Imagine\Image\Box($width, $height); if ($options['resize'] == 'proportional' || $options['resize'] == 'stretch') { $image->resize($box); } elseif ($options['resize'] == 'crop') { $image = $image->thumbnail($box, \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND); } } $image->save($destFile, array('quality' => $options['quality'])); $this->addTempFile($destFile); $result = new \SplFileInfo($destFile); } //set variant metadata $variant->setMetaValue('size', $result->getSize()); $variant->setMetaValue('width', $width); $variant->setMetaValue('height', $height); return $result; }
/** * Loads an image from a file system path. * * @param string $path * * @throws Exception * @return Image */ public function loadImage($path) { if (!IOHelper::fileExists($path)) { throw new Exception(Craft::t('No file exists at the path “{path}”', array('path' => $path))); } if (!craft()->images->checkMemoryForImage($path)) { throw new Exception(Craft::t("Not enough memory available to perform this image operation.")); } $imageInfo = @getimagesize($path); if (!is_array($imageInfo)) { throw new Exception(Craft::t('The file “{path}” does not appear to be an image.', array('path' => $path))); } $this->_image = $this->_instance->open($path); $this->_extension = IOHelper::getExtension($path); $this->_imageSourcePath = $path; if ($this->_extension == 'gif') { if (!craft()->images->isGd() && $this->_image->layers()) { $this->_isAnimatedGif = true; } } return $this; }
/** * {@inheritdoc} */ public function getDimensions() { if (null === $this->dimensions) { // Try getSvgSize() or native getimagesize() for better performance if ($this->imagine instanceof SvgImagine) { $size = $this->getSvgSize(); if (null !== $size) { $this->dimensions = new ImageDimensions($size); } } else { $size = @getimagesize($this->path); if (!empty($size[0]) && !empty($size[1])) { $this->dimensions = new ImageDimensions(new Box($size[0], $size[1])); } } // Fall back to Imagine if (null === $this->dimensions) { $this->dimensions = new ImageDimensions($this->imagine->open($this->path)->getSize()); } } return $this->dimensions; }
/** * getGallery * * @param SiteInterface $site * @param bool $syncFirst * @return array */ public function getGallery(SiteInterface $site, $syncFirst = false) { if (true === $syncFirst) { $this->sync($site); } $images = $this->getGalleryQuery($site)->getQuery()->getResult(); foreach ($images as $image) { $cachedImage = $this->getCached($image); if (!$cachedImage instanceof \SplFileInfo || !$cachedImage->isFile()) { $cachedImage = $this->createCached($image); } $image->setCached($cachedImage); } return $images; foreach ($images as $file) { if (stripos($file->getRealPath(), $this->cacheDir->getRealPath()) !== false) { continue; // finder isnt working correctly? } if (!$file->getRealPath()) { continue; } $cacheName = md5_file($file->getRealPath()) . '.' . $file->getExtension(); $cachePath = $this->cacheDir->getRealPath() . '/' . $cacheName; $image = $this->imagine->open($file->getRealPath()); $fullDimensions = $image->getSize(); if (!file_exists($cachePath)) { $image->resize(new Box($this->options['thumbnail_width'], $this->options['thumbnail_height']))->save($cachePath); $cachedDimensions = $image->getSize(); } else { $cachedImage = $this->imagine->open($cachePath); $cachedDimensions = $cachedImage->getSize(); } $cachedFile = new \SplFileInfo($cachePath); $return[] = array('original' => $file, 'cache' => $cachedFile, 'width' => $fullDimensions->getWidth(), 'height' => $fullDimensions->getHeight(), 'cache_width' => $cachedDimensions->getWidth(), 'cache_height' => $cachedDimensions->getHeight()); } return $return; }
/** * {@inheritdoc} */ public function updateMetadata(MediaInterface $media, $force = true) { try { if (!$media->getBinaryContent() instanceof \SplFileInfo) { // this is now optimized at all!!! $path = tempnam(sys_get_temp_dir(), 'sonata_update_metadata'); $fileObject = new \SplFileObject($path, 'w'); $fileObject->fwrite($this->getReferenceFile($media)->getContent()); } else { $fileObject = $media->getBinaryContent(); } $image = $this->imagineAdapter->open($fileObject->getPathname()); $size = $image->getSize(); $media->setSize($fileObject->getSize()); $media->setWidth($size->getWidth()); $media->setHeight($size->getHeight()); } catch (\LogicException $e) { $media->setProviderStatus(MediaInterface::STATUS_ERROR); $media->setSize(0); $media->setWidth(0); $media->setHeight(0); } }
/** * Creates a gray icon. * @param \Imagine\Image\ImagineInterface $imagine * @return bool */ public function createGrayIcon($imagine) { $customIcon = $this->getCustomIcon(); if (empty($customIcon)) { return false; } if (file_exists($this->getAbsolutePath())) { $image = $imagine->open($this->getAbsolutePath()); $fileInfo = pathinfo($this->getAbsolutePath()); $originalFilename = $fileInfo['basename']; $filename = $fileInfo['filename'] . '_na.' . $fileInfo['extension']; $newPath = str_replace($originalFilename, $filename, $this->getAbsolutePath()); $transformation = new \Imagine\Filter\Advanced\Grayscale(); $transformation->apply($image)->save($newPath); } }