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('error', 'Can not determine the absolute path for that image, skipping.');
100 $res = $this->client
->get($absolutePath);
101 } catch (\Exception
$e) {
102 $this->logger
->log('error', 'Can not retrieve image, skipping.', ['exception' => $e]);
107 $ext = $this->mimeGuesser
->guess($res->getHeader('content-type'));
108 $this->logger
->log('debug', 'Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]);
109 if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
110 $this->logger
->log('error', 'Processed image with not allowed extension. Skipping '.$imagePath);
114 $hashImage = hash('crc32', $absolutePath);
115 $localPath = $folderPath.'/'.$hashImage.'.'.$ext;
118 $im = imagecreatefromstring($res->getBody());
119 } catch (\Exception
$e) {
124 $this->logger
->log('error', 'Error while regenerating image', ['path' => $localPath]);
131 $result = imagegif($im, $localPath);
132 $this->logger
->log('debug', 'Re-creating gif');
136 $result = imagejpeg($im, $localPath, self
::REGENERATE_PICTURES_QUALITY
);
137 $this->logger
->log('debug', 'Re-creating jpg');
140 $result = imagepng($im, $localPath, ceil(self
::REGENERATE_PICTURES_QUALITY
/ 100 * 9));
141 $this->logger
->log('debug', 'Re-creating png');
146 return '/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
150 * Generate the folder where we are going to save images based on the entry url.
156 private function getRelativePath($url)
158 $hashUrl = hash('crc32', $url);
159 $relativePath = $hashUrl[0].'/'.$hashUrl[1].'/'.$hashUrl;
160 $folderPath = $this->baseFolder
.'/'.$relativePath;
162 if (!file_exists($folderPath)) {
163 mkdir($folderPath, 0777, true);
166 $this->logger
->log('debug', 'Folder used for that url', ['folder' => $folderPath, 'url' => $url]);
168 return $relativePath;
172 * Make an $url absolute based on the $base.
174 * @see Graby->makeAbsoluteStr
176 * @param string $base Base url
177 * @param string $url Url to make it absolute
179 * @return false|string
181 private function getAbsoluteLink($base, $url)
183 if (preg_match('!^https?://!i', $url)) {
188 $base = new \
SimplePie_IRI($base);
190 // remove '//' in URL path (causes URLs not to resolve properly)
191 if (isset($base->ipath
)) {
192 $base->ipath
= preg_replace('!//+!', '/', $base->ipath
);
195 if ($absolute = \SimplePie_IRI
::absolutize($base, $url)) {
196 return $absolute->get_uri();
199 $this->logger
->log('error', 'Can not make an absolute link', ['base' => $base, 'url' => $url]);