/** * {@inheritdoc} */ public function store(BinaryInterface $binary, $path, $filter) { $objectPath = $this->getObjectPath($path, $filter); $storageResponse = $this->storage->create_object($this->bucket, $objectPath, array('body' => $binary->getContent(), 'contentType' => $binary->getMimeType(), 'length' => strlen($binary->getContent()), 'acl' => $this->acl)); if (!$storageResponse->isOK()) { $this->logError('The object could not be created on Amazon S3.', array('objectPath' => $objectPath, 'filter' => $filter, 's3_response' => $storageResponse)); throw new NotStorableException('The object could not be created on Amazon S3.'); } }
/** * {@inheritDoc} */ public function store(BinaryInterface $binary, $path, $filter) { $this->filesystem->dumpFile( $this->getFilePath($path, $filter), $binary->getContent() ); }
/** * @param BinaryInterface $binary * @param array $options * * @throws ProcessFailedException * * @return BinaryInterface */ public function processWithConfiguration(BinaryInterface $binary, array $options) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/jpeg', 'image/jpg'))) { return $binary; } $pb = new ProcessBuilder(array($this->mozjpegBin)); // Places emphasis on DC $pb->add('-quant-table'); $pb->add(2); $transformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality; if ($transformQuality !== null) { $pb->add('-quality'); $pb->add($transformQuality); } $pb->add('-optimise'); // Favor stdin/stdout so we don't waste time creating a new file. $pb->setInput($binary->getContent()); $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { throw new ProcessFailedException($proc); } $result = new Binary($proc->getOutput(), $binary->getMimeType(), $binary->getFormat()); return $result; }
/** * @param BinaryInterface $binary * * @throws ProcessFailedException * * @return BinaryInterface * * @see Implementation taken from Assetic\Filter\optipngFilter */ public function process(BinaryInterface $binary) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/png'))) { return $binary; } if (false === ($input = tempnam(sys_get_temp_dir(), 'imagine_optipng'))) { throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', sys_get_temp_dir())); } $pb = new ProcessBuilder(array($this->optipngBin)); $pb->add('--o7'); $pb->add($input); if ($binary instanceof FileBinaryInterface) { copy($binary->getPath(), $input); } else { file_put_contents($input, $binary->getContent()); } $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { unlink($input); throw new ProcessFailedException($proc); } $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); unlink($input); return $result; }
/** * @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); $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)); } $image = $this->loaders[$eachFilter]->load($image, $eachOptions); } $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); return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); }
/** * Stores image alias in the IO Repository. * A temporary file is created to dump the filtered image and is used as basis for creation in the IO Repository. * * {@inheritdoc} */ public function store(BinaryInterface $binary, $path, $filter) { $tmpFile = tmpfile(); fwrite($tmpFile, $binary->getContent()); $tmpMetadata = stream_get_meta_data($tmpFile); $binaryCreateStruct = $this->ioService->newBinaryCreateStructFromLocalFile($tmpMetadata['uri']); $binaryCreateStruct->id = $this->getFilePath($path, $filter); $this->ioService->createBinaryFile($binaryCreateStruct); fclose($tmpFile); }
/** * {@inheritDoc} */ public function store(BinaryInterface $binary, $path, $filter) { $objectPath = $this->getObjectPath($path, $filter); try { $this->storage->putObject(array('ACL' => $this->acl, 'Bucket' => $this->bucket, 'Key' => $objectPath, 'Body' => $binary->getContent(), 'ContentType' => $binary->getMimeType())); } catch (\Exception $e) { $this->logError('The object could not be created on Amazon S3.', array('objectPath' => $objectPath, 'filter' => $filter, 'bucket' => $this->bucket, 'exception' => $e)); throw new NotStorableException('The object could not be created on Amazon S3.', null, $e); } }
/** * @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); $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)); } $image = $this->loaders[$eachFilter]->load($image, $eachOptions); } $options = array('quality' => $config['quality']); if ($binary->getFormat() === 'gif' && $config['animated']) { $options['animated'] = $config['animated']; } $filteredContent = $image->get($binary->getFormat(), $options); return new Binary($filteredContent, $binary->getMimeType(), $binary->getFormat()); }
/** * @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); }
/** * @param BinaryInterface $binary * * @throws ProcessFailedException * * @return BinaryInterface * * @see Implementation taken from Assetic\Filter\optipngFilter */ public function process(BinaryInterface $binary) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/png'))) { return $binary; } $pb = new ProcessBuilder(array($this->optipngBin)); $pb->add('--o7'); $pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_optipng')); file_put_contents($input, $binary->getContent()); $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { unlink($input); throw new ProcessFailedException($proc); } $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); unlink($input); return $result; }
/** * @param BinaryInterface $binary * @param array $options * * @throws ProcessFailedException * * @return BinaryInterface */ public function processWithConfiguration(BinaryInterface $binary, array $options) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/png'))) { return $binary; } $pb = new ProcessBuilder(array($this->pngquantBin)); // Specify quality. $tranformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality; $pb->add('--quality'); $pb->add($tranformQuality); // Read to/from stdout to save resources. $pb->add('-'); $pb->setInput($binary->getContent()); $proc = $pb->getProcess(); $proc->run(); // 98 and 99 are "quality too low" to compress current current image which, while isn't ideal, is not a failure if (!in_array($proc->getExitCode(), array(0, 98, 99))) { throw new ProcessFailedException($proc); } $result = new Binary($proc->getOutput(), $binary->getMimeType(), $binary->getFormat()); return $result; }
/** * Save the transformed file * * @param string $endpoint * @param BinaryInterface $cropedFile * @param array $data */ public function saveTransformedFile($endpoint, BinaryInterface $cropedFile, array $data) { $this->filesystemMap->get($this->configuration->getValue($endpoint, 'croped_fs'))->write($data['filename'], $cropedFile->getContent()); }
/** * {@inheritDoc} */ public function store(BinaryInterface $binary, $path, $filter) { $filePath = $this->getFilePath($path, $filter); $dir = pathinfo($filePath, PATHINFO_DIRNAME); $this->makeFolder($dir); file_put_contents($filePath, $binary->getContent()); }
public function storeImageAsAvatar(BinaryInterface $image) { $path = $this->generateAvatarPath($image->getMimeType()); $this->fileUploadService->storeImage($path, $image->getContent()); return $path; }
/** * @param BinaryInterface $binary * * @throws ProcessFailedException * * @return BinaryInterface * * @see Implementation taken from Assetic\Filter\JpegoptimFilter */ public function process(BinaryInterface $binary) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/jpeg', 'image/jpg'))) { return $binary; } $pb = new ProcessBuilder(array($this->jpegoptimBin)); if ($this->stripAll) { $pb->add('--strip-all'); } if ($this->max) { $pb->add('--max=' . $this->max); } if ($this->progressive) { $pb->add('--all-progressive'); } else { $pb->add('--all-normal'); } $pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_jpegoptim')); file_put_contents($input, $binary->getContent()); $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { unlink($input); throw new ProcessFailedException($proc); } $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); unlink($input); return $result; }