Ejemplo n.º 1
0
 /**
  * Attempt to re-create images that don't exist, when possible
  *
  * @param Pagefile $pagefile
  * @param $img
  * @param $src
  * @param $value
  *
  */
 protected function checkImgExists(Pagefile $pagefile, $img, $src, &$value)
 {
     $basename = basename($src);
     $pathname = $pagefile->pagefiles->path() . $basename;
     if (file_exists($pathname)) {
         return;
     }
     // no action necessary
     // file referenced in <img> tag does not exist, and it is not a variation we can re-create
     if ($pagefile->basename == $basename) {
         // original file no longer exists
         $this->error("Original image file on {$this->page->path} no longer exists, unable to create new variation ({$basename})");
         if ($this->page->of()) {
             $value = str_replace($img, '', $value);
         }
         // remove reference to image, when output formatting is on
         return;
     }
     // check if this is a variation that we might be able to re-create
     $info = $pagefile->isVariation($basename);
     if (!$info) {
         // file is not a variation, so we apparently have no source to pull info from
         $this->error("Unrecognized image that does not exist ({$basename})");
         if ($this->page->of()) {
             $value = str_replace($img, '', $value);
         }
         // remove reference to image, when output formatting is on
         return;
     }
     $info['targetName'] = $basename;
     $variations = array($info);
     while (!empty($info['parent'])) {
         $variations[] = $info['parent'];
         $info = $info['parent'];
     }
     foreach (array_reverse($variations) as $info) {
         // definitely a variation, attempt to re-create it
         $options = array();
         if ($info['crop']) {
             $options['cropping'] = $info['crop'];
         }
         if ($info['suffix']) {
             $options['suffix'] = $info['suffix'];
             if (in_array('hidpi', $options['suffix'])) {
                 $options['hidpi'] = true;
             }
         }
         $newPagefile = $pagefile->size($info['width'], $info['height'], $options);
         // $this->wire('log')->message("size($info[width], $info[height], " . print_r($options, true) . ")");
         if ($newPagefile && is_file($newPagefile->filename())) {
             if (!empty($info['targetName']) && $newPagefile->basename != $info['targetName']) {
                 // new name differs from what is in text. Rename file to be consistent with text.
                 rename($newPagefile->filename(), $pathname);
             }
             $this->wire('log')->message("Re-created image variation: {$newPagefile->name}");
             $pagefile = $newPagefile;
             // for next iteration
         } else {
             $this->wire('log')->error("Unable to re-create image variation ({$newPagefile->name})");
         }
     }
 }