imagecreatefromjpeg显示一个空白图像


imagecreatefromjpeg shows a blank image

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

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在调整大小后返回一个黑色图像。如有任何帮助,请

首先,您不需要分解文件扩展名,因为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