【发布时间】:2017-06-08 13:45:34
【问题描述】:
我正在使用 ImageResize php 库来调整图像大小和裁剪图像。
但是现在,我面临一个我找不到解决方案的问题,就是将图像从纵向转换为横向。
当然我可以使图像变形,但我想实现这样的目标:
所以,关键是如何将肖像图像放在风景背景中,以免变形。
有什么想法吗?
干杯。
【问题讨论】:
-
发布一些可以找到您正在使用的库的链接。
我正在使用 ImageResize php 库来调整图像大小和裁剪图像。
但是现在,我面临一个我找不到解决方案的问题,就是将图像从纵向转换为横向。
当然我可以使图像变形,但我想实现这样的目标:
所以,关键是如何将肖像图像放在风景背景中,以免变形。
有什么想法吗?
干杯。
【问题讨论】:
您可以像这样在普通 PHP 中手动调整大小:
//define image path
$filename="image.jpg";
// Load the image
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
//and save it on your server...
file_put_contents("myNEWimage.jpg",$rotate);
我在 Github 上有一个图像类(请注意不要旋转),它具有调整大小和裁剪功能。那里的逻辑基本上保持纵横比正确并裁剪掉重叠的东西。旋转后你可以做类似的事情:
https://github.com/delboy1978uk/image/blob/master/src/Image.php#L163-L180
public function resizeAndCrop($width,$height)
{
$target_ratio = $width / $height;
$actual_ratio = $this->getWidth() / $this->getHeight();
if($target_ratio == $actual_ratio){
// Scale to size
$this->resize($width,$height);
} elseif($target_ratio > $actual_ratio) {
// Resize to width, crop extra height
$this->resizeToWidth($width);
$this->crop($width,$height,true);
} else {
// Resize to height, crop additional width
$this->resizeToHeight($height);
$this->crop($width,$height,true);
}
}
【讨论】: