3 namespace Wallabag\CoreBundle\Helper
;
5 use Psr\Log\LoggerInterface
as Logger
;
6 use Symfony\Component\DomCrawler\Crawler
;
8 define('REGENERATE_PICTURES_QUALITY', 75);
10 class DownloadImages
{
17 public function __construct($html, $url, Logger
$logger) {
21 $this->logger
= $logger;
24 public function setFolder($folder = "assets/images") {
25 // if folder doesn't exist, attempt to create one and store the folder name in property $folder
26 if(!file_exists($folder)) {
29 $this->folder
= $folder;
32 public function process() {
33 //instantiate the symfony DomCrawler Component
34 $crawler = new Crawler($this->html
);
35 // create an array of all scrapped image links
36 $this->logger
->log('debug', 'Finding images inside document');
38 ->filterXpath('//img')
39 ->extract(array('src'));
41 // download and save the image to the folder
42 foreach ($result as $image) {
43 $file = file_get_contents($image);
46 $absolute_path = self
::getAbsoluteLink($image, $this->url
);
47 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
48 $fullpath = $this->folder
."/".$filename;
49 self
::checks($file, $fullpath, $absolute_path);
50 $this->html
= str_replace($image, $fullpath, $this->html
);
56 private function checks($rawdata, $fullpath, $absolute_path) {
57 $fullpath = urldecode($fullpath);
59 if (file_exists($fullpath)) {
64 $this->logger
->log('debug','Checking extension');
66 $file_ext = strrchr($fullpath, '.');
67 $whitelist = array('.jpg', '.jpeg', '.gif', '.png');
68 if (!(in_array($file_ext, $whitelist))) {
69 $this->logger
->log('debug','processed image with not allowed extension. Skipping '.$fullpath);
75 $this->logger
->log('debug','Checking headers');
76 $imageinfo = getimagesize($absolute_path);
77 if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') {
78 $this->logger
->log('debug','processed image with bad header. Skipping '.$fullpath);
84 $this->logger
->log('debug','regenerating image');
85 $im = imagecreatefromstring($rawdata);
87 $this->logger
->log('error','error while regenerating image '.$fullpath);
92 switch ($imageinfo['mime']) {
94 $result = imagegif($im, $fullpath);
95 $this->logger
->log('debug','Re-creating gif');
99 $result = imagejpeg($im, $fullpath, REGENERATE_PICTURES_QUALITY
);
100 $this->logger
->log('debug','Re-creating jpg');
103 $this->logger
->log('debug','Re-creating png');
104 $result = imagepng($im, $fullpath, ceil(REGENERATE_PICTURES_QUALITY
/ 100 * 9));
112 private static function getAbsoluteLink($relativeLink, $url)
114 /* return if already absolute URL */
115 if (parse_url($relativeLink, PHP_URL_SCHEME
) != '') {
116 return $relativeLink;
119 /* queries and anchors */
120 if ($relativeLink[0] == '#' || $relativeLink[0] == '?') {
121 return $url.$relativeLink;
124 /* parse base URL and convert to local variables:
125 $scheme, $host, $path */
126 extract(parse_url($url));
128 /* remove non-directory element from path */
129 $path = preg_replace('#/[^/]*$#', '', $path);
131 /* destroy path if relative url points to root */
132 if ($relativeLink[0] == '/') {
136 /* dirty absolute URL */
137 $abs = $host.$path.'/'.$relativeLink;
139 /* replace '//' or '/./' or '/foo/../' with '/' */
140 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
141 for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
144 /* absolute URL is ready! */
145 return $scheme.'://'.$abs;