Skip to content

Instantly share code, notes, and snippets.

@n1215
Last active April 1, 2021 01:37
Show Gist options
  • Save n1215/0db32cc19bebdc5bee6c5a1e3de1b738 to your computer and use it in GitHub Desktop.
Save n1215/0db32cc19bebdc5bee6c5a1e3de1b738 to your computer and use it in GitHub Desktop.
cakephperize
<?php
declare(strict_types=1);
/**
* (experimental)
* cakephperize your icons
*
* usage)
* $ php cakephperize.php {file_url} {threshold}
* example)
* $ php cakephperize.php https://pbs.twimg.com/profile_images/1231114994550370304/bYwYKuJY_400x400.png 0.8
*/
const CAKEPHPER_BLUE = '#006CF3';
const CAKEPHPER_BLACK = '#000000';
$imageUrl = $argv[1] ?? null;
if ($imageUrl === null) {
echo '画像URLを指定してください。ex) php cakephperize.php https://example.com/test.png 0.8' . PHP_EOL;
exit(1);
}
$threshold = (float) ($argv[2] ?? 0.5);
$contents = file_get_contents($imageUrl);
if (!$contents) {
echo "画像を読み込めません {$imageUrl}" . PHP_EOL;
exit(1);
}
$img = new Imagick();
try {
$img->readImageBlob($contents);
} catch (Exception $e) {
echo "画像の読み込みに失敗しました {$imageUrl}" . PHP_EOL;
exit(1);
}
$img->thresholdImage($threshold * Imagick::getQuantum());
$imageIterator = $img->getPixelIterator();
foreach ($imageIterator as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
$newColor = (int) $pixel->getHSL()['luminosity'] === 0 ? CAKEPHPER_BLUE : CAKEPHPER_BLACK;
$pixel->setColor($newColor);
}
$imageIterator->syncIterator();
}
$writeFileHandle = fopen(__DIR__ . '/cakephperized.png', 'w');
$img->writeImageFile($writeFileHandle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment