Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的缩放图像总是黑色?_Php_Image Scaling - Fatal编程技术网

Php 为什么我的缩放图像总是黑色?

Php 为什么我的缩放图像总是黑色?,php,image-scaling,Php,Image Scaling,我正在制作缩略图,出于某种原因,我的输出是正确的大小,但总是黑色的。我看到另一篇关于类似主题的堆栈溢出帖子,但在他的例子中,他错误地传递了参数 我正在从摄像机捕获图像,然后使用以下代码: $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 h

我正在制作缩略图,出于某种原因,我的输出是正确的大小,但总是黑色的。我看到另一篇关于类似主题的堆栈溢出帖子,但在他的例子中,他错误地传递了参数

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

$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);

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

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

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

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

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

$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\u width
$img\u height
指定原始图像的高度和宽度。但是,再看看原始的
imagecopyresampled
行。您正在使用未定义的变量
$width
$height
。所以我只是用修正后的答案编辑了一下,所以你现在应该开始做生意了。@DougWolfgram别对自己太苛刻了。你知道我是怎么处理这类事情的,因为它让我困惑?我创建了完整的函数和类来封装所有这些令人头痛的问题,这样我就可以发送一个文件,维度&就是这样。考虑为你的代码做这件事。很高兴这个问题解决了!