[英]Trying to upload a 1M file locally and i get a Fatal Error
"Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 9828 bytes)". “致命错误:允许的内存大小为31457280字节耗尽(尝试分配9828字节)”。
This is the error i get but I am only trying to upload a 1mb image. 这是我得到的错误,但我只是尝试上传1mb图像。 I have increased the memory limit in php.ini and the execution time.
我增加了php.ini中的内存限制和执行时间。 I am trying this on a local MAMP server, on a Mac using firefox.
我在使用firefox的Mac上的本地MAMP服务器上尝试此操作。 This going to be for an online image gallery.
这将是一个在线图片库。 Any ideas?
有任何想法吗? Below is the code:
以下是代码:
ini_set("memory_limit","30M");
if(isset($_POST['submit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "images/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
//below here for the removed code
$save = "thumbs/uploads/" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
$imagesize = getimagesize($file);
list($width, $height) = $imagesize;
unset($imagesize);
if($width>$height)
{
$modwidth = 150;
$diff = $width / $modwidth;
$modheight = $height / $diff;
}else{
$modheight = 150;
$diff = $height / $modheight;
$modwidth = $width / $diff;
}
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
$imagecopy = imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagedestroy($image);
imagedestroy($im);
imagedestroy($imagecopy);
imagedestroy($source);
$imagejpg = imagejpeg($tn, $save, 100);
imagedestroy($tn);
imagedestroy($imagejpg);
EDIT 编辑
This has now been sorted out hopefully. 现在已经有希望了解这一点。 One of my colleagues had a solution all along but neglected to tell me!
我的一位同事一直都有解决方案,却忽略了告诉我!
You're likely loading the image to do some manipulation of it. 您可能正在加载图像以对其进行一些操作。 That causes the image data to be decompressed, which requires a lot of memory for big images (I think it's about 4 bytes per pixel).
这导致图像数据被解压缩,这需要大量的内存用于大图像(我认为每个像素大约4个字节)。
You can choose to either not process the image, or do your processing outside of PHP - for example by invoking ImageMagick or some other program. 您可以选择不处理图像,也不选择在PHP之外进行处理 - 例如通过调用ImageMagick或其他程序。 It depends a bit on what you're trying to accomplish.
这取决于你想要完成的事情。
It's nothing to do with the (file)size of the image you're uploading, the call that's breaking your memory limit is imagecreatetruecolor(). 它与您上传的图像的(文件)大小无关,打破内存限制的调用是imagecreatetruecolor()。
imagecreatetruecolor() will allocate an area of memory to store a true colour image in with no compression, and use 32 bits (4 bytes) per pixel. imagecreatetruecolor()将分配一个内存区域,以便在没有压缩的情况下存储真彩色图像,并且每个像素使用32位(4个字节)。
So for a 1024x768 pixel image, for example, imagecreatetruecolour() will use up 1024*768*4 = 3145728 bytes, or around 3MB. 因此,对于1024x768像素的图像,例如,imagecreatetruecolour()将耗尽1024 * 768 * 4 = 3145728字节,或大约3MB。
The problem is that this scales up quite dramatically. 问题是,这种情况非常显着。 A 3072x2034 (6 megapixel) image, by the same sort of calculation, needs around 24MB just to be loaded into memory - I would guess this is the sort of size you're dealing with.
一个3072x2034(600万像素)的图像,通过相同的计算,需要大约24MB才能加载到内存中 - 我猜这是你正在处理的那种大小。
The solution is to hand off the processing to something else like imagemagick or NetPBM that will run as a separate process and not count towards the PHP memory limit. 解决方案是将处理交给像imagemagick或NetPBM这样的其他东西,它将作为一个单独的进程运行,而不是计入PHP内存限制。
NetPBM is my personal favourite and would look something like: NetPBM是我个人的最爱,看起来像是:
anytopnm <file> | pnmscale -xysize <dimensions> | pnmtojpg > <outfile>
don't forget the imagedestroy () function, or caching your thumbnails - they'll save you a LOT of work down the road.
不要忘记imagedestroy ()函数,或缓存你的缩略图 - 它们将为你节省大量的工作。
Did you restart apache after you increased the memory limit? 在增加内存限制后重启apache了吗? If yes, then increase a little more.
如果是,那么再增加一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.