I'm trying to upload image to my localhost right now. It had no problem, If it's a small image(Not over 1 MB). But when i try uploading about 3MB Image. Php said
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18048 bytes) in C:\Users\Tharit\Desktop\Work\Pixelbar\Code\ecompro\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 115
It shouldn't be this much.This is code,in image upload part.
if (Input::hasFile('profile_picture'))
{
$old_profile_pic= $user->profile_pic;
$profile_picture = Input::file('profile_picture');
$profile_picture_size=getimagesize($profile_picture);
if($profile_picture_size[0]>$profile_picture_size[1])
{
$mainside = $profile_picture_size[1];
$cordx=($profile_picture_size[0]-$profile_picture_size[1])/2;
$cordx=(int) $cordx;
$cordy=0;
}
else
{
$mainside = $profile_picture_size[0];
$cordy=($profile_picture_size[1]-$profile_picture_size[0])/2;
$cordy=(int) $cordy;
$cordx=0;
}
$filename = time() .$user->id . '.' . $profile_picture->getClientOriginalExtension();
$path = public_path('img/user/' . $filename);
Image::make($profile_picture->getRealPath())
->crop($mainside,$mainside,$cordx , $cordy)
->resize(100, 100)
->save($path);
$user->profile_pic = 'img/user/'.$filename;
if($old_profile_pic != 'img/user/default_profile.gif')
{$imagecheck=1;}
}
check your php.ini file for this line
upload_max_filesize=1M
and change it to upload_max_filesize=10M
or change "1M" to any other required file size in MBs
There are a number of parameters that you have to set in the php ini to make large file uploads possible. They are:
upload_max_filesize
post_max_size
max_input_time
max_execution_time
memory_limit
You should find helpful comments to set them in the php ini file.
Plus, its a memory error so I think it has nothing to do with file upload. memory_limit should fix it. Try setting memory_limit to -1 to assign unlimited memory and figure out if its really a memory problem.
Related
I am implementing some image manipulation using great intervention library on Laravel 5. It works fine if image is small, like under 700KB with size lower than 800px wide.
But its not able to handle large size images, I want to upload images as big as 8 MB with 2000px wide.
I have tried classic way of bumping the memory_limit but its not working
ini_set("memory_limit","20M");
Is there any solution which can work without killing the server.
Here is the code of my Upload service. It takes image from Request::file('file') and Resize it to 3 sizes using intervention.
Big size which is max width of 2000 px, then a
Medium 800px wide
Thumb is 200px wide
public function photo($file)
{
$me = Auth::user();
//user folder for upload
$userFolder = $me->media_folder;
//Check the folder exist, if not create one
if($this->setupUsrFolder($userFolder)) {
//Unique filename for image
$filename = $this->getUniqueFilename($userFolder);
//Bump the memory to perform the image manipulation
ini_set("memory_limit","20M");
//Generate thumb, medium, and max_width image
$img = Image::make($file);
//big_img
$big = $img->resize(config('go.img.max_width'), null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//Big Image
$big->save($this->getPhotoPath($filename, $userFolder, 'b_'));
//Medium image
$med = $big->resize(config('go.img.med_width'), null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$med->save($this->getPhotoPath($filename, $userFolder, 'm_'));
//Thumbnail
$thumb = $med->resize(config('go.img.thumb_width'), null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$thumb->save($this->getPhotoPath($filename, $userFolder, 't_'));
return $filename;
}
return null;
}
Please help me how I can make it more efficient on and fast
FWIW, the commented solutions didn't work for me. That is, I couldn't get the following to work for me:
Image::make($file)->resize(2000, null)->save('big.jpg')->destroy();
Image::make($file)->resize(800, null)->save('med.jpg')->destroy();
Image::make($file)->resize(200, null)->save('thumb.jpg')->destroy();
It was still throwing the following error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) in
C:\Users\XXX\app\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php on line 136
This solution worked for me was to change the memory limit as follows:
public function resize() {
ini_set('memory_limit', '256M');
// Do your Intervention/image operations...
}
// or...
ini_set('memory_limit', '256M');
Image::make($file)->resize(2000, null)->save('big.jpg');
Image::make($file)->resize(800, null)->save('med.jpg');
Image::make($file)->resize(200, null)->save('thumb.jpg');
This successfully solved the problem.
The reason being:
Resizing images is a very memory consuming task. You can't assume that a 1MB image takes 1MB of memory. Resizing a 3000 x 2000 pixel image to 300 x 200 may take up to 32MB memory for example. Even if the image is under 1MB filesize.
#olivervogel. 2018. Allowed memory size of 134217728 bytes exhausted. [ONLINE] Available at: https://github.com/Intervention/image/issues/567#issuecomment-224230343. [Accessed 14 June 2018].
Just update php.ini file memory_limit to 256M.
memory_limit=256M
I have tried with other memory_limit with 128 and 20. That doesn't work for me. But when I update my memory limit to 256M then the issue solved for me.
In the cpanel, you will find PHP options. From that you need to update the memory limit to:
memory_limit=256M
or
memory_limit=192M
or
memory_limit=368M
or
memory_limit=512M
Hy, I have a page where a user can upload an image (one at a time, the second upload overwrites the first one).
When someone chooses an image which is more than 5 MB, the script will upload the image but it will then run another script that will resize the image to make it smaller.
So, i get the image size, and set the maximum size like so:
$size = $_FILES["userImage"]["size"]
$maxSize = 5000000 //this should be 5 MB, right?
then, I move the image to a folder with this line
move_uploaded_file( $tmpName, "../user_img/".$sessionId."/".$sessionId."_img.".$type);
and in the end, I want to resize the image if it is above 5 MB
if($size > $maxSize){
include('resizeImage2.php');
//indicate which file to resize (can be any type jpg/png/gif/etc...)
$file = "../user_img/".$sessionId."/".$sessionId."_img.".$type;
//indicate the path and name for the new resized file
$resizedFile = $file;
//call the function (when passing path to pic)
//smart_resize_image($file, null, 1200, 0, true, $resizedFile, true, false, 100 );
//call the function (when passing pic as string)
smart_resize_image(null , file_get_contents($file), 1200 , 0 , true , $resizedFile , true , false ,100 );
}
the resizeImage2.php is something I found on the internet at this address
and the problem occurs when I try to upload an image from my PC that is 7MB, and 10004 x 10967 in resolution. I get an error
Allowed memory size of 62914560 bytes exhausted (tried to allocate 40016 bytes)
I know i can change the memory limit in php.ini, but i was wondering if there is another way to make this work? Are any of my scripts badly written?
Thanks in advance!
Try this put it at header on page-
<?php
ini_set('memory_limit','16M');
?>
You can increase this uploaded files limit in your php.ini file, by adding or updating a line similar to the following:
memory_limit = 128M
There are two tricks to increase the max upload size in php
Increasing file upload size by .htaccess
php_value upload_max_filesize 10M
Increasing file upload size by php.ini files
upload_max_filesize = 10M
I have a form that lets users upload files but I'm having problems detecting files that are too large.
I have the following set in php.ini:
upload_max_filesize = 10M
post_max_size = 20M
I understand that the standard way to detect if files are larger than the config file allows is to check $_FILES['file_name']['error']. However this only works for me if the actual file is greater than upload_max_filesize and smaller than post_max_size.
If the actual file size is greater than post_max_size then the $_FILES variable is removed and I get the log message:
PHP Warning: POST Content-Length of xxx bytes exceeds the limit of
yyy ...
So the question: How do I detect if a user is uploading a file that is greater than post_max_size? I need to display a valid user message if this happens.
Thanks.
I suggest you use set_error_handler() and check for E_USER_WARNING error + appropriate message, and then send it to a user, possibly via throwing an Exception, possibly some other way.
Like this:
$filesize = $_FILES['file']['size'];
$limit = ini_get('post_max_size');
if($filesize > $limit) {
trigger_error("POST Content-Length of $filesize bytes exceeds the limit of $limit bytes.", E_WARNING);
}
else {
// upload file
}
I have made a php page that sends an email message with multiple attachments.
The loop which i used to attach multiple attachments and to check the size of attachments is ,
foreach(array_keys($_FILES['attach']['name']) as $key)
{
$filesize = $_FILES['attach']['size'][$key];
$extention = pathinfo ($_FILES['attach']['name'][$key] ,PATHINFO_EXTENSION
);
$name=$_FILES['attach']['name'][$key];
$data=($_FILES['attach']['tmp_name']);
$totalsize = $totalsize + $filesize;
if($totalsize > 10000000) //10mb10000000
{$err="<font color=#990000 size=1>File exceeded maximum allowed limit of 10
Mb</font>";}
else{
$source = $_FILES['attach']['tmp_name'][$key];
$filename = $_FILES['attach']['name'][$key];
$mail->AddAttachment($source, $filename);
}
}//end Foreach loop
But when i try to attach a large file i get this error from the phpmailer class.
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate
7355049 bytes) in /var/www/dev01/maiarn/Email/class.phpmailer.php on line 1677
Any body who can guide me Please.
You might want to increase the PHP memory limit. If you're working on your development machine, you could search for the php.ini file and modify the memory_limit (which often defaults to 16M). Change that to f.e. 128M and restart your webserver.
If you want to see it change, you can use the following line to show the configuration currently in use:
<?php phpinfo(); ?>
Let PHP use more memory on this script only by using the following PHP code:
ini_set(‘memory_limit’,’64M’);
I'm using a simple image manager class, and the following code:
<?php
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($target_path);
if($image->getWidth() > 500) {
$image->resizeToWidth(500);
echo "<p>Image Resized</p>";
} else echo "<p>Image did not need to be resized.</p>";
$image->save($target_path);
echo "<p>Image Saved</p>";
?>
The image is successfully resized when I upload an image with a width of 700, but when I upload a really big picture (width ~= 2300), it doesn't work, and I don't see any of my echo messages.
Do certain php image functions have a size limit that might be causing this?
You are most likely hitting the memory_limit setting specified in php.ini.
Add error_reporting(E_ALL); to your script and see what the output is.
Use phpinfo() to find out the current memory limit setting.
It can sometimes be changed using ini_set("memory_limit", xyz). Otherwise, you need to change php.ini.
A 2300 x 2300 Pixel image is going to take up at least
2300 x 2300 x 3 = 15,870,000
= roughly 16 Megabytes of RAM (or 2300 x 2300 x 4 if there's an alpha channel) so I'd say you would need at least 24 Megabytes of RAM per script to make this work well. Maybe even more.
Check your error log. The chances are that you're exceeding the memory limit (memory_limit in the ini settings). Try adding ini_set('memory_limit', '32M'); to the top of the file.
And to directly answer your question, no there is no size limit on the internal functions...
There is an upload file size limit ; you can to set it in your php.ini :
upload_max_filesize
post_max_size