i got an image upload on my website where I want to control the file
size on my server. So I use imagecreatefromjpeg to create a smaller
image from the one that's uploaded..
You also have the new image (up to 3mb now), plus all of your custom functions, and the current script that is running. Which is all of your variables, calculations, etc. I'm not really sure if the built in functions are held in this allocated memory or not, but I think they are.
A 1.5MB image will consume a lot of memory when loaded with the gd library. There's a lot of additional stuff it does in memory.
You'll have to increase the memory allocated to PHP in php.ini.
The setting is: "memory_limit." Set it to something like: 16M or 32M if you can.
(The error is telling you it couldn't allocate the amount of memory needed above the amount already
in use. Yes, the amount noted in the error is less than your current memory limit.)
If your hosting account doesn't allow you to have custom php.ini setttings or you don't control the box, you'll have to find a different solution.
One possible solution is to detect if the file size is greater than some amount and if it is, pass the
image out to a different server with the requisite memory allocated to PHP - possibly a server
running at your home. Then send the image back to the hosting server after it's been processed
to a special URL that only your home server can access (password protection, etc.).
Obviously another solution is to get a different host.
Another way is to limit the pixel size of the images uploaded.
The way GD library works the imagecreatefromjpeg function is like this:
pixel width of image * pixel height of image * 5 = memory consumption.
So, if you indeed have an 8mb memory limit, you probably have 8.000.000 bytes to play with.
Subtract 50.000 for other script functionality, leaves 7.950.000.
This means the maximum amount of pixels in your uploaded image is (7.950.000/5) = 1.590.000.
The maximum pixel resolution is then 1260x1260 (for a square one)
So, to solve the problem as it is, retrieve the height and width of the uploaded image, multiply them with each other and again with 5, see if it's less then 7.950.000 and continue with the script. If it's more then that 7.950.000, exit the script and let them resize the picture before continuing.
/edit: I've only just seen that it is actually 8MB, not 8MB as in 8.000.000 bytes. So yeah, adapt the numbers a bit, but the theory is still the same.
I think storing the image in the database is always not advisable because ur DB may bulge in size as you insert more than very few images.
It is rather easy to store them in a separate folder and just store the filename in the database and you can use these filenames whereever u want to display the images.
@giriprasad.gn: where did you find i'm storing in a DB?.. only the name of the picture is stored in a DB
not the whole image. That is stored in my image folder.