0

这段代码已经给我带来了几个星期的问题,但还没有接近解决它......

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
4

2 回答 2

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);
}
于 2012-05-02T15:39:17.627 回答
0

啊,我明白了.. 您希望使用您的图像数据设置 $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) 删除 header("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;
于 2012-05-02T17:27:59.340 回答