Created
July 21, 2023 09:16
-
-
Save lintaba/90a7f44de7cc0e4a5612089bdffaf2ec to your computer and use it in GitHub Desktop.
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 | |
$file = $argv[1]; | |
$threashold = $argv[2] ?? 5; | |
// Load image | |
$image = imagecreatefrompng($file); | |
if (!$image) { | |
exit(2); | |
} | |
$width = imagesx($image); | |
$height = imagesy($image); | |
echo 'file:'.$file.', '.$width.'px X '.$height.'px'.PHP_EOL; | |
// Convert RGB values to hex | |
function rgb2hex(array $c) | |
{ | |
$g = $c['green']; | |
$b = $c['blue']; | |
$r = $c['red']; | |
$a = $c['alpha']; | |
if ($a > 0) { | |
return sprintf('rgba(%d,%d,%d,%d)', $r, $g, $b, $a); | |
} | |
return sprintf('#%02x%02x%02x', $r, $g, $b); | |
} | |
$a = []; | |
// Get color at left (0,0) and right (width,0) edges | |
$lc = null; | |
for ($i = 0; $i < $height; $i++) { | |
$color = imagecolorsforindex($image, imagecolorat($image, 0, $i)); | |
for ($j = 1; $j < $width; $j++) { | |
$colorj = imagecolorsforindex($image, imagecolorat($image, $j, $i)); | |
if (rgb2hex($color) != rgb2hex($colorj)) { | |
echo "ERR: " . $j . ',' . $i . ' color differs in row: ' . rgb2hex($color) . ', ' . rgb2hex($colorj) . PHP_EOL; | |
exit(1); | |
} | |
} | |
if ($i > 1 && $i < $height - 1) { | |
$next = imagecolorsforindex($image, imagecolorat($image, 0, $i + 1)); | |
$delta = abs($next['red'] - $color['red'] - ($color['red'] - $lc['red'])) + | |
abs($next['green'] - $color['green'] - ($color['green'] - $lc['green'])) + | |
abs($next['blue'] - $color['blue'] - ($color['blue'] - $lc['blue'])) + | |
abs($next['alpha'] - $color['alpha'] - ($color['alpha'] - $lc['alpha'])); | |
if ($delta < $threashold) { | |
echo "\033[0;33m".$delta."\033[0m "; | |
$lc = $color; | |
continue; | |
} | |
echo "\033[1;31m".$delta."\033[0m "; | |
} | |
$lc = $color; | |
$a[] = rgb2hex($color) . ' ' . round(($i) / ($height - 1) * 100, 1) . '%'; | |
} | |
echo PHP_EOL; | |
echo 'background: linear-gradient(to bottom, ' . implode(', ', $a) . ');'; | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment