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

时间:2012-02-22 11:57:35

标签: php gd image-resizing

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

你能帮忙吗?

由于 安托

示例肖像: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);
}

0 个答案:

没有答案