数据URI保存的JPG图像在上传时使用php imagecreatefromjpeg创建黑色图像


Data URI saved JPG images create black images on upload with php imagecreatefromjpeg

我正在使用以下代码上传

//width,new_width,height,new_height are set
$tmp_filename = $_FILES['file']['tmp_name'];
$image_p = imagecreatetruecolor($new_width, $new_height); //resample image          
if(($ext=='jpg') | ($ext=='jpeg')){
    //if image to be uploaded is jpg
    $image = imagecreatefromjpeg($tmp_filename); 
}
else{
    //if image to be uploaded is png
    $image = imagecreatefrompng($tmp_filename);
}
//to convert to jpg
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$done=imagejpeg($image_p, $output_dirrectory."/".$image_name.".jpg",95); //95% quality

现在,问题是,当我保存使用DataURI生成的任何其他网站的图像时,它会作为JPG图像保存在我的本地机器上。当我尝试用以下代码上传相同的图片时,它创建了黑色图片。

我想不通这个问题。我希望解决方案在imagecreatefromstring中,但我不知道下面的代码会如何实现?请提出解决方案?

尝试添加:

$width = imageSX($image);
$height = imageSY($image);

调用imagecopyreseampled 之前