本文整理汇总了PHP中Imagine\Image\BoxInterface类的典型用法代码示例。如果您正苦于以下问题:PHP BoxInterface类的具体用法?PHP BoxInterface怎么用?PHP BoxInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BoxInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getHeightWidth
/**
* @param $parameters
* @param \Imagine\Image\BoxInterface $size
*
* @return array
*/
protected function getHeightWidth($parameters, $size)
{
$newWidth = $parameters['x'];
$newHeight = $parameters['y'];
// retina x2
if ($parameters['retina']) {
$newWidth = $parameters['x'] * 2;
$newHeight = $parameters['x'] * 2;
}
// calculate height when not set
if (!$newHeight) {
$newHeight = $size->getHeight() / $size->getWidth() * $newWidth;
}
// calculate width when not set
if (!$newWidth) {
$newWidth = $size->getWidth() / $size->getHeight() * $newHeight;
}
// if image is smaller keep ratio
// e.g. when a square image is requested (200x200) and the original image is smaller (150x100)
// it still returns a squared image (100x100)
if ($parameters['forceRatio']) {
if ($newWidth > $size->getWidth()) {
list($newHeight, $newWidth) = $this->getSizeInSameRatio($newHeight, $newWidth, $size->getWidth());
}
if ($newHeight > $size->getHeight()) {
list($newWidth, $newHeight) = $this->getSizeInSameRatio($newWidth, $newHeight, $size->getHeight());
}
}
return [$newWidth, $newHeight];
}
示例2: create
public function create(BoxInterface $size, ColorInterface $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
if ($color === null) {
$palette = self::$rgb;
$color = '#ffffff';
$alpha = 0;
} else {
$palette = $color->getPalette();
$alpha = $color->getAlpha() / 100;
}
try {
$pixel = new \GmagickPixel((string) $color);
$pixel->setcolorvalue(\Gmagick::COLOR_OPACITY, $alpha);
// does nothing as of Gmagick 1.1.7RC2. Background will be fully opaque.
$magick = new \Gmagick();
$magick->newimage($width, $height, $pixel->getcolor(false));
$magick->setimagecolorspace(\Gmagick::COLORSPACE_TRANSPARENT);
$magick->setimagebackgroundcolor($pixel);
return new RImage($magick, $palette, self::$emptyBag, array($width, $height));
} catch (\Exception $e) {
throw new \Imagine\Exception\RuntimeException("Gmagick: could not create empty image. {$e->getMessage()}", $e->getCode(), $e);
}
}
示例3: create
/**
* (non-PHPdoc)
* @see Imagine\Image\ImagineInterface::create()
*/
public function create(BoxInterface $size, Color $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
$color = null !== $color ? $color : new Color('fff');
try {
$pixel = new \ImagickPixel((string) $color);
$pixel->setColorValue(
\Imagick::COLOR_OPACITY,
number_format(abs(round($color->getAlpha() / 100, 1)), 1)
);
$imagick = new \Imagick();
$imagick->newImage($width, $height, $pixel);
$imagick->setImageMatte(true);
$imagick->setImageBackgroundColor($pixel);
$pixel->clear();
$pixel->destroy();
return new Image($imagick);
} catch (\ImagickException $e) {
throw new RuntimeException(
'Could not create empty image', $e->getCode(), $e
);
}
}
示例4: create
/**
* (non-PHPdoc)
* @see Imagine\Image\ImagineInterface::create()
*/
public function create(BoxInterface $size, Color $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
$resource = imagecreatetruecolor($width, $height);
if (false === $resource) {
throw new RuntimeException('Create operation failed');
}
$color = $color ? $color : new Color('fff');
if (false === imagealphablending($resource, false) || false === imagesavealpha($resource, true)) {
throw new RuntimeException('Could not set alphablending, savealpha and antialias values');
}
if (function_exists('imageantialias')) {
imageantialias($resource, true);
}
$index = imagecolorallocatealpha($resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100));
if (false === $index) {
throw new RuntimeException('Unable to allocate color');
}
if (false === imagefill($resource, 0, 0, $index)) {
throw new RuntimeException('Could not set background color fill');
}
if ($color->getAlpha() >= 95) {
imagecolortransparent($resource, $index);
}
return new Image($resource, $this);
}
示例5: create
/**
* {@inheritdoc}
*/
public function create(BoxInterface $size, ColorInterface $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
$resource = imagecreatetruecolor($width, $height);
if (false === $resource) {
throw new RuntimeException('Create operation failed');
}
$palette = null !== $color ? $color->getPalette() : new RGB();
$color = $color ? $color : $palette->color('fff');
if (!$color instanceof RGBColor) {
throw new InvalidArgumentException('GD driver only supports RGB colors');
}
$index = imagecolorallocatealpha($resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100));
if (false === $index) {
throw new RuntimeException('Unable to allocate color');
}
if (false === imagefill($resource, 0, 0, $index)) {
throw new RuntimeException('Could not set background color fill');
}
if ($color->getAlpha() >= 95) {
imagecolortransparent($resource, $index);
}
return $this->wrap($resource, $palette);
}
示例6: create
/**
* {@inheritdoc}
*/
public function create(BoxInterface $size, ColorInterface $color = null)
{
$width = $size->getWidth();
$height = $size->getHeight();
$palette = null !== $color ? $color->getPalette() : new RGB();
$color = null !== $color ? $color : $palette->color('fff');
try {
$gmagick = new \Gmagick();
// Gmagick does not support creation of CMYK GmagickPixel
// see https://bugs.php.net/bug.php?id=64466
if ($color instanceof CMYKColor) {
$switchPalette = $palette;
$palette = new RGB();
$pixel = new \GmagickPixel($palette->color((string) $color));
} else {
$switchPalette = null;
$pixel = new \GmagickPixel((string) $color);
}
if ($color->getPalette()->supportsAlpha() && $color->getAlpha() < 100) {
throw new NotSupportedException('alpha transparency is not supported');
}
$gmagick->newimage($width, $height, $pixel->getcolor(false));
$gmagick->setimagecolorspace(\Gmagick::COLORSPACE_TRANSPARENT);
$gmagick->setimagebackgroundcolor($pixel);
$image = new Image($gmagick, $palette, new MetadataBag());
if ($switchPalette) {
$image->usePalette($switchPalette);
}
return $image;
} catch (\GmagickException $e) {
throw new RuntimeException('Could not create empty image', $e->getCode(), $e);
}
}
示例7: scaleSize
/**
* Modifies $width or $height (if one of them is empty) based on $box ratio.
* @param number|null $width Desired width
* @param number|null $height Desired height
* @param \Imagine\Image\BoxInterface $box Original box for a reference
*/
public static function scaleSize(&$width, &$height, \Imagine\Image\BoxInterface $box)
{
if (!$width || !$height) {
$r = $box->getWidth() / $box->getHeight();
if ($width) {
$height = floor($width / $r);
} else {
$width = floor($height * $r);
}
}
}
示例8: calculateRatio
/**
* @param BoxInterface $imageSize
* @param array $settings
* @return array|mixed
*/
private function calculateRatio(BoxInterface $imageSize, array $settings)
{
$ratio = array();
if ($settings['height']) {
$ratio[] = $settings['height'] / $imageSize->getHeight();
}
if ($settings['width']) {
$ratio[] = $settings['width'] / $imageSize->getWidth();
}
$ratio = max($ratio);
return $ratio;
}
示例9: crop
public function crop(PointInterface $start, BoxInterface $size)
{
if ($this->image === null) {
$this->load();
}
$width = $size->getWidth();
$height = $size->getHeight();
try {
$this->image->getGmagick()->cropimage($width, $height, $start->getX(), $start->getY());
} catch (\GmagickException $e) {
throw new \Imagine\Exception\RuntimeException("Gmagick: Crop operation failed. {$e->getMessage()}", $e->getCode(), $e);
}
$this->size = array($width, $height);
return $this;
}
示例10: testGrayscaling
/**
* @covers \Imagine\Filter\Advanced\Grayscale::apply
*
* @dataProvider getDataSet
*
* @param \Imagine\Image\BoxInterface $size
* @param \Imagine\Image\Color $color
* @param \Imagine\Image\Color $filteredColor
*/
public function testGrayscaling(BoxInterface $size, Color $color, Color $filteredColor)
{
$image = $this->getImage();
$imageWidth = $size->getWidth();
$imageHeight = $size->getHeight();
$size = $this->getMock('Imagine\\Image\\BoxInterface');
$size->expects($this->exactly($imageWidth + 1))->method('getWidth')->will($this->returnValue($imageWidth));
$size->expects($this->exactly($imageWidth * ($imageHeight + 1)))->method('getHeight')->will($this->returnValue($imageHeight));
$image->expects($this->any())->method('getSize')->will($this->returnValue($size));
$image->expects($this->exactly($imageWidth * $imageHeight))->method('getColorAt')->will($this->returnValue($color));
$draw = $this->getDrawer();
$draw->expects($this->exactly($imageWidth * $imageHeight))->method('dot')->with($this->isInstanceOf('Imagine\\Image\\Point'), $this->equalTo($filteredColor));
$image->expects($this->exactly($imageWidth * $imageHeight))->method('draw')->will($this->returnValue($draw));
$filter = new Grayscale();
$this->assertSame($image, $filter->apply($image));
}
示例11: create
/**
* {@inheritdoc}
*/
public function create(BoxInterface $size, ColorInterface $color = null)
{
if (null !== $color) {
throw new InvalidArgumentException('Imagine SVG does not support colors');
}
$document = new \DOMDocument();
$svg = $document->createElementNS('http://www.w3.org/2000/svg', 'svg');
$svg->setAttribute('version', '1.1');
if ($size->getWidth()) {
$svg->setAttribute('width', $size->getWidth());
}
if ($size->getHeight()) {
$svg->setAttribute('height', $size->getHeight());
}
$document->appendChild($svg);
return new Image($document, new MetadataBag());
}
示例12: positionLayerOnCanvas
/**
* Calculates coordinates of top left corner of layer that should be positioned on canvas using given
* vertical and horizontal positions.
*
* @param BoxInterface $canvas_size Size of canvas
* @param BoxInterface $layer_size Size of layer
* @param string $horizontal_position left|center|right
* @param string $vertical_position top|center|bottom
* @param BoxInterface|null $padding Optional padding between canvas and layer sides.
*
* @return Point
*/
public static function positionLayerOnCanvas(BoxInterface $canvas_size, BoxInterface $layer_size, $horizontal_position, $vertical_position, BoxInterface $padding = null)
{
$dest_x = $dest_y = 0;
$original_width = $canvas_size->getWidth();
$original_height = $canvas_size->getHeight();
if ($padding instanceof BoxInterface) {
$delta_x = $padding->getWidth();
$delta_y = $padding->getHeight();
} else {
$delta_x = 0;
$delta_y = 0;
}
$new_wt_width = $layer_size->getWidth();
$new_wt_height = $layer_size->getHeight();
if ($new_wt_width + $delta_x > $original_width) {
$new_wt_height = $new_wt_height * ($original_width - $delta_x) / $new_wt_width;
$new_wt_width = $original_width - $delta_x;
}
if ($new_wt_height > $original_height) {
$new_wt_width = $new_wt_width * ($original_height - $delta_y) / $new_wt_height;
$new_wt_height = $original_height - $delta_y;
}
if ($vertical_position == 'top') {
$dest_y = $delta_y;
} elseif ($vertical_position == 'center') {
$dest_y = (int) (($original_height - $new_wt_height) / 2);
} elseif ($vertical_position == 'bottom') {
$dest_y = $original_height - $new_wt_height - $delta_y;
}
if ($horizontal_position == 'left') {
$dest_x = $delta_x;
} elseif ($horizontal_position == 'center') {
$dest_x = (int) (($original_width - $new_wt_width) / 2);
} elseif ($horizontal_position == 'right') {
$dest_x = $original_width - $new_wt_width - $delta_x;
}
if ($dest_x < 1) {
$dest_x = 0;
}
if ($dest_y < 1) {
$dest_y = 0;
}
return new Point($dest_x, $dest_y);
}
示例13: getPathFilter
/**
* @param array | BoxInterface $data
*
* @throws \Zf2FileUploader\Filter\Thumbnail\Exception\InvalidArgumentException
* @return PathFilter
*/
public function getPathFilter($data)
{
if ($data instanceof BoxInterface) {
$width = $data->getWidth();
$height = $data->getHeight();
} else {
if (func_num_args() > 1) {
list($width, $height) = func_get_args();
} else {
if (is_array($data) && array_key_exists('width', $data) && array_key_exists('height', $data) || preg_match('/^(?P<width>[0-9]+)x(?P<height>[0-9]+)$/', $data, $data)) {
$width = $data['width'];
$height = $data['height'];
} else {
throw new Exception\InvalidArgumentException("Could not retrieve width and height from data");
}
}
}
return new PathFilter($width, $height);
}
示例14: isEqualTo
/**
* {@inheritdoc}
*/
public function isEqualTo($coordinates)
{
if ($coordinates instanceof BoxInterface) {
$coordinates = new static($coordinates, new Point(0, 0), $coordinates);
}
if (!$coordinates instanceof ResizeCoordinatesInterface) {
throw new \InvalidArgumentException(sprintf('$coordinates must be an instance of ResizeCoordinatesInterface or BoxInterface, "%s" given', get_class($coordinates)));
}
/* @var ResizeCoordinatesInterface $coordinates */
return $this->cropStart->getX() === $coordinates->getCropStart()->getX() && $this->cropStart->getY() === $coordinates->getCropStart()->getY() && $this->cropSize->getWidth() === $coordinates->getCropSize()->getWidth() && $this->cropSize->getHeight() === $coordinates->getCropSize()->getHeight() && $this->size->getWidth() === $coordinates->getSize()->getWidth() && $this->size->getHeight() === $coordinates->getSize()->getHeight();
}
示例15: resize
/**
* resizes image using a focal point
* @param ImageInterface $image an Imagine image
* @param BoxInterface $size size of resized imaga
* @param FocalPoint $focal focal point to use for size
* @param string $filter Imagine filter to use
* @return ImageInterface resized image
*/
public function resize(ImageInterface $image, BoxInterface $size, FocalPointInterface $focal, $filter = ImageInterface::FILTER_UNDEFINED)
{
$origSize = $image->getSize();
$origWidth = $origSize->getWidth();
$origHeight = $origSize->getHeight();
$width = $size->getWidth();
$height = $size->getHeight();
$ratioHeight = $origHeight / $height;
$ratioWidth = $origWidth / $width;
if ($ratioHeight < $ratioWidth) {
list($cropX1, $cropX2) = $this->calculateCrop($origWidth, $width, $focal->getX(), $ratioHeight);
$cropY1 = 0;
$cropY2 = $origHeight;
} else {
list($cropY1, $cropY2) = $this->calculateCrop($origHeight, $height, -$focal->getY(), $ratioWidth);
$cropX1 = 0;
$cropX2 = $origWidth;
}
$image->crop(new Point($cropX1, $cropY1), new Box($cropX2 - $cropX1, $cropY2 - $cropY1))->resize(new Box($width, $height, $filter));
return $image;
}