本文整理汇总了PHP中Imagine\Image\ImagineInterface::load方法的典型用法代码示例。如果您正苦于以下问题:PHP ImagineInterface::load方法的具体用法?PHP ImagineInterface::load怎么用?PHP ImagineInterface::load使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Imagine\Image\ImagineInterface
的用法示例。
在下文中一共展示了ImagineInterface::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getImage
/**
* {@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];
}
示例2: showAction
/**
* @param Request $request
* @param string $filename
*
* @throws NotFoundHttpException If media is not found
*
* @return Response
*/
public function showAction(Request $request, $filename)
{
if (!$this->filesystem->has($filename)) {
throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename));
}
$response = new Response($content = $this->filesystem->read($filename));
$mime = $this->filesystem->mimeType($filename);
if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) {
try {
$cachePath = $this->cacheManager->resolve($request, $filename, $filter);
if ($cachePath instanceof Response) {
$response = $cachePath;
} else {
$image = $this->imagine->load($content);
$response = $this->filterManager->get($request, $filter, $image, $filename);
$response = $this->cacheManager->store($response, $cachePath, $filter);
}
} catch (\RuntimeException $e) {
if (0 === strpos($e->getMessage(), 'Filter not defined')) {
throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e);
}
throw $e;
}
}
if ($mime) {
$response->headers->set('Content-Type', $mime);
}
return $response;
}
示例3: apply
/**
* @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);
}
示例4: find
/**
* {@inheritDoc}
*/
public function find($id)
{
$image = $this->dm->getRepository($this->class)->findAll()->getCollection()->findOne(array("_id" => new \MongoId($id)));
if (!$image) {
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $id));
}
return $this->imagine->load($image['file']->getBytes());
}
示例5: resize
/**
* {@inheritdoc}
*/
public function resize(MediaInterface $media, File $in, File $out, $format, array $settings)
{
if (!isset($settings['width'])) {
throw new \RuntimeException(sprintf('Width parameter is missing in context "%s" for provider "%s"', $media->getContext(), $media->getProviderName()));
}
$image = $this->adapter->load($in->getContent());
$content = $image->thumbnail($this->getBox($media, $settings), $this->mode)->get($format, array('quality' => $settings['quality']));
$out->setContent($content, $this->metadata->get($media, $out->getName()));
}
示例6: convert
/**
* {@inheritdoc}
*/
public function convert(FileVersion $fileVersion, $formatKey)
{
$content = $this->storage->loadAsString($fileVersion->getName(), $fileVersion->getVersion(), $fileVersion->getStorageOptions());
$extractedImage = $this->mediaImageExtractor->extract($content);
try {
$image = $this->imagine->load($extractedImage);
} catch (RuntimeException $e) {
throw new InvalidFileTypeException($e->getMessage());
}
$image = $this->toRGB($image);
$format = $this->getFormat($formatKey);
$cropParameters = $this->getCropParameters($image, $fileVersion->getFormatOptions()->get($formatKey), $this->formats[$formatKey]);
if (isset($cropParameters)) {
$image = $this->applyFormatCrop($image, $cropParameters);
}
if (isset($format['scale']) && $format['scale']['mode'] !== ImageInterface::THUMBNAIL_INSET) {
$image = $this->applyFocus($image, $fileVersion, $format['scale']);
}
if (isset($format['scale'])) {
$image = $this->applyScale($image, $format['scale']);
}
if (isset($format['transformations'])) {
$image = $this->applyTransformations($image, $format['transformations']);
}
$image->strip();
// Set Interlacing to plane for smaller image size.
if (count($image->layers()) == 1) {
$image->interlace(ImageInterface::INTERLACE_PLANE);
}
$imagineOptions = $format['options'];
$imageExtension = $this->getImageExtension($fileVersion->getName());
return $image->get($imageExtension, $this->getOptionsFromImage($image, $imageExtension, $imagineOptions));
}
示例7: apply
/**
* @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());
}
示例8: drawImage
/**
* Draw $image on $this->resultImage.
*
* @param string $image Image blob.
* @param Point $point The point where to draw $image.
*
* @return void
*/
protected function drawImage($image, Point $point)
{
try {
$this->resultImage->paste($this->imagine->load($image), $point);
} catch (\Exception $exception) {
// Most likely an exception about a out of bounds past, we'll just ignore that
}
}
示例9: find
/**
* {@inheritDoc}
*/
public function find($path)
{
$name = $this->wrapperPrefix . $path;
/*
* This looks strange, but at least in PHP 5.3.8 it will raise an E_WARNING if the 4th parameter is null.
* fopen() will be called only once with the correct arguments.
*
* The error suppression is solely to determine whether the file exists.
* file_exists() is not used as not all wrappers support stat() to actually check for existing resources.
*/
if ($this->context && !($resource = @fopen($name, 'r', null, $this->context)) || !($resource = @fopen($name, 'r'))) {
throw new NotFoundHttpException('Source image not found.');
}
// Closing the opened stream to avoid locking of the resource to find.
fclose($resource);
return $this->imagine->load(file_get_contents($name, null, $this->context));
}
示例10: loadImage
/**
* 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."));
}
$extension = IOHelper::getExtension($path);
if ($extension === 'svg') {
if (!craft()->images->isImagick()) {
throw new Exception(Craft::t('The file “{path}” does not appear to be an image.', array('path' => $path)));
}
$svg = IOHelper::getFileContents($path);
if ($this->minSvgWidth !== null && $this->minSvgHeight !== null) {
// Does the <svg> node contain valid `width` and `height` attributes?
list($width, $height) = ImageHelper::parseSvgSize($svg);
if ($width !== null && $height !== null) {
$scale = 1;
if ($width < $this->minSvgWidth) {
$scale = $this->minSvgWidth / $width;
}
if ($height < $this->minSvgHeight) {
$scale = max($scale, $this->minSvgHeight / $height);
}
$width = round($width * $scale);
$height = round($height * $scale);
if (preg_match(ImageHelper::SVG_WIDTH_RE, $svg) && preg_match(ImageHelper::SVG_HEIGHT_RE, $svg)) {
$svg = preg_replace(ImageHelper::SVG_WIDTH_RE, "\${1}{$width}px\"", $svg);
$svg = preg_replace(ImageHelper::SVG_HEIGHT_RE, "\${1}{$height}px\"", $svg);
} else {
$svg = preg_replace(ImageHelper::SVG_TAG_RE, "\${1} width=\"{$width}px\" height=\"{$height}px\" \${2}", $svg);
}
}
}
try {
$this->_image = $this->_instance->load($svg);
} catch (\Imagine\Exception\RuntimeException $e) {
// Invalid SVG. Maybe it's missing its DTD?
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg;
$this->_image = $this->_instance->load($svg);
}
} else {
$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 = $extension;
$this->_imageSourcePath = $path;
if ($this->_extension == 'gif') {
if (!craft()->images->isGd() && $this->_image->layers()) {
$this->_isAnimatedGif = true;
}
}
return $this;
}
示例11: apply
/**
* @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);
}
示例12: create
/**
* 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();
}
示例13: loadFromSVG
/**
* Load an image from an SVG string.
*
* @param $svgContent
*
* @return Image
*/
public function loadFromSVG($svgContent)
{
try {
$this->_image = $this->_instance->load($svgContent);
} catch (\Imagine\Exception\RuntimeException $e) {
// Invalid SVG. Maybe it's missing its DTD?
$svgContent = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svgContent;
$this->_image = $this->_instance->load($svgContent);
}
return $this;
}
示例14: setSizeWidthAndHeight
/**
* Set size of file, if it's image, set width and height.
*
* @param MediaInterface $media
*
* @return MediaInterface
*/
public function setSizeWidthAndHeight(MediaInterface $media)
{
$key = $this->originalDir . '/' . $media->getReference();
$media->setSize($this->filesystemManipulator->size($key));
if ($media->getType() === $media::IMAGE) {
$image = $this->imagine->load($this->filesystemManipulator->read($key));
$size = $image->getSize();
$media->setWidth($size->getWidth());
$media->setHeight($size->getHeight());
}
return $this;
}
示例15: normalizeImage
/**
* {@inheritDoc}
*/
public function normalizeImage($name, $content, $width, $height)
{
$extension = $this->getExtension($name);
$image = $this->imagine->load($content);
$size = $image->getSize();
$box = new \Imagine\Image\Box($size->getWidth(), $size->getHeight());
if ($size->getWidth() >= $size->getHeight() && $size->getWidth() > $width) {
return $image->resize($box->widen($width))->get($extension);
} elseif ($size->getWidth() < $size->getHeight() && $size->getHeight() > $height) {
return $image->resize($box->heighten($height))->get($extension);
}
return $image->get($extension);
}