Example #1
0
 /**
  * View image
  * @param Difra\Param\AnyInt $id
  * @throws Difra\View\HttpError
  */
 public function tmpAction(\Difra\Param\AnyInt $id)
 {
     $data = \Difra\Libs\Vault::get($id->val());
     if (!$data) {
         throw new \Difra\View\HttpError(404);
     }
     \Difra\View::$rendered = true;
     header('Content-type: image/png');
     echo $data;
 }
Example #2
0
 /**
  * Save images
  * Saves images found in $html to $path and replaces paths in img src="..." to $urlPrefix/{$id}.png.
  * Warning: if $path contains files not found in $html's as img src="..." links, those files will be deleted.
  * 1. Use $path exclusively for one object.
  * 2. Call saveImages() before saving $html
  * @param $html
  * @param $path
  * @param $urlPrefix
  */
 public static function saveImages(&$html, $path, $urlPrefix)
 {
     // when using AjaxSafeHTML, characters inside src= are encoded using ESAPI
     $html = str_replace('src="http://' . Envi::getHost() . '/up/tmp/', 'src="/up/tmp/', $html);
     $html = str_replace('src="/up/tmp/', 'src="/up/tmp/', $html);
     $html = str_replace('src="http://' . Envi::getHost() . str_replace('/', '/', "{$urlPrefix}/"), 'src="' . $urlPrefix . '/', $html);
     $html = str_replace('src="' . str_replace('/', '/', $urlPrefix . '/'), 'src="' . $urlPrefix . '/', $html);
     preg_match_all('/src=\\"\\/up\\/tmp\\/([0-9]+)\\"/', $html, $newImages);
     preg_match_all('/src=\\"' . preg_quote($urlPrefix, '/') . '\\/([0-9]+)\\.png\\"/', $html, $oldImages);
     if (!empty($oldImages[1])) {
         $usedImages = $oldImages[1];
     } else {
         $usedImages = [];
     }
     if (!empty($newImages[1])) {
         @mkdir($path, 0777, true);
         $urlPrefix = trim($urlPrefix, '/');
         foreach ($newImages[1] as $v) {
             $img = Vault::get($v);
             file_put_contents("{$path}/{$v}.png", $img);
             $html = str_replace("src=\"/up/tmp/{$v}\"", "src=\"/{$urlPrefix}/{$v}.png\"", $html);
             Vault::delete($v);
             $usedImages[] = $v;
         }
     }
     if (is_dir($path)) {
         $dir = opendir($path);
         while (false !== ($file = readdir($dir))) {
             if ($file[0] == '.') {
                 continue;
             }
             if (substr($file, -4) != '.png' or !in_array(substr($file, 0, strlen($file) - 4), $usedImages)) {
                 @unlink("{$path}/{$file}");
             }
         }
     }
 }
Example #3
0
 /**
  * Save images
  * @param string $path
  * @param string $urlPrefix
  */
 public function saveImages($path, $urlPrefix)
 {
     /** @noinspection PhpUndefinedFieldInspection */
     Vault::saveImages($this->value, $path, $urlPrefix);
     $this->savedImages = true;
 }