473,898 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = imagecreatefrom jpeg($this->imageName);

2) I call a method that resized the image

$this->resizeImage( );

this is the code of the method:

*************** ************
$this->dest_image = imagecreatetrue color($thumb_w, $thumb_h);
imagecopyresize d($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->resizedImageNa me);

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.bestbostont errier.it/home/sNews/ImageResizeClas s.php on
line 57.
*************** *************** ***************

So, I read some comment in php.net about imagecreatefrom jpeg()
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('P ATH/TO/YOUR/IMAGE');
$memoryNeeded = round(($imageIn fo[0] * $imageInfo[1] *
$imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);

if (function_exist s('memory_get_u sage') && memory_get_usag e() +
$memoryNeeded > (integer) ini_get('memory _limit') * pow(1024, 2)) {

ini_set('memory _limit', (integer) ini_get('memory _limit') +
ceil(((memory_g et_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 1922

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

Similar topics

3
12375
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 1677706 bytes) in /var/html/www/tiki/lib/filegals/filegallib.php on line 30 Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to
2
4697
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 = mysql_query($this->query, $this->dbcnx) or die(@include("$DOCUMENT_ROOT${devpath}errors/query_error.php")); It is found here in this class method:
3
2067
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 memory size of 16777216 bytes exhausted (tried to allocate 42 bytes) in /usr/share/pear/DB/mysql.php on line 290 OK I understand what this means (I assume) that the program is trying to allocate more memory to return my query result but it wont...
5
2222
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 as the following (edited for length, repetitions, etc): TopMemoryContext: 32792 total in 4 blocks; 9712 free (2 chunks); 23080 used TopTransactionContext: 8192 total in 1 blocks; 8176 free (0 chunks); 16 used DeferredTriggerXact: 0 total in 0...
4
2417
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 pixel resolution from the image file (without loading the entire image into memory on the server) to allow it to properly size the table cell and its associated Image control? Thanks, Bruce
4
6945
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 allocate 12288 bytes) in /home/httpd/vhosts/monkeyclaus.org/httpdocs/media/audio/pdsIncludes/resizeImageToSquare.php on line 82 How can I protect against out-of-memory errors?
2
6727
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 address: http://it.mobi.dada.net/lostpassword.php end it isn't very complicated: <form method="post" action="/cgi-bin/hotw/sendpassword.chm">
1
2985
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 both hardware and software, running the same scripts that used to be fine with 8 MB hit the roof all the time. The maximum memory allocation was set to 128 MB and still PHP encounters fatal errors with allowed memory size exhausted. Pardon my french,...
3
3242
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. take the job and check in the database that job exist in database or not. If existed then update the visited date for that job. otherwise add new job record in database. Currently our database has 11422185 job records. And all are in one table. but...
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10857
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10946
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9658
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5877
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4705
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.