Skip to content

Commit

Permalink
move background to input, forget about digit as svg, change digit ava…
Browse files Browse the repository at this point in the history
…tar to png
  • Loading branch information
frederikbosch committed Aug 1, 2023
1 parent 3b1957b commit dd7d5db
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 78 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ use Genkgo\Favicon;
$outputDirectory = '/var/www/html/favicon';

$input = Favicon\Input::fromFile('/var/www/html/logo.png', InputImageType::PNG);
// or
// or add a different background color
$input = Favicon\Input::fromFile('/var/www/html/logo.png', InputImageType::PNG, '#FF0000');
// or use a svg as input
$input = Favicon\Input::fromFile('/var/www/html/logo.svg', InputImageType::SVG);
// or
$input = Favicon\Input::letter('G', '#FFFFFF', '#00aaad');
// or create a letter avatar
$input = Favicon\Input::digit('G', '#FFFFFF', '#00AAAD');

$generator = new Favicon\FullPackageGenerator(
$input,
'#00AAAD', // background color
'#00AAAD', // theme color
'#00AAAD', // tile color
'Website Title',
'/',
);
Expand All @@ -36,9 +38,9 @@ or use the command-line.
```bash
./vendor/bin/favicon-generator --help
./vendor/bin/favicon-generator 'Title of the website' file:public/logo.png output
./vendor/bin/favicon-generator 'Title of the website' file:public/logo.png output --theme-color=#00AAAD --background-color=#00AAAD --root=/
./vendor/bin/favicon-generator 'Title of the website' file:public/logo.png output --theme-color=#00AAAD --icon-background=#00AAAD --root=/
./vendor/bin/favicon-generator 'Title of the website' letter:G output
./vendor/bin/favicon-generator 'Title of the website' letter:G output --letter-color=#FFFFFF --theme-color=#00AAAD --background-color=#00AAAD --root=/
./vendor/bin/favicon-generator 'Title of the website' letter:G output --letter-color=#FFFFFF --theme-color=#00AAAD --icon-background=#00AAAD --root=/
```

## Default package
Expand Down
10 changes: 5 additions & 5 deletions bin/favicon-generator
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Genkgo\Favicon\FullPackageGenerator;
use Genkgo\Favicon\Input;
use Genkgo\Favicon\InputImageType;

function writeln(string $line, string|int ... $values) {
function writeln(string $line, string|int ... $values): void {
echo sprintf($line, ...$values) . "\n";
}

Expand Down Expand Up @@ -104,7 +104,7 @@ final class CliContext
writeln('2: output directory, defaults to current working directory');
writeln('');
writeln('Options:');
writeln('--background-color Set color under icon, defaults to transparent');
writeln('--icon-background Set color under icon, defaults to transparent');
writeln('--theme-color Color to used in manifest `theme_color` and default letter background color, defaults to #00AAAD');
writeln('--tile-color Color to used in manifest `background_color` and used fill ms-tiles with, defaults to #00AAAD');
writeln('--letter-color Color of the letter, used when using letter: input, defaults to #FFFFFF');
Expand All @@ -129,18 +129,19 @@ final class CliContext
exit(1);
}

$input = Input::fromFile($file, InputImageType::PNG);
$input = Input::fromFile($file, InputImageType::PNG, $context->getOption('icon-background', 'transparent'));
} elseif (str_starts_with($inputString, 'letter:')) {
$letter = substr($inputString, 7);
if (strlen($letter) !== 1) {
writeln('[ERROR] Input letter got only be 1 character, got %s', $letter);
exit(1);
}

$input = Input::letter(
$input = Input::digit(
$letter,
$context->getOption('letter-color', '#FFFFFF'),
$context->getOption('letter-background', $context->getOption('theme-color', '#00AAAD')),
$context->getOption('icon-background', 'transparent'),
);
} else {
writeln('[ERROR] Unknown input string %s. Please start with file: or letter:', $inputString);
Expand All @@ -159,7 +160,6 @@ final class CliContext
$generator = new FullPackageGenerator(
$input,
$context->getOption('theme-color', '#00AAAD'),
$context->getOption('background-color', 'transparent'),
$tileColor,
$name,
$rootPrefix,
Expand Down
4 changes: 1 addition & 3 deletions src/ApplePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ final class ApplePackage implements PackageAppendInterface
{
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly int $size = 180,
) {
}

public function package(): \Generator
{
\var_dump($this->backgroundColor);
$generator = new AppleTouchGenerator($this->input, $this->size, $this->backgroundColor);
$generator = new AppleTouchGenerator($this->input, $this->size);
yield 'apple-touch-icon.png' => $generator->generate();

$generator = AppleSafariPinGenerator::cliDetectImageMagickVersion($this->input);
Expand Down
4 changes: 2 additions & 2 deletions src/AppleSafariPinGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public function __construct(private readonly Input $input, private readonly stri
public function generate(): string
{
if ($this->input->type === InputImageType::SVG) {
return \stream_get_contents($this->input->rewindedFileHandle());
return \stream_get_contents($this->input->newResourceHandle());
}

// If someone knows how to convert a PNG to SVG by using the Imagick extension in PHP, I'd love to know how.
// https://github.com/Imagick/imagick/issues/622
return $this->tempFile(
function ($source, $target) {
$sourceHandle = \fopen($source, 'r+');
\stream_copy_to_stream($this->input->rewindedFileHandle(), $sourceHandle);
\stream_copy_to_stream($this->input->newResourceHandle(), $sourceHandle);
\fclose($sourceHandle);

$descriptor = [
Expand Down
10 changes: 2 additions & 8 deletions src/AppleTouchGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ final class AppleTouchGenerator implements GeneratorInterface
public function __construct(
private readonly Input $input,
private readonly int $size,
private readonly string $backgroundColor = 'transparent',
) {
}

public function generate(): string
{
$imagick = new \Imagick();
$backgroundPixel = new \ImagickPixel($this->backgroundColor);

$imagick->setBackgroundColor($backgroundPixel);
$imagick->readImageFile($this->input->rewindedFileHandle());
$imagick->trimImage(0);
$imagick = $this->input->newImagick();

$newWidth = $imagick->getImageWidth();
$newHeight = $imagick->getImageHeight();
Expand All @@ -30,7 +24,7 @@ public function generate(): string
$composite->newImage($newWidth + 2 * $padding, $newHeight + 2 * $padding, new \ImagickPixel('none'));

$paddingDraw = new \ImagickDraw();
$paddingDraw->setFillColor($backgroundPixel);
$paddingDraw->setFillColor(new \ImagickPixel($this->input->backgroundColor));
$paddingDraw->roundRectangle(
0,
0,
Expand Down
7 changes: 2 additions & 5 deletions src/FullPackageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ final class FullPackageGenerator implements PackageAppendInterface
public function __construct(
private readonly Input $input,
private readonly string $themeColor,
private readonly string $backgroundColor,
private readonly string $tileColor,
private readonly string $name,
private readonly string $rootPrefix = '/',
Expand All @@ -20,22 +19,20 @@ public function __construct(
public function package(): \Generator
{
$generator = new AggregatePackage([
new ApplePackage($this->input, $this->backgroundColor),
new GenericIcoPackage($this->input, $this->backgroundColor),
new ApplePackage($this->input),
new GenericIcoPackage($this->input),
new GenericPngPackage(
$this->input,
$this->name,
$this->shortName ?? $this->name,
$this->themeColor,
$this->rootPrefix,
$this->tileColor,
$this->backgroundColor,
),
new MicrosoftTilePackage(
$this->input,
$this->tileColor,
$this->rootPrefix,
$this->backgroundColor,
),
]);
return $generator->package();
Expand Down
3 changes: 1 addition & 2 deletions src/GenericIcoPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ final class GenericIcoPackage implements PackageAppendInterface
*/
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly array $sizes = [48],
) {
}
Expand All @@ -20,7 +19,7 @@ public function package(): \Generator
{
$first = true;
foreach ($this->sizes as $size) {
$generator = new IcoGenerator($this->input, $this->backgroundColor, $size);
$generator = new IcoGenerator($this->input, $size);
$blob = $generator->generate();
if ($first) {
yield 'favicon.ico' => $blob;
Expand Down
3 changes: 1 addition & 2 deletions src/GenericPngPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function __construct(
private readonly string $themeColor,
private readonly string $rootPrefix = '/',
private readonly ?string $tileColor = null,
private readonly string $backgroundColor = 'transparent',
private readonly array $sizes = [32, 16, 48, 57, 76, 96, 128, 192, 228, 512],
private readonly WebApplicationManifestDisplay $display = WebApplicationManifestDisplay::Standalone,
) {
Expand All @@ -32,7 +31,7 @@ public function package(): \Generator
$first = true;
$manifestFormats = [];
foreach ($this->sizes as $size) {
$generator = new PngGenerator($this->input, $size, $this->backgroundColor);
$generator = new PngGenerator($this->input, $size);
$blob = $generator->generate();
if ($first) {
yield 'favicon.png' => $blob;
Expand Down
7 changes: 1 addition & 6 deletions src/IcoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ final class IcoGenerator implements GeneratorInterface
{
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly int $size = 48,
) {
}

public function generate(): string
{
$imagick = new \Imagick();
$imagick->setBackgroundColor(new \ImagickPixel($this->backgroundColor));
$imagick->readImageFile($this->input->rewindedFileHandle());
$imagick = $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);

$imagick = $this->input->newImagick();
$imagick->scaleImage($this->size, $this->size);
$imagick->setFormat('ico');
$imagick->setImageFormat('ico');
Expand Down
Loading

0 comments on commit dd7d5db

Please sign in to comment.