imagecreatefromjpeg显示空白图像

时间:2012-09-03 17:45:46

标签: php thumbnails

我在html中使用以下代码来调用php文件来创建缩略图并在此页面上显示它 & w = 150& h =& 00“alt =”Image“/>

miniature.php的代码如下:

     <?php

     function redimensionner_image($chemin_image, $largeur_max, $hauteur_max)
    {
list($src_w, $src_h) = getimagesize($chemin_image);
$dst_w = $largeur_max;
$dst_h = $hauteur_max;

if($src_w < $dst_w)
    $dst_w = $src_w;

// Teste les dimensions tenant dans la zone
$test_h = round(($dst_w / $src_w) * $src_h);
$test_w = round(($dst_h / $src_h) * $src_w);

if(!$dst_h)// Si Height final non précisé (0)
    $dst_h = $test_h;
elseif(!$dst_w) // Sinon si Width final non précisé (0)
    $dst_w = $test_w;
elseif($test_h>$dst_h) // Sinon teste quel redimensionnement tient dans la zone
    $dst_w = $test_w;
else
    $dst_h = $test_h;

$array_ext = explode('.', $chemin_image);
$extension = strtolower($array_ext[count($array_ext)-1]);

if($extension == 'jpg' || $extension == 'jpeg')
   $img_in = imagecreatefromjpeg($chemin_image);
else if($extension == 'png')
   $img_in = imagecreatefrompng($chemin_image);
else if($extension == 'gif')
   $img_in = imagecreatefromgif($chemin_image);
else
    return false;

$img_out = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $dst_w, $dst_h, imagesx($img_in), imagesy($img_in));

imagejpeg($img_out);
     }

      ?>

但是,Imagecreatefromjpeg会在调整大小后返回黑色图像。请帮忙

1 个答案:

答案 0 :(得分:3)

首先,您不需要展开文件扩展名,因为getimagesize会为您提供类型:

list($width, $height, $type) = getimagesize($source);

接下来我不按照您的尺寸计算,尝试简化它们,例如:

$scale = min($maxWidth / $width, $maxHeight / $height, 1); // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter

$newWidth = min($width * $scale, $maxWidth);
$newHeight = min($height * $scale, $maxHeight);

简单重新缩放并保留纵横比。

供参考,这是我在项目中使用的代码:

function SaveImageAsJpeg($sourceFilename, $destFilename, $maxWidth = 0, $maxHeight = 0, $jpegQuality = 80) {
    list($width, $height, $type) = getimagesize($sourceFilename);

    $sourceImage = false;

    switch ($type) {
        case IMAGETYPE_GIF:
            $sourceImage = imagecreatefromgif($sourceFilename); 
            break;
        case IMAGETYPE_JPEG:
            $sourceImage = imagecreatefromjpeg($sourceFilename);
            break;
        case IMAGETYPE_PNG:
            $sourceImage = imagecreatefrompng($sourceFilename);
            break;
    }

    if (!$sourceImage)
        return false;

    if (($maxWidth == 0) || ($maxHeight == 0)) {
        // Don't resize
        $destinationImage = imagecreatetruecolor($width, $height);
        imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height);
        imagejpeg($destinationImage, $destFilename, $jpegQuality);
        imagedestroy($destinationImage);
    } else {
        // Resize image
        $scale = min($maxWidth / $width, $maxHeight / $height, 1);  // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter

        $newWidth = min($width * $scale, $maxWidth);
        $newHeight = min($height * $scale, $maxHeight);

        $destinationImage = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
        imagejpeg($destinationImage, $destFilename, $jpegQuality);
        imagedestroy($destinationImage);
    }

    imagedestroy($sourceImage);

    return true;
}

当然,上面的代码不会返回图像数据。它只是将重新缩放的图像保存到服务器上的其他文件中。但是如果您将NULL作为$destFilename参数传递,它会将图像数据输出到输出流:

header('Content-Type: image/jpeg');
SaveImageAsJpeg($sourceFilename, NULL, 200, 200);

如果您仍然获得黑色图像,我建议增加PHP内存限制。如果您可以修改PHP.INI,请调整memory_limit设置。否则,请使用此行的.htaccess文件,例如:php_value memory_limit 64M