Skip to content

Commit

Permalink
add index.html and ability to generate html tags for in your <head> s…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
frederikbosch committed Aug 1, 2023
1 parent dac7bd5 commit 0f9681e
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 35 deletions.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

Requires PHP 8.1+ and both the imagick and DOM extension.

## Install using composer

```bash
$ composer require genkgo/favicon
```


## Create favicon package quick and easy

```php
Expand Down Expand Up @@ -31,6 +38,23 @@ foreach ($generator->package() as $fileName => $contents) {
$pathName = $outputDirectory . '/' . $fileName;
file_put_contents($pathName, $contents);
}

// append the head tags to your document
$document = new DOMDocument('1.0', 'UTF-8');
$html = $document->createElement('html');
$document->appendChild($html);

$head = $document->createElement('head');
foreach ($generator->headTags($document) as $tag) {
$head->appendChild($tag);
}

// or just generate the tag strings
$tags = [];
$document = new DOMDocument('1.0', 'UTF-8');
foreach ($generator->headTags($document) as $tag) {
$tags[] = $document->saveHTML($tag);
}
```

or use the command-line.
Expand All @@ -43,7 +67,7 @@ or use the command-line.
./vendor/bin/favicon-generator 'Title of the website' letter:G output --letter-color=#FFFFFF --theme-color=#00AAAD --icon-background=#00AAAD --root=/
```

## Default package
## Full package

- apple-touch-icon.png
- browserconfig.xml
Expand All @@ -60,18 +84,13 @@ or use the command-line.
- favicon-192x192.png
- favicon-228x228.png
- favicon-512x512.png
- index.html
- mstile-70x70.png
- mstile-150x150.png
- mstile-310x310.png
- safari-pinned-tab.svg
- web-app-manifest.json

## Install using composer

```bash
$ composer require genkgo/favicon
```

## Tests

There are no tests. Maybe later.
2 changes: 1 addition & 1 deletion bin/favicon-generator
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ final class CliContext
$tileColor = $context->getOption('tile-color', '#00AAAD');
$rootPrefix = $context->getOption('root', '/');

$generator = new FullPackageGenerator(
$generator = FullPackageGenerator::newGenerator(
$input,
$context->getOption('theme-color', '#00AAAD'),
$tileColor,
Expand Down
9 changes: 8 additions & 1 deletion src/AggregatePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ public function __construct(private readonly iterable $generators)
}

/**
* @return \Generator<string, string>
* @throws \ImagickException
*/
public function package(): \Generator
{
foreach ($this->generators as $generator) {
yield from $generator->package();
}
}

public function headTags(\DOMDocument $document): \Generator
{
foreach ($this->generators as $generator) {
yield from $generator->headTags($document);
}
}
}
22 changes: 22 additions & 0 deletions src/ApplePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class ApplePackage implements PackageAppendInterface
{
public function __construct(
private readonly Input $input,
private readonly string $themeColor,
private readonly string $rootPrefix = '/',
private readonly int $size = 180,
) {
}
Expand All @@ -20,4 +22,24 @@ public function package(): \Generator
$generator = AppleSafariPinGenerator::cliDetectImageMagickVersion($this->input);
yield 'safari-pinned-tab.svg' => $generator->generate();
}

public function headTags(\DOMDocument $document): \Generator
{
$rootPrefix = $this->rootPrefix;
if (\substr($rootPrefix, -1, 1) === '/') {
$rootPrefix = \substr($rootPrefix, 0, -1);
}

$link = $document->createElement('link');
$link->setAttribute('rel', 'apple-touch-icon');
$link->setAttribute('sizes', $this->size . 'x' . $this->size);
$link->setAttribute('href', $rootPrefix . '/apple-touch-icon.png');
yield $link;

$link = $document->createElement('link');
$link->setAttribute('rel', 'mask-icon');
$link->setAttribute('href', $rootPrefix . '/safari-pinned-tab.svg');
$link->setAttribute('color', $this->themeColor);
yield $link;
}
}
96 changes: 71 additions & 25 deletions src/FullPackageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,81 @@

final class FullPackageGenerator implements PackageAppendInterface
{
public function __construct(
private readonly Input $input,
private readonly string $themeColor,
private readonly string $tileColor,
private readonly string $name,
private readonly string $rootPrefix = '/',
private readonly ?string $shortName = null,
private function __construct(
private readonly AggregatePackage $packages,
private readonly string $title,
private readonly string $themeColor
) {
}

public function package(): \Generator
{
$generator = new AggregatePackage([
new ApplePackage($this->input),
new GenericIcoPackage($this->input),
new GenericPngPackage(
$this->input,
$this->name,
$this->shortName ?? $this->name,
$this->themeColor,
$this->rootPrefix,
$this->tileColor,
),
new MicrosoftTilePackage(
$this->input,
$this->tileColor,
$this->rootPrefix,
),
]);
return $generator->package();
yield from $this->packages->package();

$impl = new \DOMImplementation();
$doctype = $impl->createDocumentType('html');
$document = $impl->createDocument(null, '', $doctype);

$html = $document->createElement('html');
$head = $document->createElement('head');

$meta = $document->createElement('meta');
$meta->setAttribute('http-equiv', 'Content-Type');
$meta->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($meta);

$title = $document->createElement('title', $this->title);
$head->appendChild($title);

foreach ($this->headTags($document) as $tag) {
$head->appendChild($tag);
}

$html->appendChild($head);
$document->appendChild($html);
$document->formatOutput = true;
yield 'index.html' => $document->saveHTML();
}

public function headTags(\DOMDocument $document): \Generator
{
yield from $this->packages->headTags($document);

$meta = $document->createElement('meta');
$meta->setAttribute('name', 'theme-color');
$meta->setAttribute('content', $this->themeColor);
yield $meta;
}

public static function newGenerator(
Input $input,
string $themeColor,
string $tileColor,
string $name,
string $rootPrefix = '/',
?string $shortName = null,
): self
{
return new self(
new AggregatePackage([
new ApplePackage($input, $themeColor, $rootPrefix),
new GenericIcoPackage($input, $rootPrefix),
new GenericPngPackage(
$input,
$name,
$shortName ?? $name,
$themeColor,
$rootPrefix,
$tileColor,
),
new MicrosoftTilePackage(
$input,
$tileColor,
$rootPrefix,
),
]),
$name,
$themeColor
);
}
}
16 changes: 16 additions & 0 deletions src/GenericIcoPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class GenericIcoPackage implements PackageAppendInterface
*/
public function __construct(
private readonly Input $input,
private readonly string $rootPrefix = '/',
private readonly array $sizes = [48],
) {
}
Expand All @@ -29,4 +30,19 @@ public function package(): \Generator
yield 'favicon-' . $size . 'x' . $size . '.ico' => $blob;
}
}

public function headTags(\DOMDocument $document): \Generator
{
$rootPrefix = $this->rootPrefix;
if (\substr($rootPrefix, -1, 1) === '/') {
$rootPrefix = \substr($rootPrefix, 0, -1);
}

foreach ($this->sizes as $size) {
$link = $document->createElement('link');
$link->setAttribute('rel', 'shortcut icon');
$link->setAttribute('href', $rootPrefix . '/favicon-' . $size . 'x' . $size . '.ico');
yield $link;
}
}
}
22 changes: 22 additions & 0 deletions src/GenericPngPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,26 @@ public function package(): \Generator

yield 'web-app-manifest.json' => $manifest->generate();
}

public function headTags(\DOMDocument $document): \Generator
{
$rootPrefix = $this->rootPrefix;
if (\substr($rootPrefix, -1, 1) === '/') {
$rootPrefix = \substr($rootPrefix, 0, -1);
}

foreach ($this->sizes as $size) {
$link = $document->createElement('link');
$link->setAttribute('rel', 'icon');
$link->setAttribute('type', 'image/png');
$link->setAttribute('href', $rootPrefix . '/favicon-' . $size . 'x' . $size . '.png');
$link->setAttribute('sizes', $size . 'x' . $size);
yield $link;
}

$link = $document->createElement('link');
$link->setAttribute('rel', 'manifest');
$link->setAttribute('href', $rootPrefix . '/web-app-manifest.json');
yield $link;
}
}
26 changes: 25 additions & 1 deletion src/MicrosoftTilePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(
private readonly Input $input,
private readonly string $tileColor,
private readonly string $rootPrefix = '/',
private readonly array $sizes = [70, 150, 310],
private readonly array $sizes = [150, 310],
) {
}

Expand All @@ -37,4 +37,28 @@ public function package(): \Generator

yield 'browserconfig.xml' => $browserConfigXml->generate();
}

public function headTags(\DOMDocument $document): \Generator
{
$rootPrefix = $this->rootPrefix;
if (\substr($rootPrefix, -1, 1) === '/') {
$rootPrefix = \substr($rootPrefix, 0, -1);
}

$meta = $document->createElement('meta');
$meta->setAttribute('name', 'msapplication-config');
$meta->setAttribute('content', $rootPrefix . '/browserconfig.xml');
yield $meta;

$meta = $document->createElement('meta');
$meta->setAttribute('name', 'msapplication-TileColor');
$meta->setAttribute('content', $this->tileColor);
yield $meta;

$defaultSize = $this->sizes[\array_key_first($this->sizes)];
$meta = $document->createElement('meta');
$meta->setAttribute('name', 'msapplication-TileImage');
$meta->setAttribute('content', 'mstile-' . $defaultSize . 'x' . $defaultSize . '.png');
yield $meta;
}
}
6 changes: 6 additions & 0 deletions src/PackageAppendInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ interface PackageAppendInterface
* @throws \ImagickException
*/
public function package(): \Generator;

/**
* @param \DOMDocument $document
* @return \Generator<int|string, \DOMElement>
*/
public function headTags(\DOMDocument $document): \Generator;
}

0 comments on commit 0f9681e

Please sign in to comment.