imagecreatetruecolor

(PHP 4 >= 4.0.6, PHP 5)

imagecreatetruecolorCreate a new true color image

Description

resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() returns an image identifier representing a black image of the specified size.

Depending on your PHP and GD versions this function is defined or not. With PHP 4.0.6 through 4.1.x this function always exists if the GD module is loaded, but calling it without GD2 being installed PHP will issue a fatal error and exit. With PHP 4.2.x this behaviour is different in issuing a warning instead of an error. Other versions only define this function, if the correct GD version is installed.

Parameters

width

Image width.

height

Image height.

Return Values

Returns an image resource identifier on success, FALSE on errors.

Examples

Example #1 Creating a new GD image stream and outputting an image.

<?php
header 
('Content-Type: image/png');
$im = @imagecreatetruecolor(12020)
      or die(
'Cannot Initialize new GD image stream');
$text_color imagecolorallocate($im2331491);
imagestring($im155,  'A Simple Text String'$text_color);
imagepng($im);
imagedestroy($im);
?>

The above example will output something similar to:

Output of example : Creating a new GD image stream and outputting an image.

Notes

Note: This function requires GD 2.0.1 or later (2.0.28 or later is recommended).

See Also

Коментарии

This little function does it for you:

<?
$image_id 
imageCreateFromJPEG($image);
for(
$a=0;$a<imagecolorstotal ($image_id);$a++) 
        {
           
$color ImageColorsForIndex($image_id,$i);
           
$R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
           
$G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
           
$B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
           
ImageColorSet($image_id$a$R$G$B);
        }
imageJPEG($image_id,"$image");
?>

The .299 , .578 , .114 values are estimations, and add a gamma correction to the colors. For more or less luminace in the result, you can change these values.

Goodluck,
Eric Mulders, Netherlands
2001-08-12 13:45:20
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
function ConvertGreyscale($image){
# this file outputs a grey version of specified image

  $total = ImageColorsTotal($image);
  for( $i=0; $i<$total; $i++){
     $old = ImageColorsForIndex($image, $i);
     
     #trying to keep proper saturation when converting
     $commongrey = (int)(($old[red] + $old[green] + $old[blue]) / 3);

     ImageColorSet($image, $i, $commongrey, $commongrey, $commongrey);
  }
}
2002-01-23 21:38:15
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Here is answer for first question 'why do I need true color images'.

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.
2002-02-26 07:40:25
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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

<?php
ob_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
2002-05-25 17:26:32
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Why not just use function_exists? 

http://www.php.net/function_exists

Just because they have version 2.0 GD doesn't mean that they haven't disabled that function.

Also instead of determining which one you have on your machine and then writing setup specific code, you can write a universal code usable on either setups.

::pseudo code::

if (function_exists(imagecreatetruecolor)){
use imagecreatetruecolor()
}else{
use imagecreate()
}
2002-09-15 19:01:32
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Actually GD <2.0 had imagecreattruecolor, it just didn't work :P

This is a better test:

function chkgd2(){
  $testGD = get_extension_funcs("gd"); // Grab function list
  if (!$testGD){ echo "GD not even installed."; exit; }
  if (in_array ("imagegd2",$testGD)) $gd_version = "<2"; // Check
  if ($gd_version == "<2") return false; else return true;
}

if (chkgd2()) echo "<h1>GD2+ is installed.</h1>"; // Test for GD2
else echo "<h1>GD2+ is not installed.</h1>";
2002-09-25 17:49:29
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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.

Then you can check gd2 with this function:

function sysB_chkgd2()
{

$rep=false;
if(isset($GLOBALS["gBGDVersion"]))
    {
    $rep=$GLOBALS["gBGDVersion"];
    }
else
    {
    if(function_exists("gd_info"))
       {
       $gdver=gd_info();
       if(strstr($gdver["GD Version"],"1.")!=false)
          {
          $rep=false;
          }
       else
           {
           $rep=true;
           }
       }
    else
        {
        $size=40;
        $font= your font file path here;
        $b=imagettfbbox ($size,0,$font,"abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ");

        if(($size+1)<abs($b[5]-$b[3]))
           {
           $rep=true;
           }

         }
    $GLOBALS["gBGDVersion"]=$rep;
    }

return $rep;
}
2003-01-14 07:26:24
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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.
2003-01-24 18:40:54
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
The request of gdlib from brad at brwebdesign dot com won't work with PHP < 4.1 (version_compare).

Some phpinfo versions offer the version number without space so you better ask for the dot:

ob_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);
$length = strlen($phpinfo)-1;
$phpinfo=substr($phpinfo,$length);
if($phpinfo<2){
    $dst_img=ImageCreate($new_w,$new_h);}
else {
    $dst_img=ImageCreateTrueColor($new_w,$new_h);
}
2003-02-28 08:15:05
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Here is my solution for creating images with the same code for both GD < 2 and GD > 2:

$dst_img = @imageCreateTrueColor($width, $height);
if (!$dst_img) { $dst_img = imageCreate($width, $height); }
2003-03-08 18:21:21
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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:

$im = ImageCreateFromJPEG($source); 
$new_im = ImageCreate($new_width,$new_height);
   
ImageCopyResized($new_im,$im,0,0,0,0,
$new_width,$new_height,ImageSX($im),ImageSY($im)); 

TO

$im = ImageCreateFromJPEG($source); 
$new_im = ImageCreateTrueColor($new_width,$new_height);
   
ImageCopyResized($new_im,$im,0,0,0,0,$new_width,
$new_height,ImageSX($im),ImageSY($im));
2003-06-08 01:41:39
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
It's good to use the imagecopyresampled- function when creating thumbnails with true colors, it might look better than imagecopyresized..
2003-07-10 06:11:43
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
Regarding choosing the TrueColor or the plain version of imagecreate automatically, I've found that the following works for me:

<?php

$newImg
=@ImageCreateTrueColor($width$height
    or 
$newImg=ImageCreate($width$height); 

?>
2003-10-27 07:40:22
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
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.

function gd_version() {
    static $gd_version_number = null;
    if ($gd_version_number === null) {
        // Use output buffering to get results from phpinfo() 
        // without disturbing the page we're in.  Output 
        // buffering is "stackable" so we don't even have to 
        // worry about previous or encompassing buffering.
        ob_start();
        phpinfo(8);
        $module_info = ob_get_contents();
        ob_end_clean();
        if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
                $module_info,$matches)) {
            $gd_version_number = $matches[1];
        } else {
            $gd_version_number = 0;
        }
    }
    return $gd_version_number;
}

Then you can simply use it something like this:

if (gd_version() >= 2) {
    $image = ImageCreateTrueColor($width, $height);
} else {
    $image = ImageCreate($width, $height);
}
2003-11-18 16:40:37
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
A note to post by Justin Greer @ 18-Nov-2003 10:40:

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:

<?php
if (function_exists('imagecreatetruecolor') {
 
// use imagecreatetruecolor
} else {
 
// use imagecreate
}
2003-12-26 19:47:20
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
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.)
2004-01-08 22:38:20
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
If your hosting system does not have imagecreatetruecolor() because of PHP<4.2 and GD<2.0 then a get around is

<?
$thumb 
imagecreate ($width$height);
imageJPEG($thumb,"images/temp.jpg");
$thumb = @imagecreatefromjpeg("images/temp.jpg");
?>

which creates a thumbnail of right size, saves as a jpeg and then reads it, in true color.

This corrected the degradation caused by using palette images as destination for imagecopyresized ()
2004-05-25 05:46:37
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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);

kai
2004-10-07 12:20:58
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
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);

even this doesn't work for my configuration - fedora core2, php 4.3.8 + gd bundled (2.0.23 compatible) and I have to do this:
$img = imagecreatetruecolor($x, $y);
$bgColor = imagecolorallocate($img, 255,255,255);
imagefilledrectangle($img, 0, 0, $x-1, $y-1, $bgColor);
2004-12-05 06:59:08
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
i dont know if there was an easyer way to do this, but heres my code to convert a .tga image to whatever you want to output it as
it works with basic tga files only

<?
$handle 
fopen("xxx.tga","rb");
$data fread($handle,filesize("xxx.tga"));
fclose($handle);
$pointer 18;
$x 0;
$y 0;
$w fileint(substr($data,12,2));
$h fileint(substr($data,14,2));
$img imagecreatetruecolor($w,$h);

while (
$pointer strlen($data))
{
imagesetpixel($img$x,$y,fileint(substr($data,$pointer,3)));
$x++;

if (
$x==$w)
{
$y++;
$x=0;
}

$pointer += 3;
}

header("Content-type: image/jpeg");
imagepng($img);
imagedestroy($img);

function 
fileint($str)
{
  return 
base_convert(bin2hex(strrev($str)),16,10);
}
?>
2004-12-14 02:14:52
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
OverFlow636, 

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.

Nice job with the code buddy.

function tga2jpg ($image)
{
    $handle = fopen($image, "rb");
    $data = fread($handle, filesize($image));
    fclose($handle);

    $pointer = 18;
    $w = fileint (substr ($data, 12, 2));
    $h = fileint (substr ($data, 14, 2));
    $x = 0;
    $y = $h;

    $img = imagecreatetruecolor($w, $h);

    while ($pointer < strlen($data))
    {
        imagesetpixel ($img, $x, $y, fileint (substr ($data, $pointer, 3)));
       
        $x++;

        if ($x == $w)
        {
            $y--;
            $x = 0;
        }

        $pointer += 3;
    }
   
    for($a = 0; $a < imagecolorstotal ($img); $a++) 
    { 
        $color = imagecolorsforindex ($img, $a); 
           
        $R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']); 
        $G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']); 
        $B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']); 
           
        imagecolorset ($img, $a, $R, $G, $B); 
    }
         
    imagejpeg ($img, 'test.jpg', 100);
    imagedestroy ($img);
}

function fileint($str)
{
    return base_convert (bin2hex (strrev ($str)), 16, 10);
}

tga2jpg ('test.tga');
2005-02-27 01:48:12
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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.

<?php
function imagecreatefromtga $filename$return_array )
{
   
$handle fopen $filename'rb' );
   
$data fread $handlefilesize$filename ) );
   
fclose $handle );
   
   
$pointer 18;
   
$x 0;
   
$y 0;
   
$w base_convert bin2hex strrev substr $data12) ) ), 1610 );
   
$h base_convert bin2hex strrev substr $data14) ) ), 1610 );
   
$img imagecreatetruecolor$w$h );

    while ( 
$pointer strlen $data ) )
    {
       
imagesetpixel $img$x$ybase_convert bin2hex strrev substr $data$pointer) ) ), 1610 ) );
       
$x++;

        if (
$x == $w)
        {
           
$y++;
           
$x=0;
        }

       
$pointer += 3;
    }
   
    if ( 
$return_array )
        return array ( 
$img$w$h );
    else
        return 
$img;
}
?>

Example Usage

<?php
// Get the image & its dimensions
$array imagecreatefromtga("source_image.tga",1);

// the image ( resource image )
$image $array[0];

// its dimensions ( integers )
$dimensions['width'] = $array[1];
$dimensions['height'] = $array[2];

// Delete the image resource array entry to free memory
imagedestroy($array[0]);
?>

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" );

// Declare the content-type as a JPEG image.
header 'Content-type: image/jpeg' );

// Convert the image to JPEG for smaller file size and compatability
imagejpeg $resource_imageNULL100 );
imagedestroy $resource_image );
?>
2005-06-20 21:20:52
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
// Creates a transparent image rather than the default black image
function imageCreateTransparent($x, $y) { 
    $imageOut = imagecreate($x, $y);
    $colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
    imagecolortransparent($imageOut, $colourBlack);
    return $imageOut;
}
2005-06-21 06:17:52
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
If you want a simple way to mirror images, use the function I've included below.

<?php
function image_mirror ($input_image_resource)
{
   
$width imagesx $input_image_resource );
   
$height imagesy $input_image_resource );
   
$output_image_resource imagecreatetruecolor $width$height );
   
$y 1;

    while ( 
$y $height )
    {
        for ( 
$i 1$i <= $width$i++ )
           
imagesetpixel $output_image_resource$i$yimagecolorat $input_image_resource, ( $i ), ( $height $y ) ) );
       
$y $y 1;
    }
   
    return 
$output_image_resource;
}
?>
Example Usage:
<?php

// 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!)

if ( isset( $_GET['file'] ) )
{
   
$filename "./" basename $_GET['file'] ) . ".jpg";
// Making the image resource that needs to be mirrored
// Taking it from a previously exported to JPEG file.
   
$output_resource_image image_mirror imagecreatefromjpeg $filename ) );
    if ( 
imagejpeg $output_resource_image$filename75 ) )
        echo 
"Image resaved successfully";
    else
        echo 
"Image couldn't be written over (Check permissions)!";
}
?>
2005-06-24 22:40:46
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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) ...

$im = imagecreatetruecolor( $w, $h );
imagealphablending( $im, false );
$col = imagecolorallocate( $im, 0, 0, 0, 127 );
imagefilledrectangle( $im, 0, 0, $w, $h, $col );
imagealphablending( $im, true );

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.

Don't miss to disable blending mode and enable savealpha prior to generating a PNG ...

imagealphablending( $im, false );
imagesavealpha( $im, true );
imagepng( $im );
2006-05-22 21:28:03
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
My function to know how much bytes imagecreate or imagecreatetruecolor require before using it.
<?php
function getNeededMemoryForImageCreate($width$height$truecolor) {
  return 
$width*$height*(2.2+($truecolor*3));
}
?>
2006-06-01 16:26:15
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
I had a strange bug occurring under Debian Linux with PHP5.

In file B (where I create the image) I included a file (file A). The image that was produced was always corrupt. When I did not include file A, everything was fine.

After 2 hours of searching I found that there were some extra spaces and linefeeds after the PHP closing tag ?> in file A. After removing those chars, everything works fine again.

Obviously those extra bytes of file A were added to the image and corrupted it.
2006-07-11 12:09:46
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
A very simple and efficient way to create RGB color from hexadecimal (HTML) notation:

<?php

   
function color_hex2dec ($color) {
        return array (
hexdec (substr ($color02)), hexdec (substr ($color22)), hexdec (substr ($color42)));
    }

    list (
$r$g$b) = color_hex2dec ('FFEECC');

?>
2006-07-21 05:52:08
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
imagecreatetruecolor() returns not only image identifier representing a black image of size x_size by y_size.

imagecreatetruecolor() is called to create truecolor images, with an essentially unlimited number of colors (also not only 256 colors).
2006-08-27 04:54:24
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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.)
2007-02-06 16:32:54
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
If you want to create a *transparent* PNG image, where the background is fully transparent, and all draw operations happen on-top of this, then do the following:

<?php
    $png 
imagecreatetruecolor(800600);
   
imagesavealpha($pngtrue);

   
$trans_colour imagecolorallocatealpha($png000127);
   
imagefill($png00$trans_colour);
   
   
$red imagecolorallocate($png25500);
   
imagefilledellipse($png400300400300$red);
   
   
header("Content-type: image/png");
   
imagepng($png);
?>

What you do is create a true colour image, make sure that the alpha save-state is on, then fill the image with a colour that has had its alpha level set to fully transparent (127).

The resulting PNG from the code above will have a red circle on a fully transparent background (drag the image into Photoshop to see for yourself)
2007-05-22 19:50:57
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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().

I have no idea why this should be the case, but it works!
2008-03-10 05:51:54
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
I put this together - combining two of the examples and then generated the text dynamically. but, with this set up, i was able to get the transparent background working as well. 

<?php
// Set the content-type

header('Content-type: image/png');

// Create the image
$im imagecreatetruecolor(17515);
imagesavealpha($imtrue);

// Create some colors
$white imagecolorallocate($im255255255);
$grey imagecolorallocate($im128128128);
$black imagecolorallocate($im000);
imagefilledrectangle($im0015025$black);
$trans_colour imagecolorallocatealpha($im000127);
imagefill($im00$trans_colour);

// The text to draw
$text $_GET['text'];
// Replace path by your own font path
$font 'catriel regular.ttf';

// Add some shadow to the text
imagettftext($im901316$black$font$text);

// Add the text
imagettftext($im901215$white$font$text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
2009-05-28 17:07:20
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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,$img0,00,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$y1);
?>

Taking the opportunity, I thank everyone here at php.net for the great manual and the useful user scripts.
2010-08-26 23:38:22
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
There is no need to allocate colors with imagecreatetruecolor. All [256 x 256 x 256 x 128] true colors are already allocated, and you can use the color indexes directly.

Examples :
Blue => color index 255.
White => color index 16777215 (= 255*256² + 255*256+255).
Full transparent => color index 2130706432 (= 127*256^3).
2013-08-24 20:47:18
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
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.
2014-05-18 23:51:24
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html
Автор:
As of PHP 8, the return type for image creation functions is GdImage instead of a resource. Therefore, people can type hint.
<?php
class MyImage {
private 
GdImage $img;
};
?>
Similarly, <?php
function save_image(GdImage $imgstring $namestring $directory null) : bool
{
/*...*/
};
?>
2021-01-28 09:23:29
http://php5.kiev.ua/manual/ru/function.imagecreatetruecolor.html

    Поддержать сайт на родительском проекте КГБ