Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 imagecreatefromjpeg()在调整大小时返回黑色图像_Php_Image_File_Image Processing_File Upload - Fatal编程技术网

Php imagecreatefromjpeg()在调整大小时返回黑色图像

Php imagecreatefromjpeg()在调整大小时返回黑色图像,php,image,file,image-processing,file-upload,Php,Image,File,Image Processing,File Upload,我有以下代码 // load image and get image size $img = imagecreatefrompng( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $imageWidth; $new_height = 500; // create

我有以下代码

 // load image and get image size
  $img = imagecreatefrompng( "{$pathToImages}{$fname}" );

  $width = imagesx( $img );
  $height = imagesy( $img );


  // calculate thumbnail size
  $new_width = $imageWidth;
  $new_height = 500;

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

  // copy and resize old image into new image 
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
对于某些图像,它可以正常工作。但是对于某些图像,它会显示如下错误

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: 

Warning: imagesx() expects parameter 1 to be resource, boolean given

Warning: imagesy() expects parameter 1 to be resource, boolean given
我还启用了

gd.jpeg\u忽略\u警告=1

在php.ini中

感谢您的帮助。

根据它的说法,
imagecreatefromjpeg
的实现中有一个bug,它应该返回
false
,但会抛出一个错误

解决方案是检查图像的文件类型(我删除了对
imagecreatefromjpeg
的重复调用,因为它完全多余;我们之前已经检查了正确的文件类型,如果由于其他原因出现错误,
imagecreatefromjpeg
将正确返回
false
):

然后您可以这样编写代码:

$img = imagecreatefrompng_if_correct("{$pathToImages}{$fname}");
if ($img == false) {
    // report some error
} else {
    // enter all your other functions here, because everything is ok
}

当然,如果您想打开png文件(如代码所示),也可以对png执行相同的操作。实际上,通常您会检查文件的实际文件类型,然后在这三种文件类型(jpeg、png、gif)之间调用正确的函数。

您能举个例子说明哪些文件有效/无效吗? 根据远程文件中的空格,名称可能会导致问题。

请参阅。建议更改jpeg图像加载的GD设置:

ini_set("gd.jpeg_ignore_warning", 1);
然后,您还需要检查调用的返回值:

另请参见可能重复的问题:


它的错误描述与您的错误描述几乎相同。

我的解决方案是:检测imagecreatefromjpeg是否返回“false”,在这种情况下,在文件获取内容时使用imagecreatefromstring。 为我工作。 请参见下面的代码示例:

$ind=0;
do{
    if($mime == 'image/jpeg'){
        $img = imagecreatefromjpeg($image_url_to_upload);
    }elseif ($mime == 'image/png') {
        $img = imagecreatefrompng($image_url_to_upload);
    }
    if ($img===false){
        echo "imagecreatefromjpeg error!\n";
    }
    if ($img===false){
        $img = imagecreatefromstring(file_get_contents($image_url_to_upload));
    }
    if ($img===false){
        echo "imagecreatefromstring error!\n";
    }
    $ind++;
}while($img===false&&$ind<5);
$ind=0;
做{
如果($mime=='image/jpeg'){
$img=imagecreatefromjpeg($image\u url\u to\u upload);
}elseif($mime=='image/png'){
$img=imagecreatefrompng($image\u url\u to\u upload);
}
如果($img==false){
echo“imagecreatefromjpeg错误!\n”;
}
如果($img==false){
$img=imagecreatefromstring(文件获取内容($image\u url\u to\u上传));
}
如果($img==false){
回显“imagecreatefromstring错误!\n”;
}
$ind++;

}当($img===false&&$ind使用URL作为图像源时,您需要确保启用了php设置allow\u URL\u fopen。

您的代码没有调用
imagecreatefromjpeg
。我已经检查了可能的副本。但我的文件名中没有空格。
$img = imagecreatefromjpeg($file);
if (!$img) {
    printf("Failed to load jpeg image \"%s\".\n", $file);
    die();
}
$ind=0;
do{
    if($mime == 'image/jpeg'){
        $img = imagecreatefromjpeg($image_url_to_upload);
    }elseif ($mime == 'image/png') {
        $img = imagecreatefrompng($image_url_to_upload);
    }
    if ($img===false){
        echo "imagecreatefromjpeg error!\n";
    }
    if ($img===false){
        $img = imagecreatefromstring(file_get_contents($image_url_to_upload));
    }
    if ($img===false){
        echo "imagecreatefromstring error!\n";
    }
    $ind++;
}while($img===false&&$ind<5);