【问题标题】:Why is my scaled image always black?为什么我的缩放图像总是黑色的?
【发布时间】:2014-07-28 08:11:06
【问题描述】:

我正在制作缩略图,出于某种原因,我的输出尺寸正确,但始终是黑色的。我看到另一个关于类似主题的 Stack Overflow 帖子,但在他的案例中,他错误地传递了参数。

我正在从摄像机捕获图像,然后使用此代码:

$data = base64_decode($data); // the data will be saved to the db

$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);

// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );

// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$url = IMGDIR.$imgname;
$thumburl = IMGDIR."thumb/".$imgname;

// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);

我得到的结果是两个文件都保存到了正确的目录中,大小都正确,但缩略图全是黑色的。我一定缺少一些简单的东西。有什么想法吗?

【问题讨论】:

    标签: php image-scaling


    【解决方案1】:

    尝试使用 imagesavealpha 保存图像的 Alpha 通道,并为第二个参数传递 true

    imagesavealpha($image, true);
    imagepng($image,$url);
    
    imagesavealpha($new_image, true);
    imagepng($new_image,$thumburl);
    

    【讨论】:

      【解决方案2】:

      请记住,PNG 文件具有 Alpha 通道。所以一定要使用imagealphablendingimagesavealpha。在这里,它们被集成到您的代码中。

      $data = base64_decode($data); // the data will be saved to the db
      
      $image = imagecreatefromstring($data); // need to create an image to grab the width and height
      $img_width = imagesx($image);
      $img_height = imagesy($image);
      
      // calculate thumbnail size
      $new_height = 100;
      $new_width = floor( $img_width * ( 100 / $img_height ) );
      
      // create a new temporary image
      $new_image = imagecreatetruecolor( $new_width, $new_height );
      
      // copy and resize old image into new image
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
      $url = IMGDIR . $imgname;
      $thumburl = IMGDIR . "thumb/" . $imgname;
      
      // Set the image alpha blending settings.
      imagealphablending($image, false);
      imagealphablending($new_image, false);
      
      // Set the image save alpha settings.
      imagesavealpha($image, true);
      imagesavealpha($new_image, true);
      
      // save image and thumb to disk
      imagepng($image,$url);
      imagepng($new_image,$thumburl);
      

      【讨论】:

      • 试过这个。大的保存很好,缩略图仍然是全黑的。我也试过jpg。
      • 我想知道它是否与图像源有关。它是来自视频捕获的 base64 流。我首先对其进行解码,它可以用于保存,但它可能缺少一些缩放所需的信息?
      • @DougWolfgram 非常容易修复缩略图。现在试试我的代码。在您发布的代码中,您首先使用$img_width$img_height 分配原始图像的高度和宽度。但然后看看你原来的imagecopyresampled 行。您正在使用$width$height,它们是未定义的变量。所以我只是用那个固定的东西编辑了我的答案,所以你现在应该开始做生意了。
      • @DougWolfgram 不要对自己太苛刻。你知道我如何处理像这样的GD东西,因为它让我感到困惑吗?我创建了整个函数和类来封装所有这些令人头疼的问题,这样我就可以发送一个文件、尺寸等等。考虑为您的代码执行此操作。很高兴这件事解决了!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2016-05-23
      • 2021-04-11
      • 2010-11-11
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      相关资源
      最近更新 更多