Skip to content

Commit

Permalink
Provide method of extracting the largest image from the manifest.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacerider committed Feb 6, 2017
1 parent 27bde8e commit cc65444
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/Entity/RealFavicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Component\Serialization\Json;

/**
* Defines the Favicon entity.
Expand Down Expand Up @@ -54,6 +55,13 @@ class RealFavicon extends ConfigEntityBase implements RealFaviconInterface {
*/
protected $label;

/**
* The manifest of this package.
*
* @var array
*/
protected $manifest = [];

/**
* The folder where Real Favicons exist.
*/
Expand All @@ -71,13 +79,52 @@ public function setTagsAsString($string) {
}

/**
* Set the tags from string.
* Get the tags as string.
*/
public function getTagsAsString() {
$tags = $this->get('tags');
return $tags ? implode(PHP_EOL, $tags) : '';
}

/**
* Get the tags.
*/
public function getTags() {
return $this->get('tags');
}

/**
* Get the manifest.
*/
public function getManifest() {
if (empty($this->manifest)) {
$this->manifest = [];
$path = $this->getDirectory() . '/manifest.json';
if (file_exists($path)) {
$data = file_get_contents($path);
$this->manifest = Json::decode($data);
}
}
return $this->manifest;
}

/**
* Get the largest manifest image.
*/
public function getManifestLargeImage() {
$image = '';
if ($manifest = $this->getManifest()) {
$size = 0;
foreach ($manifest['icons'] as $icon) {
$icon_size = explode('x', $icon['sizes']);
if ($icon_size[0] > $size) {
$image = $this->getDirectory() . $icon['src'];
}
}
}
return $image;
}

/**
* Set the archive as base64 encoded string.
*/
Expand Down

0 comments on commit cc65444

Please sign in to comment.