Skip to content

Commit

Permalink
improve apple touch icon, add rounded corners
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Jul 31, 2023
1 parent aaa9bbb commit 3b1957b
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 37 deletions.
8 changes: 5 additions & 3 deletions bin/favicon-generator
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ final class CliContext
writeln('2: output directory, defaults to current working directory');
writeln('');
writeln('Options:');
writeln('--background-color Color to used in manifest `background_color` and used fill ms-tiles with, defaults to #00AAAD');
writeln('--background-color 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');
writeln('--letter-background Color of the background, used when using letter: input, defaults to theme color');
writeln('--root Location inside the root directory where the favicons are saved to, defaults to /');
Expand Down Expand Up @@ -152,13 +153,14 @@ final class CliContext
$outputDirectory = getcwd();
}

$backgroundColor = $context->getOption('background-color', '#00AAAD');
$tileColor = $context->getOption('tile-color', '#00AAAD');
$rootPrefix = $context->getOption('root', '/');

$generator = new FullPackageGenerator(
$input,
$backgroundColor,
$context->getOption('theme-color', '#00AAAD'),
$context->getOption('background-color', 'transparent'),
$tileColor,
$name,
$rootPrefix,
);
Expand Down
7 changes: 4 additions & 3 deletions src/ApplePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ final class ApplePackage implements PackageAppendInterface
{
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly int $size = 180,
private readonly ?string $backgroundColor = null,
) {
}

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

$generator = SafariPinGenerator::cliDetectImageMagickVersion($this->input);
$generator = AppleSafariPinGenerator::cliDetectImageMagickVersion($this->input);
yield 'safari-pinned-tab.svg' => $generator->generate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Genkgo\Favicon;

final class SafariPinGenerator implements GeneratorInterface
final class AppleSafariPinGenerator implements GeneratorInterface
{
public function __construct(private readonly Input $input, private readonly string $executable = 'magick')
{
Expand Down
59 changes: 59 additions & 0 deletions src/AppleTouchGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Genkgo\Favicon;

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);

$newWidth = $imagick->getImageWidth();
$newHeight = $imagick->getImageHeight();

$padding = (int)(0.1 * $newWidth);
$composite = new \Imagick();
$composite->newImage($newWidth + 2 * $padding, $newHeight + 2 * $padding, new \ImagickPixel('none'));

$paddingDraw = new \ImagickDraw();
$paddingDraw->setFillColor($backgroundPixel);
$paddingDraw->roundRectangle(
0,
0,
$newWidth + 2 * $padding,
$newHeight + 2 * $padding,
$padding,
$padding
);
$composite->drawImage($paddingDraw);
$composite->setImageFormat('png');
$composite->compositeImage(
$imagick,
\Imagick::COMPOSITE_DEFAULT,
$padding,
$padding,
);

$composite->scaleImage($this->size, $this->size);
$composite->setFormat('png');
$composite->setImageFormat('png');
$composite->setCompression(\Imagick::COMPRESSION_ZIP);
$composite->setImageCompression(\Imagick::COMPRESSION_ZIP);

return $composite->getImagesBlob();
}
}
17 changes: 12 additions & 5 deletions src/FullPackageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ final class FullPackageGenerator implements PackageAppendInterface
{
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor,
private readonly string $themeColor,
private readonly string $backgroundColor,
private readonly string $tileColor,
private readonly string $name,
private readonly string $rootPrefix = '/',
private readonly ?string $shortName = null,
Expand All @@ -19,17 +20,23 @@ public function __construct(
public function package(): \Generator
{
$generator = new AggregatePackage([
new ApplePackage($this->input),
new GenericIcoPackage($this->input),
new ApplePackage($this->input, $this->backgroundColor),
new GenericIcoPackage($this->input, $this->backgroundColor),
new GenericPngPackage(
$this->input,
$this->name,
$this->shortName ?? $this->name,
$this->themeColor,
$this->rootPrefix,
$this->backgroundColor
$this->tileColor,
$this->backgroundColor,
),
new MicrosoftTilePackage(
$this->input,
$this->tileColor,
$this->rootPrefix,
$this->backgroundColor,
),
new MicrosoftTilePackage($this->input, $this->backgroundColor, $this->rootPrefix),
]);
return $generator->package();
}
Expand Down
4 changes: 2 additions & 2 deletions src/GenericIcoPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ final class GenericIcoPackage implements PackageAppendInterface
*/
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly array $sizes = [48],
private readonly ?string $backgroundColor = null,
) {
}

public function package(): \Generator
{
$first = true;
foreach ($this->sizes as $size) {
$generator = new IcoGenerator($this->input, $size, $this->backgroundColor);
$generator = new IcoGenerator($this->input, $this->backgroundColor, $size);
$blob = $generator->generate();
if ($first) {
yield 'favicon.ico' => $blob;
Expand Down
7 changes: 4 additions & 3 deletions src/GenericPngPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function __construct(
private readonly string $shortName,
private readonly string $themeColor,
private readonly string $rootPrefix = '/',
private readonly ?string $backgroundColor = null,
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 @@ -31,7 +32,7 @@ public function package(): \Generator
$first = true;
$manifestFormats = [];
foreach ($this->sizes as $size) {
$generator = new PngGenerator($this->input, $size);
$generator = new PngGenerator($this->input, $size, $this->backgroundColor);
$blob = $generator->generate();
if ($first) {
yield 'favicon.png' => $blob;
Expand All @@ -47,7 +48,7 @@ public function package(): \Generator
$this->name,
$this->shortName,
$this->themeColor,
$this->backgroundColor ?? '#FFFFFF',
$this->tileColor ?? '#FFFFFF',
$manifestFormats
);

Expand Down
9 changes: 2 additions & 7 deletions src/IcoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ final class IcoGenerator implements GeneratorInterface
{
public function __construct(
private readonly Input $input,
private readonly string $backgroundColor = 'transparent',
private readonly int $size = 48,
private readonly ?string $backgroundColor = null,
) {
}

public function generate(): string
{
$imagick = new \Imagick();
if ($this->backgroundColor) {
$imagick->setBackgroundColor(new \ImagickPixel($this->backgroundColor));
} else {
$imagick->setBackgroundColor(new \ImagickPixel('transparent'));
}

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

Expand Down
7 changes: 2 additions & 5 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final class Input
private function __construct(
$file,
public readonly InputImageType $type,
public readonly float $embeddedPadding = 0.0,
public readonly string $backgroundColor = 'transparent',
) {
$this->file = $file;
Expand All @@ -35,28 +34,26 @@ public function rewindedFileHandle()
public static function fromFile(
string $fileName,
InputImageType $type,
float $embeddedPadding = 0.0,
string $backgroundColor = 'transparent',
): self
{
if (!\is_file($fileName)) {
throw new \InvalidArgumentException('File ' . $fileName . ' does not exist');
}

return new self(fopen($fileName, 'r'), $type, $embeddedPadding, $backgroundColor);
return new self(fopen($fileName, 'r'), $type, $backgroundColor);
}

public static function fromString(
string $content,
InputImageType $type,
float $embeddedPadding = 0.0,
string $backgroundColor = 'transparent',
): self
{
$resource = \fopen('php://memory', 'r+');
\fwrite($resource, $content);
\rewind($resource);
return new self($resource, $type, $embeddedPadding, $backgroundColor);
return new self($resource, $type, $backgroundColor);
}

public static function letter(string $letter, string $color, string $backgroundColor): self
Expand Down
2 changes: 1 addition & 1 deletion src/MicrosoftTilePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function __construct(
private readonly Input $input,
private readonly string $tileColor,
private readonly string $rootPrefix = '/',
private readonly string $backgroundColor = 'transparent',
private readonly array $sizes = [70, 150, 310],
private readonly ?string $backgroundColor = null,
) {
}

Expand Down
9 changes: 2 additions & 7 deletions src/PngGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ final class PngGenerator implements GeneratorInterface
public function __construct(
private readonly Input $input,
private readonly int $size,
private readonly ?string $backgroundColor = null,
private readonly string $backgroundColor = 'transparent',
) {
}

public function generate(): string
{
$imagick = new \Imagick();
if ($this->backgroundColor) {
$imagick->setBackgroundColor(new \ImagickPixel($this->backgroundColor));
} else {
$imagick->setBackgroundColor(new \ImagickPixel('transparent'));
}

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

Expand Down

0 comments on commit 3b1957b

Please sign in to comment.