Last active
April 1, 2021 01:37
-
-
Save n1215/0db32cc19bebdc5bee6c5a1e3de1b738 to your computer and use it in GitHub Desktop.
cakephperize
This file contains 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 | |
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