namespace Wallabag\CoreBundle\Helper;
-use Psr\Log\LoggerInterface;
-use Symfony\Component\DomCrawler\Crawler;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
-use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
+use Psr\Log\LoggerInterface;
+use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Finder\Finder;
+use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
class DownloadImages
{
$this->setFolder();
}
- /**
- * Setup base folder where all images are going to be saved.
- */
- private function setFolder()
- {
- // if folder doesn't exist, attempt to create one and store the folder name in property $folder
- if (!file_exists($this->baseFolder)) {
- mkdir($this->baseFolder, 0755, true);
- }
- }
-
/**
* Process the html and extract image from it, save them to local and return the updated html.
*
public function processHtml($entryId, $html, $url)
{
$crawler = new Crawler($html);
- $result = $crawler
- ->filterXpath('//img')
+ $imagesCrawler = $crawler
+ ->filterXpath('//img');
+ $imagesUrls = $imagesCrawler
->extract(['src']);
+ $imagesSrcsetUrls = $this->getSrcsetUrls($imagesCrawler);
+ $imagesUrls = array_unique(array_merge($imagesUrls, $imagesSrcsetUrls));
$relativePath = $this->getRelativePath($entryId);
// download and save the image to the folder
- foreach ($result as $image) {
+ foreach ($imagesUrls as $image) {
$imagePath = $this->processSingleImage($entryId, $image, $url, $relativePath);
if (false === $imagePath) {
*/
public function processSingleImage($entryId, $imagePath, $url, $relativePath = null)
{
+ if (null === $imagePath) {
+ return false;
+ }
+
if (null === $relativePath) {
$relativePath = $this->getRelativePath($entryId);
}
- $this->logger->debug('DownloadImages: working on image: '.$imagePath);
+ $this->logger->debug('DownloadImages: working on image: ' . $imagePath);
- $folderPath = $this->baseFolder.'/'.$relativePath;
+ $folderPath = $this->baseFolder . '/' . $relativePath;
// build image path
$absolutePath = $this->getAbsoluteLink($url, $imagePath);
}
$hashImage = hash('crc32', $absolutePath);
- $localPath = $folderPath.'/'.$hashImage.'.'.$ext;
+ $localPath = $folderPath . '/' . $hashImage . '.' . $ext;
try {
$im = imagecreatefromstring($res->getBody());
imagedestroy($im);
- return $this->wallabagUrl.'/assets/images/'.$relativePath.'/'.$hashImage.'.'.$ext;
+ return $this->wallabagUrl . '/assets/images/' . $relativePath . '/' . $hashImage . '.' . $ext;
}
/**
public function removeImages($entryId)
{
$relativePath = $this->getRelativePath($entryId);
- $folderPath = $this->baseFolder.'/'.$relativePath;
+ $folderPath = $this->baseFolder . '/' . $relativePath;
$finder = new Finder();
$finder
@rmdir($folderPath);
}
+ /**
+ * Get images urls from the srcset image attribute.
+ *
+ * @param Crawler $imagesCrawler
+ *
+ * @return array An array of urls
+ */
+ protected function getSrcsetUrls(Crawler $imagesCrawler)
+ {
+ $urls = [];
+ $iterator = $imagesCrawler
+ ->getIterator();
+ while ($iterator->valid()) {
+ $srcsetAttribute = $iterator->current()->getAttribute('srcset');
+ if ('' !== $srcsetAttribute) {
+ $srcset = array_map('trim', explode(',', $srcsetAttribute));
+ $srcsetUrls = array_map(function ($src) {
+ return explode(' ', $src)[0];
+ }, $srcset);
+ $urls = array_merge($srcsetUrls, $urls);
+ }
+ $iterator->next();
+ }
+
+ return $urls;
+ }
+
+ /**
+ * Setup base folder where all images are going to be saved.
+ */
+ private function setFolder()
+ {
+ // if folder doesn't exist, attempt to create one and store the folder name in property $folder
+ if (!file_exists($this->baseFolder)) {
+ mkdir($this->baseFolder, 0755, true);
+ }
+ }
+
/**
* Generate the folder where we are going to save images based on the entry url.
*
private function getRelativePath($entryId)
{
$hashId = hash('crc32', $entryId);
- $relativePath = $hashId[0].'/'.$hashId[1].'/'.$hashId;
- $folderPath = $this->baseFolder.'/'.$relativePath;
+ $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
+ $folderPath = $this->baseFolder . '/' . $relativePath;
if (!file_exists($folderPath)) {
mkdir($folderPath, 0777, true);
}
if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) {
- $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: '.$imagePath);
+ $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: ' . $imagePath);
return false;
}