Content · Web Development · PHP

PHP imagepng() Function: Usage and Examples

Saves or streams an image as PNG with lossless compression.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Saves or streams an image as PNG with lossless compression.

What It Does


imagepng() is lossless and supports alpha; JPEG is lossy and has no alpha; AVIF needs codec support. Compression levels range 0–9 and affect size/speed, not visual quality.

Requirements and Compatibility


GD extension. Sample images and fonts referred to by filename must be supplied locally. Use PHP 8 GdImage objects where applicable; optional decoders depend on the installed build.

Syntax, Inputs and Results


The arguments shown below are concrete values for this function; their types and ranges matter. The example checks return data rather than assuming that every operation succeeds.

Example 1: Basic Use


Call the function with a concrete input and inspect its result.

<?php
if (!extension_loaded('gd')) throw new RuntimeException('GD extension required');
$im = imagecreatetruecolor(160, 110);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 159, 109, $white);
if (!imagepng($im, __DIR__ . '/out.png', 6)) throw new RuntimeException('Encoding failed');
echo 'File written';

Example 2: Another Case


Change the inputs to observe the behavior on another case.

<?php
if (!extension_loaded('gd')) throw new RuntimeException('GD extension required');
$im = imagecreatetruecolor(160, 110);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 159, 109, $white);
if (!imagepng($im, __DIR__ . '/icon.png', 9)) throw new RuntimeException('Encoding failed');
echo 'File written';

Practical Example


Use the result to create an output, validate a condition, or complete an image workflow.

<?php
if (!extension_loaded('gd')) throw new RuntimeException('GD extension required');
$im = imagecreatetruecolor(160, 110);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 159, 109, $white);
ob_start();
$ok = imagepng($im, null, 6);
$bytes = ob_get_clean();
if (!$ok || !is_string($bytes) || $bytes === "") throw new RuntimeException("Image encoder failed");
echo strlen($bytes), " encoded bytes";

For production, apply the compatibility and edge-case limits described below.

Important Behavior and Edge Cases


Compression levels range 0–9 and affect size/speed, not visual quality.

Comparison with Related Functions and Tools


SubjectKey difference
imagepng()imagepng() is lossless and supports alpha; JPEG is lossy and has no alpha; AVIF needs codec support.

Common Beginner Mistakes


MistakeCorrect approach
Calling imagepng() without its required input or supported extensionCheck the extension, supply valid arguments, and handle false or exceptions according to the function.
Assuming imagepng() guarantees a valid or safe imageCompression levels range 0–9 and affect size/speed, not visual quality.
Confusing the operation with a related APIimagepng() is lossless and supports alpha; JPEG is lossy and has no alpha; AVIF needs codec support.

Key Takeaways


  • Saves or streams an image as PNG with lossless compression.
  • Compression levels range 0–9 and affect size/speed, not visual quality.
  • Choose the companion API described in the comparison table when the job differs.

Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.

Share this resourceSend this page to someone who may find it useful.