/** * Copy this Pageimage and any of it's variations to another path * * @param string $path * @return bool True if successful * */ public function copyToPath($path) { if (parent::copyToPath($path)) { foreach ($this->getVariations() as $variation) { if (is_file($variation->filename)) { copy($variation->filename, $path . $variation->basename); if ($this->config->chmodFile) { chmod($path . $variation->basename, octdec($this->config->chmodFile)); } } } return true; } return false; }
/** * 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})"); } } }
/** * Install this Pagefile * * Implies copying the file to the correct location (if not already there), and populating it's name * * @param string $filename Full path and filename of file to install * @throws WireException * */ protected function ___install($filename) { parent::___install($filename); if (!$this->width()) { parent::unlink(); throw new WireException($this->_('Unable to install invalid image')); } }