All formats

Imagick

LanguagePHP (C extension)
LicensePHP License 3.01 (ImageMagick itself: ImageMagick License)
Repositorygithub.com/Imagick/imagick
DocumentationImagick on php.net
Depends onImageMagick (MagickWand)
Read / writeyes / yes, all TGA image types, raw or RLE

Imagick exposes ImageMagick to PHP. It reads every TGA variant (palette, grayscale, 15/16/24/32-bit, raw or RLE) and writes TGA with or without RLE compression, so it covers what GD can’t: writing TGA and opening non-true-color files.

It needs the ImageMagick library installed on the server; shared hosting doesn’t always have it.

Installation#

Shell
# Debian / Ubuntu
sudo apt install php-imagick
# macOS (Homebrew PHP)
brew install imagemagick
pecl install imagick

On Windows, download the DLL matching your PHP build from PECL and enable extension=imagick in php.ini.

Basic usage: convert TGA to PNG#

PHP
<?php
try {
    $image = new Imagick('in.tga');
    printf("%d x %d\n", $image->getImageWidth(), $image->getImageHeight());
    $image->setImageFormat('png');
    $image->writeImage('out.png');
} catch (ImagickException $e) {
    echo 'error: ', $e->getMessage(), "\n";
}

How to convert PNG to TGA with RLE compression#

PHP
<?php
$image = new Imagick('in.png');
$image->setImageFormat('tga');
$image->setImageCompression(Imagick::COMPRESSION_RLE);  // COMPRESSION_NO for raw
$image->writeImage('out.tga');

How to get TGA width and height without decoding#

pingImage reads only the header:

PHP
<?php
$image = new Imagick();
$image->pingImage('in.tga');
printf("%d x %d, alpha: %s\n",
    $image->getImageWidth(), $image->getImageHeight(),
    $image->getImageAlphaChannel() ? 'yes' : 'no');

How to read TGA data from a string#

TGA has no magic bytes, so set the format before reading a blob:

PHP
<?php
$data = file_get_contents('texture.bin');
$image = new Imagick();
$image->setFormat('tga');
$image->readImageBlob($data);

How to create a TGA thumbnail#

PHP
<?php
$image = new Imagick('in.tga');
$image->thumbnailImage(256, 0);  // height 0: keep the aspect ratio
$image->setImageCompression(Imagick::COMPRESSION_RLE);
$image->writeImage('thumb.tga');

Tips#