本文整理汇总了PHP中Imagine\Image\ImageInterface::resize方法的典型用法代码示例。如果您正苦于以下问题:PHP ImageInterface::resize方法的具体用法?PHP ImageInterface::resize怎么用?PHP ImageInterface::resize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Imagine\Image\ImageInterface
的用法示例。
在下文中一共展示了ImageInterface::resize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: resize
/**
* Re-sizes the image. If $height is not specified, it will default to $width, creating a square.
*
* @param int $targetWidth
* @param int|null $targetHeight
*
* @return Image
*/
public function resize($targetWidth, $targetHeight = null)
{
$this->normalizeDimensions($targetWidth, $targetHeight);
if ($this->_isAnimatedGif) {
$this->_image->layers()->coalesce();
// Create a new image instance to avoid object references messing up our dimensions.
$newSize = new \Imagine\Image\Box($targetWidth, $targetHeight);
$gif = $this->_instance->create($newSize);
$gif->layers()->remove(0);
foreach ($this->_image->layers() as $layer) {
$resizedLayer = $layer->resize($newSize, $this->_getResizeFilter());
$gif->layers()->add($resizedLayer);
// Let's update dateUpdated in case this is going to take awhile.
if ($index = craft()->assetTransforms->getActiveTransformIndexModel()) {
craft()->assetTransforms->storeTransformIndexData($index);
}
}
$this->_image = $gif;
} else {
if (craft()->images->isImagick()) {
$this->_image->smartResize(new \Imagine\Image\Box($targetWidth, $targetHeight), (bool) craft()->config->get('preserveImageColorProfiles'), $this->_quality);
} else {
$this->_image->resize(new \Imagine\Image\Box($targetWidth, $targetHeight), $this->_getResizeFilter());
}
}
return $this;
}
示例2: resize
/**
* Re-sizes the image. If $height is not specified, it will default to $width, creating a square.
*
* @param int $targetWidth
* @param int|null $targetHeight
*
* @return Image
*/
public function resize($targetWidth, $targetHeight = null)
{
$this->_normalizeDimensions($targetWidth, $targetHeight);
if ($this->_isAnimatedGif)
{
// Create a new image instance to avoid object references messing up our dimensions.
$newSize = new \Imagine\Image\Box($targetWidth, $targetHeight);
$gif = $this->_instance->create($newSize);
$gif->layers()->remove(0);
foreach ($this->_image->layers() as $layer)
{
$resizedLayer = $layer->resize($newSize, $this->_getResizeFilter());
$gif->layers()->add($resizedLayer);
}
$this->_image = $gif;
}
else
{
$this->_image->resize(new \Imagine\Image\Box($targetWidth, $targetHeight), $this->_getResizeFilter());
}
return $this;
}
示例3: imageResize
/**
* @return null|string
*/
public function imageResize()
{
$this->openImage();
$width = $this->imageWidth;
$height = $this->imageHeight;
$maxWidth = $this->imageMaxWidth;
$maxHeight = $this->imageMaxHeight;
if ($maxWidth === null && $maxHeight !== null) {
$imageBox = $this->image->getSize()->heighten($maxHeight);
} elseif ($maxHeight === null && $maxWidth !== null) {
$imageBox = $this->image->getSize()->widen($maxWidth);
} elseif ($maxHeight !== null && $maxWidth !== null) {
$imageBox = $this->image->getSize()->heighten($maxHeight)->widen($maxWidth);
} elseif ($width !== null && $height !== null) {
$imageBox = new \Imagine\Image\Box($width, $height);
} elseif ($width === null && $height !== null) {
$imageBox = new \Imagine\Image\Box($this->image->getSize()->getWidth(), $height);
} elseif ($width !== null && $height === null) {
$imageBox = new \Imagine\Image\Box($width, $this->image->getSize()->getHeight());
} else {
$imageBox = new \Imagine\Image\Box($this->image->getSize()->getWidth(), $this->image->getSize()->getHeight());
}
$this->image = $this->image->resize($imageBox);
return $this;
}
示例4: apply
public function apply(ImageInterface &$image, ImagineInterface $imagine)
{
$box = $image->getSize();
$width = Helper::percentValue($this->_width, $box->getWidth());
$height = Helper::percentValue($this->_height, $box->getHeight());
// no upscale
if ($box->getWidth() <= $width && $box->getHeight() <= $height) {
return;
}
Helper::scaleSize($width, $height, $box);
$image->resize(new Box($width, $height));
}
示例5: performResize
/**
* Performs resize of the image. Imagine component is used.
*
* @param ImageInterface $imagine
* @param $resize
* @param bool $crop
*
* @return BoxInterface
*/
private function performResize($imagine, $resize, $crop = true)
{
$box = $imagine->getSize();
list($width, $height) = Utils::getDimension($resize);
$box = $box->scale(max($width / $box->getWidth(), $height / $box->getHeight()));
$imagine->resize($box);
if ($crop) {
$point = new Point(($box->getWidth() - $width) / 2, ($box->getHeight() - $height) / 2);
$imagine->crop($point, new Box($width, $height));
}
return $box;
}
示例6: imageResize
/**
* Command image resize
*
* @param int $width
* @param int $height
*
* @return void
*/
protected function imageResize($width, $height)
{
$width = (int) $width;
$height = (int) $height;
if ($width <= 0) {
throw new BadMethodCallException('Invalid parameter width for command "resize"');
}
if ($height <= 0) {
throw new BadMethodCallException('Invalid parameter height for command "resize"');
}
$this->image->resize(new Box($width, $height));
}
示例7: execute
/**
* {@inheritdoc}
*/
public function execute(ImageInterface &$image, $parameters)
{
$size = $image->getSize();
$retina = isset($parameters['retina']) && $parameters['retina'] != 'false' ? 2 : 1;
$newWidth = isset($parameters['x']) ? intval($parameters['x']) * $retina : null;
$newHeight = isset($parameters['y']) ? intval($parameters['y']) * $retina : null;
if ($newHeight == null) {
$newHeight = $size->getHeight() / $size->getWidth() * $newWidth;
}
if ($newWidth == null) {
$newWidth = $size->getWidth() / $size->getHeight() * $newHeight;
}
$image->resize(new Box($newWidth, $newHeight));
}
示例8: resize
/**
* Re-sizes the image. If $height is not specified, it will default to $width, creating a square.
*
* @param int $targetWidth
* @param int|null $targetHeight
*
* @return Image
*/
public function resize($targetWidth, $targetHeight = null)
{
$this->normalizeDimensions($targetWidth, $targetHeight);
if ($this->_isAnimatedGif) {
// Create a new image instance to avoid object references messing up our dimensions.
$newSize = new \Imagine\Image\Box($targetWidth, $targetHeight);
$gif = $this->_instance->create($newSize);
$gif->layers()->remove(0);
foreach ($this->_image->layers() as $layer) {
$resizedLayer = $layer->resize($newSize, $this->_getResizeFilter());
$gif->layers()->add($resizedLayer);
}
$this->_image = $gif;
} else {
if (craft()->images->isImagick()) {
$this->_image->smartResize(new \Imagine\Image\Box($targetWidth, $targetHeight), (bool) craft()->config->get('preserveImageColorProfiles'), $this->_quality);
} else {
$this->_image->resize(new \Imagine\Image\Box($targetWidth, $targetHeight), $this->_getResizeFilter());
}
}
return $this;
}
示例9: applyResize
/**
* Process image resizing, with borders or cropping. If $dest_width and $dest_height
* are both null, no resize is performed.
*
* @param ImagineInterface $imagine the Imagine instance
* @param ImageInterface $image the image to process
* @param int $dest_width the required width
* @param int $dest_height the required height
* @param int $resize_mode the resize mode (crop / bands / keep image ratio)p
* @param string $bg_color the bg_color used for bands
* @param bool $allow_zoom if true, image may be zoomed to matchrequired size. If false, image is not zoomed.
* @return ImageInterface the resized image.
*/
protected function applyResize(ImagineInterface $imagine, ImageInterface $image, $dest_width, $dest_height, $resize_mode, $bg_color, $allow_zoom = false)
{
if (!(is_null($dest_width) && is_null($dest_height))) {
$width_orig = $image->getSize()->getWidth();
$height_orig = $image->getSize()->getHeight();
$ratio = $width_orig / $height_orig;
if (is_null($dest_width)) {
$dest_width = $dest_height * $ratio;
}
if (is_null($dest_height)) {
$dest_height = $dest_width / $ratio;
}
if (is_null($resize_mode)) {
$resize_mode = self::KEEP_IMAGE_RATIO;
}
$width_diff = $dest_width / $width_orig;
$height_diff = $dest_height / $height_orig;
$delta_x = $delta_y = $border_width = $border_height = 0;
if ($width_diff > 1 && $height_diff > 1) {
$resize_width = $width_orig;
$resize_height = $height_orig;
// When cropping, be sure to always generate an image which is
// no smaller than the required size, zooming it if required.
if ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
if ($allow_zoom) {
if ($width_diff > $height_diff) {
$resize_width = $dest_width;
$resize_height = intval($height_orig * $dest_width / $width_orig);
$delta_y = ($resize_height - $dest_height) / 2;
} else {
$resize_height = $dest_height;
$resize_width = intval($width_orig * $resize_height / $height_orig);
$delta_x = ($resize_width - $dest_width) / 2;
}
} else {
// No zoom : final image may be smaller than the required size.
$dest_width = $resize_width;
$dest_height = $resize_height;
}
}
} elseif ($width_diff > $height_diff) {
// Image height > image width
$resize_height = $dest_height;
$resize_width = intval($width_orig * $resize_height / $height_orig);
if ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$resize_width = $dest_width;
$resize_height = intval($height_orig * $dest_width / $width_orig);
$delta_y = ($resize_height - $dest_height) / 2;
} elseif ($resize_mode != self::EXACT_RATIO_WITH_BORDERS) {
$dest_width = $resize_width;
}
} else {
// Image width > image height
$resize_width = $dest_width;
$resize_height = intval($height_orig * $dest_width / $width_orig);
if ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$resize_height = $dest_height;
$resize_width = intval($width_orig * $resize_height / $height_orig);
$delta_x = ($resize_width - $dest_width) / 2;
} elseif ($resize_mode != self::EXACT_RATIO_WITH_BORDERS) {
$dest_height = $resize_height;
}
}
$image->resize(new Box($resize_width, $resize_height));
if ($resize_mode == self::EXACT_RATIO_WITH_BORDERS) {
$border_width = intval(($dest_width - $resize_width) / 2);
$border_height = intval(($dest_height - $resize_height) / 2);
$canvas = new Box($dest_width, $dest_height);
return $imagine->create($canvas, $bg_color)->paste($image, new Point($border_width, $border_height));
} elseif ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$image->crop(new Point($delta_x, $delta_y), new Box($dest_width, $dest_height));
}
}
return $image;
}
示例10: resizeImageByHeight
/**
* @param ImageInterface $image
* @param $requestHeight
*/
protected function resizeImageByHeight($image, $requestHeight)
{
$imageSizeBox = $image->getSize();
$resizeBox = $imageSizeBox->heighten($requestHeight);
$image->resize($resizeBox);
}
示例11: applyResize
/**
* Process image resizing, with borders or cropping. If $dest_width and $dest_height
* are both null, no resize is performed.
*
* @param ImagineInterface $imagine the Imagine instance
* @param ImageInterface $image the image to process
* @param int $dest_width the required width
* @param int $dest_height the required height
* @param int $resize_mode the resize mode (crop / bands / keep image ratio)p
* @param string $bg_color the bg_color used for bands
* @return ImageInterface the resized image.
*/
protected function applyResize(ImagineInterface $imagine, ImageInterface $image, $dest_width, $dest_height, $resize_mode, $bg_color)
{
if (!(is_null($dest_width) && is_null($dest_height))) {
$width_orig = $image->getSize()->getWidth();
$height_orig = $image->getSize()->getHeight();
if (is_null($dest_width)) {
$dest_width = $width_orig;
}
if (is_null($dest_height)) {
$dest_height = $height_orig;
}
if (is_null($resize_mode)) {
$resize_mode = self::KEEP_IMAGE_RATIO;
}
$width_diff = $dest_width / $width_orig;
$height_diff = $dest_height / $height_orig;
$delta_x = $delta_y = $border_width = $border_height = 0;
if ($width_diff > 1 && $height_diff > 1) {
$next_width = $width_orig;
$next_height = $height_orig;
$dest_width = $resize_mode == self::EXACT_RATIO_WITH_BORDERS ? $dest_width : $next_width;
$dest_height = $resize_mode == self::EXACT_RATIO_WITH_BORDERS ? $dest_height : $next_height;
} elseif ($width_diff > $height_diff) {
// Image height > image width
$next_height = $dest_height;
$next_width = intval($width_orig * $next_height / $height_orig);
if ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$next_width = $dest_width;
$next_height = intval($height_orig * $dest_width / $width_orig);
$delta_y = ($next_height - $dest_height) / 2;
} elseif ($resize_mode != self::EXACT_RATIO_WITH_BORDERS) {
$dest_width = $next_width;
}
} else {
// Image width > image height
$next_width = $dest_width;
$next_height = intval($height_orig * $dest_width / $width_orig);
if ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$next_height = $dest_height;
$next_width = intval($width_orig * $next_height / $height_orig);
$delta_x = ($next_width - $dest_width) / 2;
} elseif ($resize_mode != self::EXACT_RATIO_WITH_BORDERS) {
$dest_height = $next_height;
}
}
$image->resize(new Box($next_width, $next_height));
if ($resize_mode == self::EXACT_RATIO_WITH_BORDERS) {
$border_width = intval(($dest_width - $next_width) / 2);
$border_height = intval(($dest_height - $next_height) / 2);
$canvas = new Box($dest_width, $dest_height);
return $imagine->create($canvas, $bg_color)->paste($image, new Point($border_width, $border_height));
} elseif ($resize_mode == self::EXACT_RATIO_WITH_CROP) {
$image->crop(new Point($delta_x, $delta_y), new Box($dest_width, $dest_height));
}
}
return $image;
}
示例12: resizeExact
/**
* Resize an image to an exact width and height.
*
* @param ImageInterface $image
* @param string $width - The image's new width.
* @param string $height - The image's new height.
* @return ImageInterface
*/
protected function resizeExact(ImageInterface $image, $width, $height)
{
return $image->resize(new Box($width, $height));
}
示例13: expandImage
/**
* Расширяет маленькую картинку по заданной стратегии
*
* @param \Imagine\Image\ImageInterface $image
* @param int $widthFormat
* @param int $heightFormat
* @param int $mode
*
* @return \Imagine\Image\ImageInterface
*/
protected static function expandImage($image, $widthFormat, $heightFormat, $mode)
{
$size = $image->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
if ($width < $widthFormat || $height < $heightFormat) {
// расширяю картинку
if ($mode == self::MODE_THUMBNAIL_CUT) {
if ($width < $widthFormat && $height >= $heightFormat) {
$size = $size->widen($widthFormat);
} else {
if ($width >= $widthFormat && $height < $heightFormat) {
$size = $size->heighten($heightFormat);
} else {
if ($width < $widthFormat && $height < $heightFormat) {
// определяю как расширять по ширине или по высоте
if ($width / $widthFormat < $height / $heightFormat) {
$size = $size->widen($widthFormat);
} else {
$size = $size->heighten($heightFormat);
}
}
}
}
$image->resize($size);
} else {
if ($width < $widthFormat && $height >= $heightFormat) {
$size = $size->heighten($heightFormat);
} else {
if ($width >= $widthFormat && $height < $heightFormat) {
$size = $size->widen($widthFormat);
} else {
if ($width < $widthFormat && $height < $heightFormat) {
// определяю как расширять по ширине или по высоте
if ($width / $widthFormat < $height / $heightFormat) {
$size = $size->heighten($heightFormat);
} else {
$size = $size->widen($widthFormat);
}
}
}
}
$image->resize($size);
}
}
return $image;
}
示例14: save
/**
* Save image
* @return ImageInterface
*/
public function save()
{
if (file_exists($this->_pathObjectFile)) {
$this->_url = $this->_urlObjectFile;
return true;
} elseif (file_exists($this->_pathObjectFileOrigin)) {
$this->_image = Image::getImagine()->open($this->_pathObjectFileOrigin);
} elseif (file_exists($this->_pathObjectPlaceholder)) {
$this->_url = $this->_urlObjectPlaceholder;
return true;
} elseif (file_exists($this->_pathObjectPlaceholderOrigin)) {
$this->_image = Image::getImagine()->open($this->_pathObjectPlaceholderOrigin);
$this->_pathObjectFile = $this->_pathObjectPlaceholder;
$this->_urlObjectFile = $this->_urlObjectPlaceholder;
} elseif (file_exists($this->_pathRootPlaceholder)) {
$this->_url = $this->_urlRootPlaceholder;
return true;
} elseif (file_exists($this->_pathRootPlaceholderOrigin)) {
$this->_image = Image::getImagine()->open($this->_pathRootPlaceholderOrigin);
$this->_pathObjectFile = $this->_pathRootPlaceholder;
$this->_urlObjectFile = $this->_urlRootPlaceholder;
}
if ($this->_image) {
if (!$this->width || !$this->height) {
$ratio = $this->_image->getSize()->getWidth() / $this->_image->getSize()->getHeight();
if ($this->width) {
$this->height = ceil($this->width / $ratio);
} else {
$this->width = ceil($this->height * $ratio);
}
$box = new Box($this->width, $this->height);
$this->_image->resize($box);
$this->_image->crop(new Point(0, 0), $box);
} else {
$box = new Box($this->width, $this->height);
$size = $this->_image->getSize();
if ($size->getWidth() <= $box->getWidth() && $size->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) {
} else {
$this->_image = $this->_image->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND);
// calculate points
$size = $this->_image->getSize();
$startX = 0;
$startY = 0;
if ($size->getWidth() < $this->width) {
$startX = ceil($this->width - $size->getWidth()) / 2;
}
if ($size->getHeight() < $this->height) {
$startY = ceil($this->height - $size->getHeight()) / 2;
}
// create empty image to preserve aspect ratio of thumbnail
$thumb = Image::getImagine()->create($box, new Color('FFF', 100));
$thumb->paste($this->_image, new Point($startX, $startY));
$this->_image = $thumb;
}
}
$this->_image->save($this->_pathObjectFile);
$this->_url = $this->_urlObjectFile;
return true;
}
return false;
}
示例15: apply
/**
* {@inheritdoc}
*/
public function apply(ImageInterface $image)
{
return $image->resize($this->size);
}