note 99623 added to function.imagecreatetruecolor
| From: | arthur at kuhrmeier dot com | Date: | Fri, 27 Aug 2010 03:38:22 +0000 |
| Subject: | note 99623 added to function.imagecreatetruecolor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172105@lists.php.net to get a copy of this message | ||
HOW THE CHECK THE MEMORY BEFORE CREATING AN IMAGE?
I worked on a script where I delt with large images. Very often the error "out of memory"
occured. So I had to figure out, how to check the memory BEFORE creating an image. I didn't
find a solution on the web, so I ran my own test script. It built lots of images to compute the
needed memory. I found out, that the formula mem = x * y * channel was not enough. There was an
additional multiplier that varied. As a result 1.7 worked best. So the formula is: mem = x * y *
channel * 1.7.
I post this script here hoping it is useful to someone who will run into the same problems.
<?php
//-------------------------------------------------- DEFINE MAXMEM
define ("MAXMEM", 32*1024*1024); //--- memory limit (32M) ---
//-------------------------------------------------- ENOUGH MEMORY ?
function enoughmem ($x, $y, $rgb=3) {
return ( $x * $y * $rgb * 1.7 < MAXMEM - memory_get_usage() );
}
//-------------------------------------------------- SIMPLE EXAMPLE
list ($x, $y) = @getimagesize ('your_img.jpg'); //--- get size of img ---
if (enoughmem($x,$y)) {
$img = @imagecreatefromjpeg ('your_img.jpg'); //--- open img file ---
$thumb = 200; //--- max. size of thumb ---
if ($x > $y) {
$tx = $thumb; //--- landscape ---
$ty = round($thumb / $x * $y);
} else {
$tx = round($thumb / $y * $x); //--- portrait ---
$ty = $thumb;
}
if (enoughmem($tx,$ty)) {
$thb = imagecreatetruecolor ($tx, $ty); //--- create thumbnail ---
imagecopyresampled ($thb,$img, 0,0, 0,0, $tx,$ty, $x,$y);
imagejpeg ($thb, 'your_thumbnail.jpg', 80);
imagedestroy ($thb);
}
imagedestroy ($img);
}
//--------------------------------------------------
//--- to check the memory working with ---
//--- b/w-image or gif use: ---
//--------------------------------------------------
$check = enoughmem ($x, $y, 1);
?>
Taking the opportunity, I thank everyone here at php.net for the great manual and the useful user
scripts.
----
Server IP: 217.160.235.204
Probable Submitter: 79.208.164.244
----
Manual Page -- http://www.php.net/manual/en/function.imagecreatetruecolor.php
Edit -- https://master.php.net/note/edit/99623
Del: integrated -- https://master.php.net/note/delete/99623/integrated
Del: useless -- https://master.php.net/note/delete/99623/useless
Del: bad code -- https://master.php.net/note/delete/99623/bad+code
Del: spam -- https://master.php.net/note/delete/99623/spam
Del: non-english -- https://master.php.net/note/delete/99623/non-english
Del: in docs -- https://master.php.net/note/delete/99623/in+docs
Del: other reasons-- https://master.php.net/note/delete/99623
Reject -- https://master.php.net/note/reject/99623
Search -- https://master.php.net/manage/user-notes.php