diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/DownloadImages.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/DownloadImages.php | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php index cc3dcfce..c1645e45 100644 --- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php +++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php | |||
@@ -31,23 +31,36 @@ class DownloadImages | |||
31 | } | 31 | } |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * Process the html and extract image from it, save them to local and return the updated html. | 34 | * Process the html and extract images URLs from it. |
35 | * | 35 | * |
36 | * @param int $entryId ID of the entry | ||
37 | * @param string $html | 36 | * @param string $html |
38 | * @param string $url Used as a base path for relative image and folder | ||
39 | * | 37 | * |
40 | * @return string | 38 | * @return string[] |
41 | */ | 39 | */ |
42 | public function processHtml($entryId, $html, $url) | 40 | public static function extractImagesUrlsFromHtml($html) |
43 | { | 41 | { |
44 | $crawler = new Crawler($html); | 42 | $crawler = new Crawler($html); |
45 | $imagesCrawler = $crawler | 43 | $imagesCrawler = $crawler |
46 | ->filterXpath('//img'); | 44 | ->filterXpath('//img'); |
47 | $imagesUrls = $imagesCrawler | 45 | $imagesUrls = $imagesCrawler |
48 | ->extract(['src']); | 46 | ->extract(['src']); |
49 | $imagesSrcsetUrls = $this->getSrcsetUrls($imagesCrawler); | 47 | $imagesSrcsetUrls = self::getSrcsetUrls($imagesCrawler); |
50 | $imagesUrls = array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); | 48 | |
49 | return array_unique(array_merge($imagesUrls, $imagesSrcsetUrls)); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Process the html and extract image from it, save them to local and return the updated html. | ||
54 | * | ||
55 | * @param int $entryId ID of the entry | ||
56 | * @param string $html | ||
57 | * @param string $url Used as a base path for relative image and folder | ||
58 | * | ||
59 | * @return string | ||
60 | */ | ||
61 | public function processHtml($entryId, $html, $url) | ||
62 | { | ||
63 | $imagesUrls = self::extractImagesUrlsFromHtml($html); | ||
51 | 64 | ||
52 | $relativePath = $this->getRelativePath($entryId); | 65 | $relativePath = $this->getRelativePath($entryId); |
53 | 66 | ||
@@ -135,7 +148,21 @@ class DownloadImages | |||
135 | 148 | ||
136 | switch ($ext) { | 149 | switch ($ext) { |
137 | case 'gif': | 150 | case 'gif': |
138 | imagegif($im, $localPath); | 151 | // use Imagick if available to keep GIF animation |
152 | if (class_exists('\\Imagick')) { | ||
153 | try { | ||
154 | $imagick = new \Imagick(); | ||
155 | $imagick->readImageBlob($res->getBody()); | ||
156 | $imagick->setImageFormat('gif'); | ||
157 | $imagick->writeImages($localPath, true); | ||
158 | } catch (\Exception $e) { | ||
159 | // if Imagick fail, fallback to the default solution | ||
160 | imagegif($im, $localPath); | ||
161 | } | ||
162 | } else { | ||
163 | imagegif($im, $localPath); | ||
164 | } | ||
165 | |||
139 | $this->logger->debug('DownloadImages: Re-creating gif'); | 166 | $this->logger->debug('DownloadImages: Re-creating gif'); |
140 | break; | 167 | break; |
141 | case 'jpeg': | 168 | case 'jpeg': |
@@ -185,7 +212,7 @@ class DownloadImages | |||
185 | * | 212 | * |
186 | * @return array An array of urls | 213 | * @return array An array of urls |
187 | */ | 214 | */ |
188 | private function getSrcsetUrls(Crawler $imagesCrawler) | 215 | private static function getSrcsetUrls(Crawler $imagesCrawler) |
189 | { | 216 | { |
190 | $urls = []; | 217 | $urls = []; |
191 | $iterator = $imagesCrawler | 218 | $iterator = $imagesCrawler |