All formats

GD

LanguagePHP (C extension)
LicensePHP License (bundled libgd: GD license, permissive)
Repositorygithub.com/php/php-src (ext/gd)
Documentationimagecreatefromtga
Depends onnothing extra (TGA support is built into GD)
Read / writeyes / no; 24-bit and 32-bit (8-bit alpha) true-color, raw or RLE

GD ships with PHP and is enabled on most hosts. Since PHP 7.4, imagecreatefromtga() reads TGA files into a GD image, which you can then resize and save as PNG, JPEG, WebP or AVIF. GD has no TGA writer.

PHP’s bundled GD reads only true-color TGA: 24-bit files and 32-bit files with 8 alpha bits, uncompressed or RLE. Palette, grayscale and 15/16-bit files fail to load; use Imagick for those.

Installation#

GD is bundled with PHP. On Linux distributions it’s usually a separate package:

Shell
# Debian / Ubuntu
sudo apt install php-gd
# Fedora
sudo dnf install php-gd

Check that it’s loaded with php -m | grep gd.

Basic usage: convert TGA to PNG#

PHP
<?php
$image = imagecreatefromtga('in.tga');
if ($image === false) {
    exit("could not read in.tga\n");
}
printf("%d x %d\n", imagesx($image), imagesy($image));

imagesavealpha($image, true);  // keep the alpha channel in the PNG
imagepng($image, 'out.png');

How to convert TGA to JPEG#

JPEG has no alpha, so draw the image on a white background first:

PHP
<?php
$src = imagecreatefromtga('in.tga');
$w = imagesx($src);
$h = imagesy($src);
$dst = imagecreatetruecolor($w, $h);
imagefill($dst, 0, 0, imagecolorallocate($dst, 255, 255, 255));
imagecopy($dst, $src, 0, 0, 0, 0, $w, $h);
imagejpeg($dst, 'out.jpg', 90);

How to make a thumbnail of a TGA#

PHP
<?php
$src = imagecreatefromtga('in.tga');
$thumb = imagescale($src, 256);  // width 256, height keeps the aspect ratio
imagesavealpha($thumb, true);
imagewebp($thumb, 'thumb.webp', 85);

How to check if GD can read TGA#

PHP
<?php
$info = gd_info();
var_dump($info['TGA Read Support'] ?? false);

Tips#