473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Manage image without exhausted memory

**************************************************
Manage image without exhausted memory
**************************************************

Hi;
I have a big problem. I must create a script that upload an image an
then resize it, if width or height are more than 250px.
Now, after upload an image (in this example I suppose that it's a JPEG
image):

1) I create an image identifier representing the image obtained from
the given filename. In this case the given filename is the image
uploaded (I remember that an user can upload a very big image, but I
control that it isn't more than 1 MB).

$this->src_image = imagecreatefromjpeg($this->imageName);

2) I call a method that resized the image

$this->resizeImage();

this is the code of the method:

***************************
$this->dest_image = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresized($this->dest_image, $this->src_image, 0, 0, 0, 0,
$thumb_w, $thumb_h, $old_x, $old_y);
**************************

In this code show above I create an image identifier representing the
new image that I want resized, and then copy and resize image from
image uploaded.

3) I create the image file resized

imagejpeg($this->dest_image, $this->resizedImageName);

But, if I upload a image file, and its size is more than 512KB, the
script crash an this is the error message:

*********************************************
Fatal error: Allowed memory size of 19922944 bytes exhausted (tried to
allocate 1000 bytes) in
/web/htdocs/www.bestbostonterrier.it/home/sNews/ImageResizeClass.php on
line 57.
*********************************************

So, I read some comment in php.net about imagecreatefromjpeg()
function, where the script crash. A user say this:

*******************************************

In addition to yaroukh at gmail dot com comment.

It seems that even a small image can eat up your default memory limit
real quick. Config value 'memory_limit' is marked PHP_INI_ALL, so you
can change it dynamically using ini_set. Therefore, we can "allocate
memory dynamically", to prevent those memory limit exceeded errors.

<?php

$imageInfo = getimagesize('PATH/TO/YOUR/IMAGE');
$memoryNeeded = round(($imageInfo[0] * $imageInfo[1] *
$imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);

if (function_exists('memory_get_usage') && memory_get_usage() +
$memoryNeeded > (integer) ini_get('memory_limit') * pow(1024, 2)) {

ini_set('memory_limit', (integer) ini_get('memory_limit') +
ceil(((memory_get_usage() + $memoryNeeded) - (integer)
ini_get('memory_limit') * pow(1024, 2)) / pow(1024, 2)) . 'M');

}

?>

***********************************************
So, before resize an image, I ensure that there is enough memory
available to executes the script.
Not often, but in some case (image size > 512KB), the crash the
script!!!
Anyone can help me?

Jan 3 '06 #1
0 1874

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul | last post by:
Hi, I'm using TikiWiki and was using its file upload feature, trying to upload a 2MB file. I get this error: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate...
2
by: Phil Powell | last post by:
Actually the client is saying it sometimes happens and sometimes doesn't happen, and when they refresh their screen it clears itself (I assume the memory clears). Here is line 1135: $result =...
3
by: Dave Smithz | last post by:
Hi there, I changed one of my MySQL search queries to allow a situation where the user gets much more information returned. However, it has now given me the error message: Fatal error: Allowed...
5
by: Aaron Birkland | last post by:
I have a long but straightforward query (on some very large tables) that always ends in 'Memory exhausted in AllocSetAlloc(108)'. Even stranger are some messages that appear in the logfile, such...
4
by: Bruce | last post by:
I have an asp.net page that is dynamically building a table and populating cells with images (typically jpeg) and with associated metadata. How can the asp.net code efficiently determine the...
4
by: lawrence k | last post by:
I've a jpeg image that is 514k, which doesn't strike me as very large. Yet I'm running out of error when I try to resize it: Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to...
2
by: Giuseppe | last post by:
Hi everybody, this is my first post in this group since it is from little time that I have begun to learn c++. My problem is to post a web form using the libcurl library. The form is at this...
1
by: Kimmo Laine | last post by:
Hi! We've encountered a strange problem concerning memory usage. In the previous install the maximum memory amount per page was limited to 8 MB and it was never reached. Now, after upgrading...
3
by: jitendrarathod | last post by:
Hello, I have a one PHP script which crawled the URLs and get the job from the URLs. My logic is so simple. First take the URL from database(total 25000 URLs). Then start to crawl the URLs....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.