aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php20
-rw-r--r--src/Wallabag/CoreBundle/Helper/DownloadImages.php45
-rw-r--r--src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php2
-rw-r--r--src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php1
4 files changed, 54 insertions, 14 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index bc257ffb..ca01dec8 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -12,8 +12,8 @@ use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Tools\Utils; 12use Wallabag\CoreBundle\Tools\Utils;
13 13
14/** 14/**
15 * This kind of proxy class take care of getting the content from an url 15 * This kind of proxy class takes care of getting the content from an url
16 * and update the entry with what it found. 16 * and updates the entry with what it found.
17 */ 17 */
18class ContentProxy 18class ContentProxy
19{ 19{
@@ -289,13 +289,25 @@ class ContentProxy
289 $this->updateLanguage($entry, $content['language']); 289 $this->updateLanguage($entry, $content['language']);
290 } 290 }
291 291
292 $previewPictureUrl = '';
292 if (!empty($content['open_graph']['og_image'])) { 293 if (!empty($content['open_graph']['og_image'])) {
293 $this->updatePreviewPicture($entry, $content['open_graph']['og_image']); 294 $previewPictureUrl = $content['open_graph']['og_image'];
294 } 295 }
295 296
296 // if content is an image, define it as a preview too 297 // if content is an image, define it as a preview too
297 if (!empty($content['content_type']) && \in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { 298 if (!empty($content['content_type']) && \in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
298 $this->updatePreviewPicture($entry, $content['url']); 299 $previewPictureUrl = $content['url'];
300 } elseif (empty($previewPictureUrl)) {
301 $this->logger->debug('Extracting images from content to provide a default preview picture');
302 $imagesUrls = DownloadImages::extractImagesUrlsFromHtml($content['html']);
303 $this->logger->debug(\count($imagesUrls) . ' pictures found');
304 if (!empty($imagesUrls)) {
305 $previewPictureUrl = $imagesUrls[0];
306 }
307 }
308
309 if (!empty($previewPictureUrl)) {
310 $this->updatePreviewPicture($entry, $previewPictureUrl);
299 } 311 }
300 312
301 if (!empty($content['content_type'])) { 313 if (!empty($content['content_type'])) {
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
diff --git a/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php
index 183d394a..04abc6d0 100644
--- a/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php
+++ b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php
@@ -21,7 +21,7 @@ class PreparePagerForEntries
21 21
22 /** 22 /**
23 * @param AdapterInterface $adapter 23 * @param AdapterInterface $adapter
24 * @param User $user If user isn't logged in, we can force it (like for rss) 24 * @param User $user If user isn't logged in, we can force it (like for feed)
25 * 25 *
26 * @return Pagerfanta|null 26 * @return Pagerfanta|null
27 */ 27 */
diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
index 63f65067..fbdf2ac7 100644
--- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
+++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
@@ -6,6 +6,7 @@ use Psr\Log\LoggerInterface;
6use RulerZ\RulerZ; 6use RulerZ\RulerZ;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag; 8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Entity\TaggingRule;
9use Wallabag\CoreBundle\Repository\EntryRepository; 10use Wallabag\CoreBundle\Repository\EntryRepository;
10use Wallabag\CoreBundle\Repository\TagRepository; 11use Wallabag\CoreBundle\Repository\TagRepository;
11use Wallabag\UserBundle\Entity\User; 12use Wallabag\UserBundle\Entity\User;