开发者

PHP Image upload problem

I'm having trouble to get the following code to upload large images. It works great with images less than 1000px by 1000px, but breaks on anything bigger. Any help/ideas greatly appreciated.

Note: I have tried increasing the '$memoryNeeded>=10000000' to '7700000000' but still no joy.

if (!$error && is_uploaded_file($_FILES['galleryFile']['tmp_name'])) {
        $format = strtolower(substr(strrchr($_FILES['galleryFile']['name'],"."),1));            
        $str = strtolower(trim($_FILES['galleryFile']['name']));
        $str = preg_replace('/[^a-z0-9-]/', '-', $str);
        $str = preg_replace('/-+/', "-", $str);    
        $filename=$str.'.'.$format;                
        $uploadGallery=$origFileDir.$filename;
        foreach ($allowedImgFormats as $key => $value) {
            $value==$format ? $imgFormatOK='1' : NULL;
        }
        $imgFormatOK=='0' ? $error='You are attempting to upload an image with an invalid format!<br />Please only upload images with ".gif", ".jpg" or ".jpeg" extensions.' : NULL;
  开发者_开发技巧      if (!$error && move_uploaded_file($_FILES['galleryFile']['tmp_name'], $uploadGallery)){ 
            $galleryW='944'; $galleryH='733';                        
            $galleryInfo = getimagesize($uploadGallery);
            $memoryNeeded = Round(($galleryInfo[0] * $galleryInfo[1] * $galleryInfo['bits'] * $galleryInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
            if ($memoryNeeded>=10000000) {
                unlink($uploadGallery); $error='The chosen image is too large to process.<br />Please upload a smaller image (lower dimensions and/or resolution).';
            } else {
                list($wOrig, $hOrig) = getimagesize($uploadGallery);
                $ratio_orig = $wOrig/$hOrig;
                if ($wOrig > $galleryW) { $galleryW = $galleryH*$ratio_orig; $galleryH = $galleryW/$ratio_orig; } else { $galleryW=$wOrig; $galleryH=$hOrig; }    
                if ($galleryH > $galleryH) { $galleryH = $galleryW*$ratio_orig; $galleryW = $galleryH/$ratio_orig; }
                $galleryP = imagecreatetruecolor($galleryW, $galleryH); 

                switch($format) {
                   case 'gif' : $thisGallery = imagecreatefromgif($uploadGallery); break;
                   case 'jpg' : $thisGallery = imagecreatefromjpeg($uploadGallery); break;
                }
                imagecopyresampled($galleryP, $thisGallery, 0, 0, 0, 0, $galleryW, $galleryH, $wOrig, $hOrig);

                switch($format) {
                   case 'gif' : $createGallery=imagegif($galleryP, $galleryFileDir.$filename, 88); break;
                   case 'jpg' : $createGallery=imagejpeg($galleryP, $galleryFileDir.$filename, 88); break;
                }
                imagedestroy($galleryP); imagedestroy($thisGallery); unlink($uploadGallery);

                if (!$createGallery) {
                     $error='The chosen image failed to transfer correctly.<br />Please try again, or attempt to upload an alternative image.';
                     file_exists($galleryFileDir.'/'.$filename) ? unlink($galleryFileDir.'/'.$filename) : NULL;
                } else {            
                    $_POST['imagename']=$filename; 
                    mysql_query("INSERT INTO isgallery(imagename, assoc_object) VALUES('".$_POST['imagename']."', '".$_POST['id']."')");                         
                }
            }        
        } else {
            !$error ? $error='The chosen image failed to upload correctly.<br />Please try again, or attempt to upload an alternative image.' : NULL;
            file_exists($uploadGallery) ? unlink($uploadGallery) : NULL;
        }
    }


A 1000x1000 image requires at LEAST 3,000,000 bytes of memory if you're dealing with true color. and 4,000,000 if you're doing alpha transparency. Your $memoryNeeded variable is useless if it's set to something larger than PHP's memory_limit. It'll happily try to create an image and fail due to exceeded the limit.

You can check what the limit is with ini_get('memory_limit'), though you most likely won't be able to directly use this value for calculations without some massaging, as it'll likely return something like '32M' (32 megabyte limit), instead of 33554432.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜