【问题标题】:php image from portrait to landscapephp图像从纵向到横向
【发布时间】:2017-06-08 13:45:34
【问题描述】:

我正在使用 ImageResize php 库来调整图像大小和裁剪图像。

但是现在,我面临一个我找不到解决方案的问题,就是将图像从纵向转换为横向。

当然我可以使图像变形,但我想实现这样的目标:

Example

所以,关键是如何将肖像图像放在风景背景中,以免变形。

有什么想法吗?

干杯。

【问题讨论】:

  • 发布一些可以找到您正在使用的库的链接。

标签: php landscape portrait


【解决方案1】:

您可以像这样在普通 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);
        }
    }

【讨论】:

  • 它就像宽屏电影。如果将横向转换为纵向,如果要保留整个图像,则上方和下方都会出现黑条。同样反过来,从肖像到风景,左右两边都会有黑色。但是,如果要填充黑色空间,则需要将纵向图像加宽到横向宽度,同时保持比例。然而,这意味着肖像图像的顶部/底部将被切断。这就是为什么宽屏广告“看看你错过了什么!”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 2014-02-07
  • 2020-02-21
相关资源
最近更新 更多