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.

Because imagecreatetruecolor exist both in gd and gd2,you can't use function_exists or get_extension_funcs to check for gd2 !But ... there is a difference in imagettfbbox between gd and gd2:gd use pixels and gd2 use points.

If you want to place an image on a larger canvas you've previously created with imagecreatetruecolor(), but you don't want the default black background to surround it: use imagefill() AFTER imagecopyresampled().

Just a small rectification for the msg by 'thomas dot urban at toxa dot de' (23-May-2006 03:28) about transparency.Line 3 in the example returns a 'wrong parameter warning': 'imagecolorallocate' should be replaced by 'imagecolorallocatealpha'.(You better just alter that note and delete this one.)

// A good use of this is when a user uploads a TGA file,// on the completion screen show the image and have a link// that asks if the image appears mirrored, and if so// that link will execute these commands below.

// I suggest checking $HTTP_REFERER for security.

// $_GET['file'] in this case would be something along the lines// of johndoe-img0001 (Basename is necessary to prevent // abuse of this script!)

Thanks to OverFlow636 at gmail dot com and kuya1284 at techie dot com for their original coding which this function is based upon.

PHP lacks an imagecreatefromtga function, so I have prepared a simple function to include in your library if you deal with TGA images. In addition to creating an image from TGA, it also, optionally, will output an array with the dimensions of the image because getimagesize() does not function with TGA files.

Hopefully this function will eventually be unneeded if GD is ever updated to support the TGA format.

Please keep in mind that TGA's come in many different compressions, color settings, and as a result this function will not work 100% of the time. Resulting images will fit the symptoms kuya1284 at techie dot com mentioned, and as a result you may need to create a second function. Keep in mind that using her code causes the "mirror" effect on images that load properly with the following function.

The second variable, $return_array is optional. If you simply want to load a TGA, leave the variable off the function's call as such:<?// Return the resource image alone$resource_image = imagecreatefromtga ( "source_image.tga" );

Please note that if you are receiving a warning about incorrect dimensions in PHP, it could be because your values are being stored as strings. You can use intval() to change a string value into an integer value which will pass the correct information into this function.

It appears that your code was causing TGA's to be inverted horizontally and upside down. I modified your code accordingly. The sample below is an example of what I did to convert a tga to jpg. The for loop was taken from the comment below (Eric Mulders) to properly render the image.

kai wrote://using imagecolorallocate to specify the image's background//color does not work with truecolor-image.////instead you have to use imagefill to force flood-filling the//image with the backgorund-color previously allocated:////$bgColor = imagecolorallocate($img, 255,255,255);//imagefill($img , 0,0 , $bgColor);

While this information has already been posted by php at REMOVEreallynicejerk dot com @ 16-Sep-2002 12:01, I feel it neccesary to notice 'cause Justin's post is in the top and it can make people go the wrong way: Justin's way of detecting which imagecreate function to use is too complicated, while there's an easy standard way:

I just fiddled around with Phpix, which had some problems using the GD lib
(washed out colours).
I changed the lines, which did the trick.
Just in case some of you are still having problems and
you can't change the PHP-install
on the hosting side:

I know it's not a discussion board, but when incorrect info is posted, it should be corrected.

The function_exists() check does not work correctly because if PHP is compiled with an older GD library, the imagecreatetruecolor() function still exists -- it just gives a fatal error when called, stating that GD2 is required. Therefore, the function_exists() method will fail on any new-ish copy of PHP that only has GD 1.x. (That includes most of the 4.1.x and 4.2.x installs I've seen.)

Unfortunately the @imagecreatetruecolor() method doesn't even work because php dies with a fatal error noting that GD 2 is required. You can't even capture this error with a custom error handler.

I have come up with a function to get the GD version number that seems to work pretty well on every version of PHP and GD I've thrown at it (even CLI versions.) It's obviously not the most efficient thing in the world, but it does cache the result in a static variable so calling it multiple times doesn't slow down further.

I discovered when installing GD 2+ that ImageCreate() doesn't work well with JPEGS, it makes a true colour JPEG into a 16 colour mess when combining ImageCreateFromJPEG(). If you have GD 2+ you must use ImageCreateTrueColor() for things like thumbnails, etc.

I came up with this today. You need GD2.0 or greater to use imagecreatetruecolor so you can parse phpinfo to get the needed information. this will have to go at the very top of the page with no whitespace above it

<?phpob_start();phpinfo(8);$phpinfo=ob_get_contents();ob_end_clean();$phpinfo=strip_tags($phpinfo);$phpinfo=stristr($phpinfo,"gd version");$phpinfo=stristr($phpinfo,"version");$end=strpos($phpinfo," ");$phpinfo=substr($phpinfo,0,$end);$phpinfo=substr($phpinfo,7);if(version_compare("2.0", "$phpinfo")==1)echo "you have a version less then 2";?>

The if statement is for PHP 4.1 or greater, but you can use other methods of comparing the version numbers if the server you are on do not have that version of php

True color images you need when you want to work with images such as photos (snapshots), video frames and so on.

When you need to combine some these images for user, or to send him only small part of your image, and you try to use 256 colors, image will be ugly (try it if you want). Some colors will change dramatically.

"Antialias" lines which you may see are probably the result of using JPEG compression. JPEG is not looseless compression so "small image details may be changed" in order to reduce image size dramatically. On really true-color images such as your snapshots of landscape and so on and using small compression level you will hardly see differences. But on exact objects, such as lines, circles, which you draw on solid background using single color, you can see that if you save and load this image that some details are changed.

This is another approach to get an initially transparent truecolor canvas and in comparison to the one below it does not declare black to be transparent (which might be okay on indexed-color images and unless you want to draw in black) ...

Without disabling blending mode this would paint transparent rectangle over current background thus not changing anything. Nevertheless blending mode is required e.g. to get proper results from using imagettftext ... that's why it is enabled after filling, again.