Ejemplo n.º 1
0
 public function preview_dimensions()
 {
     $dim = Moebooru\Resizer::reduce_to(['width' => $this->width, 'height' => $this->height], ['width' => 150, 'height' => 150]);
     return [$dim['width'], $dim['height']];
 }
Ejemplo n.º 2
0
 public function generate_sample()
 {
     if (is_file($this->sample_path())) {
         return true;
     }
     /**
      * We can generate the sample image during upload or offline.  Use tempfile_image_path
      * if it exists, otherwise use file_path.
      */
     $path = $this->tempfile_image_path();
     if (!is_file($path)) {
         $path = $this->file_path();
     }
     if (!is_file($path)) {
         $this->errors()->add('file', 'not found');
         return false;
     }
     # If we're not reducing the resolution for the sample image, only reencode if the
     # source image is above the reencode threshold.  Anything smaller won't be reduced
     # enough by the reencode to bother, so don't reencode it and save disk space.
     $sample_size = Moebooru\Resizer::reduce_to(['width' => $this->width, 'height' => $this->height], ['width' => CONFIG()->inline_sample_width, 'height' => CONFIG()->inline_sample_height]);
     if ($sample_size['width'] == $this->width && $sample_size['height'] == $this->height && filesize($path) < CONFIG()->sample_always_generate_size) {
         return true;
     }
     # If we already have a sample image, and the parameters havn't changed,
     # don't regenerate it.
     if ($sample_size['width'] == $this->sample_width && $sample_size['height'] == $this->sample_height) {
         return true;
     }
     try {
         Moebooru\Resizer::resize($this->file_ext, $path, $this->tempfile_sample_path(), $sample_size, 95);
     } catch (Exception $e) {
         $this->errors()->add('sample', "couldn't be created:" . $e->getMessage());
         return false;
     }
     $this->sample_width = $sample_size['width'];
     $this->sample_height = $sample_size['height'];
     return true;
 }
Ejemplo n.º 3
0
 protected function generate_jpeg($force_regen = false)
 {
     if ($this->gif() || !$this->image()) {
         return true;
     } elseif (!CONFIG()->jpeg_enable) {
         return true;
     } elseif (!$this->width && !$this->height) {
         return true;
     }
     # Only generate JPEGs for PNGs.    Don't do it for files that are already JPEGs; we'll just add
     # artifacts and/or make the file bigger.    Don't do it for GIFs; they're usually animated.
     if ($this->file_ext != "png") {
         return true;
     }
     # We can generate the image during upload or offline.    Use tempfile_path
     #- if it exists, otherwise use file_path.
     $path = $this->tempfile_path();
     // path = file_path unless File.exists?(path)
     // unless File.exists?(path)
     // record_errors.add(:file, "not found")
     // return false
     // end
     # If we already have the image, don't regenerate it.
     if (!$force_regen && ctype_digit((string) $this->jpeg_width)) {
         return true;
     }
     $size = Moebooru\Resizer::reduce_to(array('width' => $this->width, 'height' => $this->height), array('width' => CONFIG()->jpeg_width, 'height' => CONFIG()->jpeg_height), CONFIG()->jpeg_ratio);
     try {
         Moebooru\Resizer::resize($this->file_ext, $path, $this->tempfile_jpeg_path(), $size, CONFIG()->jpeg_quality['max']);
     } catch (Moebooru\Exception\ResizeErrorException $e) {
         $this->errors()->add("jpeg", "couldn't be created: {$e->getMessage()}");
         return false;
     }
     $this->jpeg_width = $size['width'];
     $this->jpeg_height = $size['height'];
     $this->jpeg_size = filesize($this->tempfile_jpeg_path());
     # iTODO: enable crc32 for jpg.
     $crc32_accum = 0;
     return true;
 }
Ejemplo n.º 4
0
 public static function save_search($file_contents)
 {
     $tempfile_path_resize = $tempfile_path = $file_path = null;
     try {
         if (!is_dir(self::search_cache_dir())) {
             mkdir(self::search_cache_dir());
         }
         while (true) {
             $tempfile_path = self::search_cache_dir() . "/" . uniqid('', true) . ".upload";
             if (!is_file($tempfile_path)) {
                 break;
             }
         }
         $fh = fopen($tempfile_path, 'a');
         fclose($fh);
         file_put_contents($tempfile_path, $file_contents);
         # Use the resizer to validate the file and convert it to a thumbnail-size JPEG.
         $imgsize = getimagesize($tempfile_path);
         $exts = [false, 'gif', 'jpg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', 'jb2', 'swc', 'iff', 'wbmp', 'xbm'];
         if (!$imgsize || !$imgsize[2] || !isset($exts[$imgsize[2]])) {
             throw new Moebooru\Exception\ResizeErrorException("Unrecognized image format");
         }
         $ret = [];
         $ret['original_width'] = $imgsize[0];
         $ret['original_height'] = $imgsize[1];
         $size = Moebooru\Resizer::reduce_to(['width' => $ret['original_width'], 'height' => $ret['original_height']], ['width' => 150, 'height' => 150]);
         $ext = $exts[$imgsize[2]];
         $tempfile_path_resize = $tempfile_path . ".2";
         Moebooru\Resizer::resize($ext, $tempfile_path, $tempfile_path_resize, $size, 95);
         rename($tempfile_path_resize, $tempfile_path);
         $md5 = md5_file($tempfile_path);
         $id = $md5 . "." . $ext;
         $file_path = self::search_cache_dir() . "/" . $id;
         rename($tempfile_path, $file_path);
         # Finally block
         if (is_dir($tempfile_path)) {
             rmdir($tempfile_path);
         }
         if (is_file($tempfile_path_resize)) {
             rmdir($tempfile_path_resize);
         }
         // chmod($file_path, 0664);
     } catch (Exception $e) {
         # Finally block
         if (is_dir($tempfile_path)) {
             rmdir($tempfile_path);
         }
         if (is_file($tempfile_path_resize)) {
             rmdir($tempfile_path_resize);
         }
         if (is_dir($file_path)) {
             rmdir($file_path);
         }
         throw $e;
     }
     /*
     TODO:
     finally {
         if (is_dir($tempfile_path))
             rmdir($tempfile_path);
         if (is_file($tempfile_path_resize))
             rmdir($tempfile_path_resize);
     }
     */
     $ret['file_path'] = $file_path;
     $ret['search_id'] = $id;
     return $ret;
 }
Ejemplo n.º 5
0
 private function _reduce_and_crop($image_width, $image_height, $params)
 {
     $cropped_image_width = $image_width * ($params['right'] - $params['left']);
     $cropped_image_height = $image_height * ($params['bottom'] - $params['top']);
     $size = Moebooru\Resizer::reduce_to(['width' => $cropped_image_width, 'height' => $cropped_image_height], ['width' => CONFIG()->avatar_max_width, 'height' => CONFIG()->avatar_max_height], 1, true);
     $size['crop_top'] = $image_height * $params['top'];
     $size['crop_bottom'] = $image_height * $params['bottom'];
     $size['crop_left'] = $image_width * $params['left'];
     $size['crop_right'] = $image_width * $params['right'];
     return $size;
 }