Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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 imagecreatefrom结果为黑色_Php_Gd - Fatal编程技术网

php imagecreatefrom结果为黑色

php imagecreatefrom结果为黑色,php,gd,Php,Gd,我已经想了好几天了。我需要上传一个图像,将其裁剪成正方形,大小调整为300x300,然后大小调整为150x150,上传到我的“uploads/products/$id/”文件夹中,然后将文件路径插入mysql数据库 然而,我不断得到黑色图像输出。这个脚本有什么问题 $ext = explode('.', $_FILES['image1']['name']); $extension = $ext[1]; $target_path = 'uploads/products/'.$id.'/'; $fi

我已经想了好几天了。我需要上传一个图像,将其裁剪成正方形,大小调整为300x300,然后大小调整为150x150,上传到我的“uploads/products/$id/”文件夹中,然后将文件路径插入mysql数据库

然而,我不断得到黑色图像输出。这个脚本有什么问题

$ext = explode('.', $_FILES['image1']['name']);
$extension = $ext[1];
$target_path = 'uploads/products/'.$id.'/';
$filename = 'featuredpic.'.$extension;

$featured100_full_path = $target_path.$filename;

if(!is_dir('../../../uploads/products/'.$id)){
    mkdir('../../../uploads/products/'.$id, 0777);
}

if(file_exists('../../../'.$featured100_full_path)) {
    chmod('../../../'.$featured100_full_path, 0755);
    unlink('../../../'.$featured100_full_path);
}

if(!move_uploaded_file($_FILES['image1']['tmp_name'], '../../../'.$featured100_full_path)){
    echo 'Error: Image Not Uploaded!';
}

if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['image1']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
    }
else if($extension=="png"){
$uploadedfile = $_FILES['image1']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);}

list($width, $height) = getimagesize($filename);
$newwidth = 300;
$newheight = 300;
$featured300 = imagecreatetruecolor($newwidth, $newheight);

$newwidth1 = 150;
$newheight1 = 150;
$featuredthumb = imagecreatetruecolor($newwidth1, $newheight1);

imagecopyresampled($featured300,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($featuredthumb,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filename_featured300 = 'uploads/products/'.$id.'/featured300.'.$extension;
$filename_featuredthumb = 'uploads/products/'.$id.'/featuredthumb.'.$extension;

imagejpeg($featured300, '../../../'.$filename_featured300,100);
imagejpeg($featuredthumb, '../../../'.$filename_featuredthumb,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);

此功能可能是产生黑色图像的原因:

我个人会逐步完成每个修改过程,以确保其功能正常,并完成您期望的事情,我必须承认这段代码中有很多是有风险的:

$ext = explode('.', $_FILES['image1']['name']);
$extension = $ext[1];
您对文件扩展名过于信任,这样更安全:

然而,我知道这是一个额外的PECL扩展-所以请理解为什么你可能选择了爆炸点。如果不为文件上传、目录结构等编写一个包装器,就很难对代码进行除错

提示:继续将图像移动到一个带有数字扩展名的临时目录,以便在转换链的每个阶段查看-它应该可以快速识别您的缺陷!希望这能帮上忙,抱歉我帮不上忙了