473,549 Members | 2,935 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 1895

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

Similar topics

3
12347
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
4677
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
2054
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...
5
2197
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...
4
2404
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...
4
6928
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...
2
6695
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
2962
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...
3
3221
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...
0
7518
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...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
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...
1
7469
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...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
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...
0
3498
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1057
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.