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 上载大于1mb的图像时内存耗尽_Php_Image_Memory_Upload - Fatal编程技术网

Php 上载大于1mb的图像时内存耗尽

Php 上载大于1mb的图像时内存耗尽,php,image,memory,upload,Php,Image,Memory,Upload,当我试图上传超过1MB的图像时,我收到一个错误,告诉我分配的内存已经用完,失败等等,下面的脚本--> 出现的错误是将第91行称为罪魁祸首 “图像填充($image_p,0,0,$white);” 当我试图注释掉这一行时,我没有得到错误,只是存储了一个“null”,而不是上传 -->我的服务器现在有64m的限制--> 这段代码可以通过上传一张图片在进行测试——欢迎任何帮助 function uploadImage($fileName, $maxSize, $maxW, $fullPath,

当我试图上传超过1MB的图像时,我收到一个错误,告诉我分配的内存已经用完,失败等等,下面的脚本-->

出现的错误是将第91行称为罪魁祸首

“图像填充($image_p,0,0,$white);”

当我试图注释掉这一行时,我没有得到错误,只是存储了一个“null”,而不是上传

-->我的服务器现在有64m的限制-->

这段代码可以通过上传一张图片在进行测试——欢迎任何帮助

    function uploadImage($fileName, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH = null){
        $folder = $relPath;
        $maxlimit = $maxSize;
        $allowed_ext = "jpg,jpeg,gif,png,bmp";
        $match = "";
        $filesize = $_FILES[$fileName]['size'];
        if($filesize > 0){  
            $filename = strtolower($_FILES[$fileName]['name']);
            $filename = preg_replace('/\s/', '_', $filename);
            if($filesize < 1){ 
                $errorList[] = "File size is empty.";
            }
            if($filesize > $maxlimit){ 
                $errorList[] = "File size is too big.";
            }
            if(count($errorList)<1){
                $file_ext = preg_split("/\./",$filename);
                $allowed_ext = preg_split("/\,/",$allowed_ext);
                foreach($allowed_ext as $ext){
                    if($ext==end($file_ext)){
                        $match = "1"; // File is allowed
                        $NUM = time();
                        $front_name = substr($file_ext[0], 0, 15);
                        $newfilename = $front_name."_".$NUM.".".end($file_ext);
                        $filetype = end($file_ext);
                        $save = $folder.$newfilename;
                        if(!file_exists($save)){
                            list($width_orig, $height_orig) = getimagesize($_FILES[$fileName]['tmp_name']);
                            if($maxH == null){
                                if($width_orig < $maxW){
                                    $fwidth = $width_orig;
                                }else{
                                    $fwidth = $maxW;
                                }
                                $ratio_orig = $width_orig/$height_orig;
                                $fheight = $fwidth/$ratio_orig;

                                $blank_height = $fheight;
                                $top_offset = 0;

                            }else{
                                if($width_orig <= $maxW && $height_orig <= $maxH){
                                    $fheight = $height_orig;
                                    $fwidth = $width_orig;
                                }else{
                                    if($width_orig > $maxW){
                                        $ratio = ($width_orig / $maxW);
                                        $fwidth = $maxW;
                                        $fheight = ($height_orig / $ratio);
                                        if($fheight > $maxH){
                                            $ratio = ($fheight / $maxH);
                                            $fheight = $maxH;
                                            $fwidth = ($fwidth / $ratio);
                                        }
                                    }
                                    if($height_orig > $maxH){
                                        $ratio = ($height_orig / $maxH);
                                        $fheight = $maxH;
                                        $fwidth = ($width_orig / $ratio);
                                        if($fwidth > $maxW){
                                            $ratio = ($fwidth / $maxW);
                                            $fwidth = $maxW;
                                            $fheight = ($fheight / $ratio);
                                        }
                                    }
                                }
                                if($fheight == 0 || $fwidth == 0 || $height_orig == 0 || $width_orig == 0){
                                    die("FATAL ERROR REPORT ERROR CODE [add-pic-line-67-orig] to <a href='http://www.atwebresults.com'>AT WEB RESULTS</a>");
                                }
                                if($fheight < 45){
                                    $blank_height = 45;
                                    $top_offset = round(($blank_height - $fheight)/2);
                                }else{
                                    $blank_height = $fheight;
                                }
                            }

                            $image_p = imagecreatetruecolor($fwidth, $blank_height);
                            $white = imagecolorallocate($image_p, $colorR, $colorG, $colorB);

                            imagefill($image_p, 0, 0, $white);
                            switch($filetype){
                                case "gif":
                                    $image = @imagecreatefromgif($_FILES[$fileName]['tmp_name']);
                                break;
                                case "jpg":
                                    $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
                                break;
                                case "jpeg":
                                    $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
                                break;
                                case "png":
                                    $image = @imagecreatefrompng($_FILES[$fileName]['tmp_name']);
                                break;
                            }
                            @imagecopyresampled($image_p, $image, 0, $top_offset, 0, 0, $fwidth, $fheight, $width_orig, $height_orig);
                            switch($filetype){
                                case "gif":
                                    if(!@imagegif($image_p, $save)){
                                        $errorList[]= "PERMISSION DENIED [GIF]";
                                    }
                                break;
                                case "jpg":
                                    if(!@imagejpeg($image_p, $save, 100)){
                                        $errorList[]= "PERMISSION DENIED [JPG]";
                                    }
                                break;
                                case "jpeg":
                                    if(!@imagejpeg($image_p, $save, 100)){
                                        $errorList[]= "PERMISSION DENIED [JPEG]";
                                    }
                                break;
                                case "png":
                                    if(!@imagepng($image_p, $save, 0)){
                                        $errorList[]= "PERMISSION DENIED [PNG]";
                                    }
                                break;
                            }
                            @imagedestroy($filename);
                        }else{
                            $errorList[]= "CANNOT MAKE IMAGE IT ALREADY EXISTS";
                        }   
                    }
                }       
            }
        }else{
            $errorList[]= "NO FILE SELECTED";
        }
        if(!$match){
            $errorList[]= "File type isn't allowed: $filename";
        }
        if(sizeof($errorList) == 0){
            return $fullPath.$newfilename;
        }else{
            $eMessage = array();
            for ($x=0; $x<sizeof($errorList); $x++){
                $eMessage[] = $errorList[$x];
            }
            return $eMessage;
        }
    }

    $filename = strip_tags($_REQUEST['filename']);
    $maxSize = strip_tags($_REQUEST['maxSize']);
    $maxW = strip_tags($_REQUEST['maxW']);
    $fullPath = strip_tags($_REQUEST['fullPath']);
    $relPath = strip_tags($_REQUEST['relPath']);
    $colorR = strip_tags($_REQUEST['colorR']);
    $colorG = strip_tags($_REQUEST['colorG']);
    $colorB = strip_tags($_REQUEST['colorB']);
    $maxH = strip_tags($_REQUEST['maxH']);
    $filesize_image = $_FILES[$filename]['size'];
    if($filesize_image > 0){
        $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH);
        if(is_array($upload_image)){
            foreach($upload_image as $key => $value) {
                if($value == "-ERROR-") {
                    unset($upload_image[$key]);
                }
            }
            $document = array_values($upload_image);
            for ($x=0; $x<sizeof($document); $x++){
                $errorList[] = $document[$x];
            }
            $imgUploaded = false;
        }else{
            $imgUploaded = true;
            $_SESSION['test']= $upload_image;
        }
    }else{
        $imgUploaded = false;
        $errorList[] = "File Size Empty";
    }
?>
function uploadImage($fileName、$maxSize、$maxW、$fullPath、$relPath、$colorR、$colorG、$colorB、$maxH=null){
$folder=$relPath;
$maxlimit=$maxSize;
$allowed_ext=“jpg、jpeg、gif、png、bmp”;
$match=“”;
$filesize=$_文件[$fileName]['size'];
如果($filesize>0){
$filename=strtolower($_FILES[$filename]['name']);
$filename=preg_replace('/\s/','',$filename);
如果($filesize<1){
$errorList[]=“文件大小为空。”;
}
如果($filesize>$maxlimit){
$errorList[]=“文件大小太大。”;
}
if(计数($errorList)$maxH){
$ratio=($height_orig/$maxH);
$fheight=$maxH;
$fwidth=($width\u orig/$ratio);
如果($fwidth>$maxW){
$ratio=($fwidth/$maxW);
$fwidth=$maxW;
$FHheight=($FHheight/$ratio);
}
}
}
如果($fheight==0 | |$fwidth==0 | |$height | u orig==0 | |$width | u orig==0){
die(“致命错误报告错误代码[add-pic-line-67-orig]);
}
如果($FHheight<45){
$blank_高度=45;
$top_offset=圆形($blank_height-$fheight)/2);
}否则{
$blank_height=$fheight;
}
}
$image\u p=imagecreatetruecolor($fwidth,$blank\u height);
$white=imagecolorallocate($image\u p、$colorR、$colorG、$colorB);
图像填充($image\u p,0,0,$white);
开关($filetype){
案例“gif”:
$image=@imagecreatefromformgif($_FILES[$fileName]['tmp_name']);
打破
案例“jpg”:
$image=@imagecreatefromjpeg($_文件[$fileName]['tmp_名称]);
打破
案例“jpeg”:
$image=@imagecreatefromjpeg($_文件[$fileName]['tmp_名称]);
打破
案例“png”:
$image=@imagecreatefrompng($_文件[$fileName]['tmp_名称]);
打破
}
@imagecopyresampled($image\u p,$image,0,$top\u offset,0,0,$fwidth,$fheight,$width\u orig,$height\u orig);
开关($filetype){
案例“gif”:
如果(!@imagegif($image\u p,$save)){
$errorList[]=“权限被拒绝[GIF]”;
}
打破
案例“jpg”:
如果(!@imagejpeg($image\u p,$save,100)){
$errorList[]=“权限被拒绝[JPG]”;
}
打破
案例“jpeg”:
如果(!@imagejpeg($image\u p,$save,100)){
$errorList[]=“权限被拒绝[JPEG]”;
}
打破
案例“png”:
如果(!@imagepng($image\u p,$save,0)){
$errorList[]=“权限被拒绝[PNG]”;
}
打破
}
@imagedestroy($filename);
}否则{
$errorList[]=“无法使图像已存在”;
}   
}
}       
}
}否则{
$errorList[]=“未选择任何文件”;
}
如果(!$match){
$errorList[]=“不允许使用文件类型:$filename”;
}
if(sizeof($errorList)==0){
返回$fullPath.$newfilename;
}否则{
$eMessage=array();
对于($x=0;$x 0){
$upload_image=uploadImage($filename、$maxSize、$maxW、$fullPath、$relPath、$colorR、$colorG、$colorB、$maxH);
if(is_数组($upload_image)){
foreach($key=>$value)上传图片{
如果($value==“-ERROR-”){
取消设置($upload_image[$key]);
}
}
$document=数组值($upload\u image);
对于($x=0;$x)
脚本中的ini\u set('memory\u limit','240M')


为我工作;仅供参考,我使用共享主机。

当您说您的服务器有64mb的限制时,这是通过设置应用的吗