+ @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.
+ *
+ * @param int $entryId ID of the entry
+ *
+ * @return string
+ */
+ private function getRelativePath($entryId)
+ {
+ $hashId = hash('crc32', $entryId);
+ $relativePath = $hashId[0] . '/' . $hashId[1] . '/' . $hashId;
+ $folderPath = $this->baseFolder . '/' . $relativePath;
+
+ if (!file_exists($folderPath)) {
+ mkdir($folderPath, 0777, true);