PHP调整图像大小,有点工作

时间:2012-05-02 15:28:22

标签: php resize

这段代码已经给我提供了几个星期的问题,并且还没有接近解决它......

function create_thumbnail($source, $destination, $thumb_width){
    $extension = get_image_extension($source);
    $size = getimagesize($source);
    $imageWidth  = $newWidth  = $size[0];
    $imageHeight = $newheight = $size[1];

    if ($imageWidth > $thumb_width || $imageHeight > $thumb_width)
    {
        // Calculate the ratio
        $xscale = ($imageWidth/$thumb_width);
        $yscale = ($imageHeight/$thumb_width);
        $newWidth  = ($yscale > $xscale) ? round($imageWidth * (1/$yscale)) : round($imageWidth * (1/$xscale));
        $newHeight = ($yscale > $xscale) ? round($imageHeight * (1/$yscale)) : round($imageHeight * (1/$xscale));
    }

    $newImage = imagecreatetruecolor($newWidth, $newHeight);

    switch ($extension)
    {
        case 'jpeg':
        case 'jpg':
            $imageCreateFrom = 'imagecreatefromjpeg';
            $store = 'imagejpeg';
            break;

        case 'png':
            $imageCreateFrom = 'imagecreatefrompng';
            $store = 'imagepng';
            break;

        case 'gif':
            $imageCreateFrom = 'imagecreatefromgif';
            $store = 'imagegif';
            break;

        default:
            return false;
    }

    $container = $imageCreateFrom($source);
    imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
    return $store($newImage, $destination);
}

现在它工作正常,但这些调整大小的图像进入的空间是200(高度)乘225(宽度),有时我会得到一个高而瘦的图像和一些短而宽的图像让它们难以适应这个空间,理想情况下,如果我可以将这些图像调整大小或被剪掉,但是我不确定这是否可能......是吗?

如果那是不可能的,我想我正在调整这个代码,所以如果它的图像高而瘦,要调整高度为200,如果图像宽而短,则调整宽度为225 ...我希望这是有道理的。

无论如何,任何提示,建议或任何事情都会受到赞赏。

我试图运行mschr代码(在答案中如下)并且出现了这些错误

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/content/44/8713044/html/admin/Home.php on line 321

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 323

Warning: Cannot modify header information - headers already sent by (output started at /home/content/44/8713044/html/admin/include/header.php:7) in /home/content/44/8713044/html/admin/Home.php on line 325

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 329

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/content/44/8713044/html/admin/Home.php on line 334

2 个答案:

答案 0 :(得分:0)

这是我如何做到的; $ w和$ h是请求维度,$ nHeight和$ nWidth导致的暗淡 - 维持宽高比

function thumb($img, $w = 100, $h = 100, $fill = true) {
    if (!extension_loaded('gd') && !extension_loaded('gd2')) {
        trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);
        return false;
    }

    $imgInfo = getimagesize($img);
    switch ($imgInfo[2]) {
        case 1: $im = imagecreatefromgif($img); break;
        case 2: $im = imagecreatefromjpeg($img);  break;
        case 3: $im = imagecreatefrompng($img); break;
        default:  trigger_error('Tipo de imagen no reconocido.', E_USER_WARNING);  break;
    }
    // note, how the lesser ratio is chosen
    if ($imgInfo[0] <= $w && $imgInfo[1] <= $h && !$fill) {
        $nHeight = $imgInfo[1];
        $nWidth = $imgInfo[0];
    }else{
        if ($w/$imgInfo[0] < $h/$imgInfo[1]) {
            $nWidth = $w;
            $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
        }else{
            $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
            $nHeight = $h;
        }
    }

    $nWidth = round($nWidth);
    $nHeight = round($nHeight);

    $newImg = imagecreatetruecolor($nWidth, $nHeight);

    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

    header("Content-type: ". $imgInfo['mime']);

    switch ($imgInfo[2]) {
        case 1: imagegif($newImg); break;
        case 2: imagejpeg($newImg);  break;
        case 3: imagepng($newImg); break;
        default:  trigger_error('Imposible mostrar la imagen.', E_USER_WARNING);  break;
    }

    imagedestroy($newImg);
}

答案 1 :(得分:0)

啊,我看到..你希望用你的imagedata设置$ destination,上面的帖子显示了一个函数,它将通过GET请求提供图像,如下所示:

http://localhost/thumbnail.php?w=500&h=300&file=./images/bigimage.png

而thumbnail.php包含函数'thumb'并使用

调用它
<? thumb($_GET['file'], $_GET['w'], $_GET['h']); ?>

你想要的是什么 1)修改thumb接受$ destination的参数 2)删除标题(“Content-type ...”)行 3)在最后一个switch-case中重写为:

switch ($imgInfo[2]) {
    case 1: return imagegif($newImg, $destination);
    case 2: return imagejpeg($newImg, $destination);
    case 3: return imagepng($newImg, $destination);
    default:  trigger_error('Imposible mostrar la imagen.', E_USER_WARNING);  break;
}
 return false;