Soshot/app/Soshot/GetFav.php

104 lines
3.8 KiB
PHP

<?php
namespace App\Soshot;
use App\Soshot\GetThumb;
use App\Utils\ConvertIco;
use Embed\Embed;
use Zebra_Image;
class GetFav extends GetThumb {
/**
* Creates a favicon for a given URL.
*
* This method takes the URL provided in the `demande` object and uses the Embed
* library to extract the favicon or icon from the website. If the favicon or icon
* cannot be extracted, a default error image is used. The favicon is then resized
* to the specified size and saved to the specified file path. If the favicon is in
* ICO or SVG format, it is converted to PNG format using the ConvertIco class. The
* method updates the database with the status of the favicon creation.
*
* @return bool True if the favicon was successfully created, false otherwise.
*/
public function makeFavicon() {
if (!is_dir($this->filePath)) {
mkdir($this->demande->filePath);
}
try {
$embed = new Embed();
$info = $embed->get($this->demande->url);
if (!is_null($info->favicon)) {
if (!$favicon = @file_get_contents($info->favicon)) {
copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
$this->db->addUpdate(2, $this->type);
return true;
}
} elseif (!is_null($info->icon)) {
if (!$favicon = @file_get_contents($info->icon)) {
copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
$this->db->addUpdate(2, $this->type);
return true;
}
} else {
copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
$this->db->addUpdate(2, $this->type);
return true;
}
file_put_contents($this->demande->requestImg, $favicon);
$realMimeType = mime_content_type($this->demande->requestImg);
if ($realMimeType === 'image/vnd.microsoft.icon') {
ConvertIco::convert($this->demande, $this->conf->icoSize);
}
if ($realMimeType === 'image/svg+xml') {
ConvertIco::convert($this->demande, $this->conf->icoSize);
}
$image = new Zebra_Image();
$image->source_path = $this->demande->requestImg;
$image->target_path = $this->demande->requestImg;
$image->preserve_time = false;
$image->enlarge_smaller_images = false;
$image->resize($this->conf->icoSize, $this->conf->icoSize, ZEBRA_IMAGE_CROP_TOPLEFT);
} catch (\Exception $e) {
$this->db->addUpdate(2, $this->type);
copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
return true;
}
if (!is_file($this->demande->requestImg)) {
copy(__DIR__ . '/../../src/images/error_fav.png', $this->demande->requestImg);
$this->db->addUpdate(2, $this->type);
return true;
} else {
$this->db->addUpdate(1, $this->type);
}
}
/**
* Displays the requested image.
*
* This method checks if the requested image file exists. If the requested image is
* a favicon and does not exist, it calls the makeFavicon method to generate it.
* Then, it sets the appropriate headers for the image and outputs its contents.
*
* @return void
*/
public function show() {
if (!file_exists($this->requestImg) && $this->type === 'fav') {
$this->makeFavicon();
}
header("Content-type: image/$this->fileFormat");
header('Expires: ', gmdate('D, d M Y H:i:s', time()) . ' GMT');
echo file_get_contents($this->requestImg);
exit();
}
}