PHP GD 图像调整大小使肖像图像变成风景

发布于 2025-01-08 04:32:58 字数 3753 浏览 3 评论 0原文

我一直在调整图像大小并让我的代码正常工作,但是当我上传肖像图像时,PHP GD 会正确调整其大小,但使图像横向并“旋转”90 度。

你能帮忙吗?

谢谢 Anto

肖像示例:http://ukispreview.sugarcaneweb.co.uk/images/view/file/4209e7ab1677a0ebfe6e053bf86a1857.jpg

示例景观:http://ukispreview.sugarcaneweb.co.uk/images/view/file/74818c9050e3d73bafc74b8c46077395.jpg

/***
 * Resize the image that we have opened up
 */
public function resizeImage($newWidth, $newHeight, $option="auto")
{
    // work out the optimal width and height based on wether the image is landscape or portrait
    $optionArray = $this->getDimensions($newWidth, $newHeight, strtolower($option));

    $optimalWidth  = $optionArray['optimalWidth'];
    $optimalHeight = $optionArray['optimalHeight'];

    // *** Resample - create image canvas of x, y size
    $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
    imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
}


/***
 * Returns the optimal dimensions for the new image
 */
private function getDimensions($newWidth, $newHeight, $option)
{
    switch ($option)
    {
        case 'exact':
            $optimalWidth = $newWidth;
            $optimalHeight= $newHeight;
            break;
        case 'portrait':
            $optimalWidth = $this->getSizeByFixedHeight($newHeight);
            $optimalHeight= $newHeight;
            break;
        case 'landscape':
            $optimalWidth = $newWidth;
            $optimalHeight= $this->getSizeByFixedWidth($newWidth);
            break;
        case 'auto':
            $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
            $optimalWidth = $optionArray['optimalWidth'];
            $optimalHeight = $optionArray['optimalHeight'];
            break;
    }

    return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}


/***
 * Get the optimal width based on the height of the image
 */
private function getSizeByFixedHeight($newHeight)
{
    $ratio    = $this->width / $this->height;
    $newWidth = $newHeight * $ratio;

    return $newWidth;
}


/***
 * Get the optimal height based on the width of the image
 */
private function getSizeByFixedWidth($newWidth)
{
    $ratio     = $this->height / $this->width;
    $newHeight = $newWidth * $ratio;

    return $newHeight;
}


/***
 * Get the optimal width/height when the image rotation is unknown
 */
private function getSizeByAuto($newWidth, $newHeight)
{
    if($this->height < $this->width) {
        // landscape image
        $optimalWidth = $newWidth;
        $optimalHeight= $this->getSizeByFixedWidth($newWidth);
    } elseif($this->height > $this->width) {
        // portrait image
        $optimalWidth = $this->getSizeByFixedHeight($newHeight);
        $optimalHeight= $newHeight;
    } else {
        // the image is a square, what are we turning the square into
        if($newHeight < $newWidth) {
            $optimalWidth = $newWidth;
            $optimalHeight= $this->getSizeByFixedWidth($newWidth);
        } elseif($newHeight > $newWidth) {
            $optimalWidth = $this->getSizeByFixedHeight($newHeight);
            $optimalHeight= $newHeight;
        } else {
            $optimalWidth = $newWidth;
            $optimalHeight= $newHeight;
        }
    }

    return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}

I've been playing with resizing images and have got my code working, but when I upload a Portrait image, PHP GD resizes it correctly but makes the image landscape and 'rotates' 90 degrees.

Could you please help?

Thanks
Anto

Example Portrait: http://ukispreview.sugarcaneweb.co.uk/images/view/file/4209e7ab1677a0ebfe6e053bf86a1857.jpg

Example Landscape: http://ukispreview.sugarcaneweb.co.uk/images/view/file/74818c9050e3d73bafc74b8c46077395.jpg

/***
 * Resize the image that we have opened up
 */
public function resizeImage($newWidth, $newHeight, $option="auto")
{
    // work out the optimal width and height based on wether the image is landscape or portrait
    $optionArray = $this->getDimensions($newWidth, $newHeight, strtolower($option));

    $optimalWidth  = $optionArray['optimalWidth'];
    $optimalHeight = $optionArray['optimalHeight'];

    // *** Resample - create image canvas of x, y size
    $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
    imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
}


/***
 * Returns the optimal dimensions for the new image
 */
private function getDimensions($newWidth, $newHeight, $option)
{
    switch ($option)
    {
        case 'exact':
            $optimalWidth = $newWidth;
            $optimalHeight= $newHeight;
            break;
        case 'portrait':
            $optimalWidth = $this->getSizeByFixedHeight($newHeight);
            $optimalHeight= $newHeight;
            break;
        case 'landscape':
            $optimalWidth = $newWidth;
            $optimalHeight= $this->getSizeByFixedWidth($newWidth);
            break;
        case 'auto':
            $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
            $optimalWidth = $optionArray['optimalWidth'];
            $optimalHeight = $optionArray['optimalHeight'];
            break;
    }

    return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}


/***
 * Get the optimal width based on the height of the image
 */
private function getSizeByFixedHeight($newHeight)
{
    $ratio    = $this->width / $this->height;
    $newWidth = $newHeight * $ratio;

    return $newWidth;
}


/***
 * Get the optimal height based on the width of the image
 */
private function getSizeByFixedWidth($newWidth)
{
    $ratio     = $this->height / $this->width;
    $newHeight = $newWidth * $ratio;

    return $newHeight;
}


/***
 * Get the optimal width/height when the image rotation is unknown
 */
private function getSizeByAuto($newWidth, $newHeight)
{
    if($this->height < $this->width) {
        // landscape image
        $optimalWidth = $newWidth;
        $optimalHeight= $this->getSizeByFixedWidth($newWidth);
    } elseif($this->height > $this->width) {
        // portrait image
        $optimalWidth = $this->getSizeByFixedHeight($newHeight);
        $optimalHeight= $newHeight;
    } else {
        // the image is a square, what are we turning the square into
        if($newHeight < $newWidth) {
            $optimalWidth = $newWidth;
            $optimalHeight= $this->getSizeByFixedWidth($newWidth);
        } elseif($newHeight > $newWidth) {
            $optimalWidth = $this->getSizeByFixedHeight($newHeight);
            $optimalHeight= $newHeight;
        } else {
            $optimalWidth = $newWidth;
            $optimalHeight= $newHeight;
        }
    }

    return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文