Last active
January 9, 2025 19:33
-
-
Save chsxf/76cbf62424570c1b5adef3f990433792 to your computer and use it in GitHub Desktop.
JPEG vs PNG vs AVIF vs WebP Comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---- | |
Image: horizon | |
Original Size: 212798 | |
Dimensions: 1000 x 1000 px | |
Format: jpeg | |
Output size: 164895 | |
Format: png | |
Output size: 1672103 | |
Format: avif | |
Output size: 148628 | |
Format: webp | |
Output size: 123402 | |
---- | |
Image: minit | |
Original Size: 147531 | |
Dimensions: 1000 x 1000 px | |
Format: jpeg | |
Output size: 123602 | |
Format: png | |
Output size: 595040 | |
Format: avif | |
Output size: 74898 | |
Format: webp | |
Output size: 75006 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$images = [ | |
'horizon' => 'https://cdn-images.dzcdn.net/images/cover/f0ae18d5eaf49aed9a0ac3d8cf558955/1000x1000-000000-80-0-0.jpg', | |
'minit' => 'https://cdn-images.dzcdn.net/images/cover/b6c9a16d838f0d4fc7c5502c55eb628e/1000x1000-000000-80-0-0.jpg' | |
]; | |
foreach ($images as $name => $url) { | |
if (!is_dir($name)) { | |
mkdir($name); | |
} | |
printf("\n----\n"); | |
printf("Image: %s\n", $name); | |
$data = file_get_contents($url); | |
file_put_contents("{$name}/original.jpg", $data); | |
printf(" Original Size: %d\n", strlen($data)); | |
$img = imagecreatefromstring($data); | |
printf(" Dimensions: %d x %d px\n", imagesx($img), imagesy($img)); | |
test_formats($name, $img); | |
imagedestroy($img); | |
} | |
function test_formats(string $name, GdImage $image) { | |
foreach (['jpeg', 'png', 'avif', 'webp'] as $format) { | |
printf(" Format: %s\n", $format); | |
ob_start(); | |
$params = [$image, null, $format == 'png' ? 9 : 60]; | |
if ($format == 'avif') { | |
$params[] = 0; | |
} | |
$funcName = "image{$format}"; | |
call_user_func_array($funcName, $params); | |
$result = ob_get_clean(); | |
printf(" Output size: %d\n", strlen($result)); | |
file_put_contents("{$name}/result.{$format}", $result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment