本文整理汇总了PHP中Imagine\Image\ImagineInterface类的典型用法代码示例。如果您正苦于以下问题:PHP ImagineInterface类的具体用法?PHP ImagineInterface怎么用?PHP ImagineInterface使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImagineInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* 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}");
}
示例2: getImagineImage
public function getImagineImage(ImagineInterface $imagine, FontCollection $fontCollection, $width, $height)
{
$fontPath = $fontCollection->getFontById($this->font)->getPath();
if ($this->getAbsoluteSize() !== null) {
$fontSize = $this->getAbsoluteSize();
} elseif ($this->getRelativeSize() !== null) {
$fontSize = (int) $this->getRelativeSize() / 100 * $height;
} else {
throw new \LogicException('Either relative or absolute watermark size must be set!');
}
if (true || !class_exists('ImagickDraw')) {
// Fall back to ugly image.
$palette = new \Imagine\Image\Palette\RGB();
$font = $imagine->font($fontPath, $fontSize, $palette->color('#000'));
$box = $font->box($this->getText());
$watermarkImage = $imagine->create($box, $palette->color('#FFF'));
$watermarkImage->draw()->text($this->text, $font, new \Imagine\Image\Point(0, 0));
} else {
// CURRENTLY DISABLED.
// Use nicer Imagick implementation.
// Untested!
// @todo Test and implement it!
$draw = new \ImagickDraw();
$draw->setFont($fontPath);
$draw->setFontSize($fontSize);
$draw->setStrokeAntialias(true);
//try with and without
$draw->setTextAntialias(true);
//try with and without
$draw->setFillColor('#fff');
$textOnly = new \Imagick();
$textOnly->newImage(1400, 400, "transparent");
//transparent canvas
$textOnly->annotateImage($draw, 0, 0, 0, $this->text);
//Create stroke
$draw->setFillColor('#000');
//same as stroke color
$draw->setStrokeColor('#000');
$draw->setStrokeWidth(8);
$strokeImage = new \Imagick();
$strokeImage->newImage(1400, 400, "transparent");
$strokeImage->annotateImage($draw, 0, 0, 0, $this->text);
//Composite text over stroke
$strokeImage->compositeImage($textOnly, \Imagick::COMPOSITE_OVER, 0, 0, \Imagick::CHANNEL_ALPHA);
$strokeImage->trimImage(0);
//cut transparent border
$watermarkImage = $imagine->load($strokeImage->getImageBlob());
//$strokeImage->resizeImage(300,0, \Imagick::FILTER_CATROM, 0.9, false); //resize to final size
}
return $watermarkImage;
}
示例3: 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;
}
示例4: createImageVersions
/**
* @param string $imagePath
* @param string[]|ConfigInterface[] $versionsConfig
* @param string $outputDir
* @param string $imageUniqueName
*
* @return string[]
*/
public function createImageVersions($imagePath, $versionsConfig, $outputDir = null, $imageUniqueName = null)
{
if ($outputDir === null) {
$outputDir = $this->outputDir;
}
if ($imageUniqueName === null) {
$imageUniqueName = $this->getUniqueName($imagePath);
}
$versions = [];
$imageExt = pathinfo($imagePath, PATHINFO_EXTENSION);
foreach ($versionsConfig as $versionName => $versionConfig) {
if (is_string($versionConfig)) {
$config = $this->configParser->parse($versionConfig);
} elseif ($versionConfig instanceof ConfigInterface) {
$config = $versionConfig;
} else {
throw new InvalidConfigException('Config must be a string or implement happyproff\\Kartinki\\Interfaces\\ConfigInterface.');
}
$versionFilename = $imageUniqueName . self::NAME_SEPARATOR . $versionName . '.' . $imageExt;
$image = $this->processor->read(fopen($imagePath, 'r'));
$version = $this->createImageVersion($image, $config);
$version->save($outputDir . '/' . $versionFilename, ['jpeg_quality' => $config->getQuality()]);
unset($version);
$versions[$versionName] = $versionFilename;
}
return $versions;
}
示例5: process
/**
* @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;
}
示例6: 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);
}
示例7: 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];
}
示例8: find
/**
* {@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);
}
示例9: resize
/**
* 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;
}
示例10: 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());
}
示例11: 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()));
}
示例12: generate
/**
* @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));
}
示例13: load
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
{
$background = $image->palette()->color(isset($options['color']) ? $options['color'] : '#fff', isset($options['transparency']) ? $options['transparency'] : null);
$topLeft = new Point(0, 0);
$size = $image->getSize();
if (isset($options['size'])) {
list($width, $height) = $options['size'];
$position = isset($options['position']) ? $options['position'] : 'center';
switch ($position) {
case 'topleft':
$x = 0;
$y = 0;
break;
case 'top':
$x = ($width - $image->getSize()->getWidth()) / 2;
$y = 0;
break;
case 'topright':
$x = $width - $image->getSize()->getWidth();
$y = 0;
break;
case 'left':
$x = 0;
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'center':
$x = ($width - $image->getSize()->getWidth()) / 2;
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'right':
$x = $width - $image->getSize()->getWidth();
$y = ($height - $image->getSize()->getHeight()) / 2;
break;
case 'bottomleft':
$x = 0;
$y = $height - $image->getSize()->getHeight();
break;
case 'bottom':
$x = ($width - $image->getSize()->getWidth()) / 2;
$y = $height - $image->getSize()->getHeight();
break;
case 'bottomright':
$x = $width - $image->getSize()->getWidth();
$y = $height - $image->getSize()->getHeight();
break;
default:
throw new \InvalidArgumentException("Unexpected position '{$position}'");
break;
}
$size = new Box($width, $height);
$topLeft = new Point($x, $y);
}
$canvas = $this->imagine->create($size, $background);
return $canvas->paste($image, $topLeft);
}
示例14: load
/**
* {@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);
}
示例15: load
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
{
$background = new Color(isset($options['color']) ? $options['color'] : '#fff', isset($options['transparency']) ? $options['transparency'] : 0);
$topLeft = new Point(0, 0);
$size = $image->getSize();
if (isset($options['size'])) {
list($width, $height) = $options['size'];
$size = new Box($width, $height);
$topLeft = new Point(($width - $image->getSize()->getWidth()) / 2, ($height - $image->getSize()->getHeight()) / 2);
}
$canvas = $this->imagine->create($size, $background);
return $canvas->paste($image, $topLeft);
}