3 namespace Wallabag\CoreBundle\Helper
;
5 use Psr\Log\LoggerInterface
;
6 use Symfony\Component\DomCrawler\Crawler
;
8 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
12 const REGENERATE_PICTURES_QUALITY
= 80;
19 public function __construct(Client
$client, $baseFolder, LoggerInterface
$logger)
21 $this->client
= $client;
22 $this->baseFolder
= $baseFolder;
23 $this->logger
= $logger;
24 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
30 * Setup base folder where all images are going to be saved.
32 private function setFolder()
34 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
35 if (!file_exists($this->baseFolder
)) {
36 mkdir($this->baseFolder
, 0777, true);
41 * Process the html and extract image from it, save them to local and return the updated html.
44 * @param string $url Used as a base path for relative image and folder
48 public function processHtml($html, $url)
50 $crawler = new Crawler($html);
52 ->filterXpath('//img')
53 ->extract(array('src'));
55 $relativePath = $this->getRelativePath($url);
57 // download and save the image to the folder
58 foreach ($result as $image) {
59 $imagePath = $this->processSingleImage($image, $url, $relativePath);
61 if (false === $imagePath) {
65 $html = str_replace($image, $imagePath, $html);
72 * Process a single image:
74 * - re-saved it (for security reason)
75 * - return the new local path.
77 * @param string $imagePath Path to the image to retrieve
78 * @param string $url Url from where the image were found
79 * @param string $relativePath Relative local path to saved the image
81 * @return string Relative url to access the image from the web
83 public function processSingleImage($imagePath, $url, $relativePath = null)
85 if (null == $relativePath) {
86 $relativePath = $this->getRelativePath($url);
89 $folderPath = $this->baseFolder
.'/'.$relativePath;
92 $absolutePath = $this->getAbsoluteLink($url, $imagePath);
93 if (false === $absolutePath) {
94 $this->logger
->log('debug', 'Can not determine the absolute path for that image, skipping.');
99 $res = $this->client
->get(
101 ['exceptions' => false]
104 $ext = $this->mimeGuesser
->guess($res->getHeader('content-type'));
105 $this->logger
->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
106 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'])) {
107 $this->logger
->log('debug', 'Processed image with not allowed extension. Skipping '.$imagePath);
111 $hashImage = hash('crc32', $absolutePath);
112 $localPath = $folderPath.'/'.$hashImage.'.'.$ext;
115 $im = imagecreatefromstring($res->getBody());
116 } catch (\Exception
$e) {
121 $this->logger
->log('error', 'Error while regenerating image', ['path' => $localPath]);
128 $result = imagegif($im, $localPath);
129 $this->logger
->log('debug', 'Re-creating gif');
133 $result = imagejpeg($im, $localPath, self
::REGENERATE_PICTURES_QUALITY
);
134 $this->logger
->log('debug', 'Re-creating jpg');
137 $result = imagepng($im, $localPath, ceil(self
::REGENERATE_PICTURES_QUALITY
/ 100 * 9));
138 $this->logger
->log('debug', 'Re-creating png');
143 return '/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
147 * Generate the folder where we are going to save images based on the entry url.
153 private function getRelativePath($url)
155 $hashUrl = hash('crc32', $url);
156 $relativePath = $hashUrl[0].'/'.$hashUrl[1].'/'.$hashUrl;
157 $folderPath = $this->baseFolder
.'/'.$relativePath;
159 if (!file_exists($folderPath)) {
160 mkdir($folderPath, 0777, true);
163 $this->logger
->log('debug', 'Folder used for that url', ['folder' => $folderPath, 'url' => $url]);
165 return $relativePath;
169 * Make an $url absolute based on the $base.
171 * @see Graby->makeAbsoluteStr
173 * @param string $base Base url
174 * @param string $url Url to make it absolute
176 * @return false|string
178 private function getAbsoluteLink($base, $url)
180 if (preg_match('!^https?://!i', $url)) {
185 $base = new \
SimplePie_IRI($base);
187 // remove '//' in URL path (causes URLs not to resolve properly)
188 if (isset($base->ipath
)) {
189 $base->ipath
= preg_replace('!//+!', '/', $base->ipath
);
192 if ($absolute = \SimplePie_IRI
::absolutize($base, $url)) {
193 return $absolute->get_uri();