首页 > 解决方案 > 正在工作的 PHP 图像函数现在正在生成所有黑色图像

问题描述

我多年来一直使用相同的代码,零问题。最近,我客户的服务器从 PHP 5 升级到 PHP 7,这破坏了许多项目。我以为这一切都解决了,但这个问题仍然存在。

这是我的功能。

function imageupload($filearray, $maxsize=0, $itemid=0){
    $validtype=1;
    $avalidtype=0;


$randfilename=sha1(microtime().rand(0,99999));

global $uploadDir;
global $dbh;


$fileid=0;
if ( (is_uploaded_file($filearray['tmp_name'])) && ($validtype==1)) {
    $src=$filearray['name'];
        $randfilename.=".jpg";
    $file_name = $uploadDir . $randfilename;
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
    copy($filearray['tmp_name'],$file_name);

    if(preg_match('.jpeg',$src)||preg_match('.jpg',$src)) {
        $src=imagecreatefromjpeg($filearray['tmp_name']);
    }
    elseif(preg_match('.png',$src)) {   
    $src = imagecreatefrompng($filearray['tmp_name']);   
    }  

    $src2=$file_name;
    list($width,$height)=getimagesize($src2);

    $thumbsize=getThumbSize($width, $height, 450);
    $newwidth=$thumbsize[0];
    $newheight=$thumbsize[1];
    $awidth=$width;
    $aheight=$height;
    $new_name = $uploadDir .  $randfilename;
    $tmp= imagecreatetruecolor($newwidth,$newheight)
    or croak("Cannot Initialize new GD image stream");
    //  list($twidth,$theight)=getimagesize($tmp);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
    imagejpeg($tmp,$new_name,100);
    imagedestroy($tmp);

}//end function

我已经验证正在传递的文件数组中填充了合法数据。我已验证调整大小功能正常工作。我已经验证要保存调整大小的图像的子文件夹确实具有适当的权限(我什至将它和父文件夹设置为 777)。

无论我上传 jpeg 还是 png,我仍然会得到空白黑框(尺寸正确)。这在 PHP 5 中可以完美运行——实际上也可以在多个其他服务器上运行。

我错过了什么明显的东西?

标签: phpimagegd

解决方案


推荐阅读