note 54010 added to function.imagecreatetruecolor
| From: | send at mail dot 2aj dot net | Date: | Tue, 21 Jun 2005 01:20:52 +0000 |
| Subject: | note 54010 added to function.imagecreatetruecolor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90713@lists.php.net to get a copy of this message | ||
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 = 0 )
{
$handle = fopen ( $filename, 'rb' );
$data = fread ( $handle, filesize( $filename ) );
fclose ( $handle );
$pointer = 18;
$x = 0;
$y = 0;
$w = base_convert ( bin2hex ( strrev ( substr ( $data, 12, 2 ) ) ), 16, 10 );
$h = base_convert ( bin2hex ( strrev ( substr ( $data, 14, 2 ) ) ), 16, 10 );
$img = imagecreatetruecolor( $w, $h );
while ( $pointer < strlen ( $data ) )
{
imagesetpixel ( $img, $x, $y, base_convert ( bin2hex ( strrev ( substr ( $data, $pointer, 3 ) ) ),
16, 10 ) );
$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_image, NULL, 100 );
imagedestroy ( $resource_image );
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecreatetruecolor.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54010
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54010&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54010&report=yes
Search -- http://master.php.net/manage/user-notes.php