Skip to content

marko/media-gd

GD image processing for marko/media --- resize, crop, and convert images using PHP’s built-in GD extension with no additional system dependencies. Covers the common image operations --- resize with aspect ratio preservation, crop by coordinates, format conversion, and thumbnail generation --- without requiring any system-level image library. For advanced needs like AVIF encoding or higher-quality resampling, see marko/media-imagick.

Implements ImageProcessorInterface from marko/media. Requires ext-gd, which ships with most PHP installations. Throws GdProcessingException if the extension is unavailable.

Supported formats: JPEG, PNG, WebP, GIF

Terminal window
composer require marko/media-gd

This automatically installs marko/media.

Register GdImageProcessor as the ImageProcessorInterface implementation in your module.php:

module.php
use Marko\Media\Contracts\ImageProcessorInterface;
use Marko\MediaGd\Driver\GdImageProcessor;
return [
'bindings' => [
ImageProcessorInterface::class => GdImageProcessor::class,
],
];

Resize an image to fit within 800x600, preserving the original proportions:

use Marko\Media\Contracts\ImageProcessorInterface;
class ImageService
{
public function __construct(
private ImageProcessorInterface $imageProcessor,
) {}
public function resizeForWeb(
string $imagePath,
): string {
return $this->imageProcessor->resize(
imagePath: $imagePath,
width: 800,
height: 600,
);
}
}

Pass maintainAspect: false to force exact dimensions:

$outputPath = $this->imageProcessor->resize(
imagePath: $imagePath,
width: 800,
height: 600,
maintainAspect: false,
);

Extract a region starting at pixel coordinates (50, 100), 400px wide and 300px tall:

$outputPath = $this->imageProcessor->crop(
imagePath: $imagePath,
x: 50,
y: 100,
width: 400,
height: 300,
);

Convert an image to WebP:

$outputPath = $this->imageProcessor->convert(
imagePath: $imagePath,
format: 'webp',
);

Accepted format strings: jpeg (or jpg), png, webp, gif. Passing an unsupported format throws GdProcessingException.

Generate a thumbnail where the longest side is at most 150px:

$outputPath = $this->imageProcessor->thumbnail(
imagePath: $imagePath,
maxDimension: 150,
);
  • PHP 8.5+
  • ext-gd (bundled with most PHP installations; enable via php-gd or --with-gd)

If GD is unavailable, instantiating GdImageProcessor throws GdProcessingException with a message explaining how to enable the extension.

Implements ImageProcessorInterface. See marko/media for the interface contract.

MethodDescription
resize(string $imagePath, int $width, int $height, bool $maintainAspect = true): stringResize an image; preserves aspect ratio by default
crop(string $imagePath, int $x, int $y, int $width, int $height): stringExtract a region by pixel coordinates
convert(string $imagePath, string $format): stringConvert to another format (jpeg, png, webp, gif)
thumbnail(string $imagePath, int $maxDimension): stringGenerate a thumbnail constrained to maxDimension on the longest side

All methods return the file path to the processed image (written to the system temp directory).

ExceptionThrown When
GdProcessingExceptionThe GD extension is unavailable, the source image cannot be loaded, or an unsupported format is requested