예제 #1
0
파일: Inline.php 프로젝트: JCQS04/myimouto
 public function crop(array $params = [])
 {
     # MI: set default params
     $params = array_merge(['top' => 0, 'bottom' => 0, 'left' => 0, 'right' => 0], $params);
     if ($params['top'] < 0 or $params['top'] > 1 or $params['bottom'] < 0 or $params['bottom'] > 1 or $params['left'] < 0 or $params['left'] > 1 or $params['right'] < 0 or $params['right'] > 1 or $params['top'] >= $params['bottom'] or $params['left'] >= $params['right']) {
         $this->errors()->add('parameter', 'error');
         return false;
     }
     $images = $this->inline_images;
     foreach ($images as $image) {
         # Create a new image with the same properties, crop this image into the new one,
         # and delete the old one.
         $new_image = new InlineImage(['description' => $image->description, 'sequence' => $image->sequence, 'inline_id' => $this->id, 'file_ext' => 'jpg']);
         $size = $this->reduce_and_crop($image->width, $image->height, $params);
         try {
             # Create one crop for the image, and InlineImage will create the sample and preview from that.
             Moebooru\Resizer::resize($image->file_ext, $image->file_path(), $new_image->tempfile_image_path(), $size, 95);
             chmod($new_image->tempfile_image_path(), 0775);
         } catch (Exception $e) {
             if (is_file($new_image->tempfile_image_path())) {
                 unlink($new_image->tempfile_image_path());
             }
             $this->errors()->add('crop', "couldn't be genrated (" . $e->getMessage() . ")");
             return false;
         }
         $new_image->got_file();
         $new_image->save();
         $image->destroy();
     }
 }
예제 #2
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']];
 }
예제 #3
0
 public function generate_preview()
 {
     if (is_file($this->preview_path())) {
         return true;
     }
     if (!is_file($this->tempfile_image_path())) {
         $this->errors()->add('file', 'not found');
         return false;
     }
     # Generate the preview from the new sample if we have one to save CPU, otherwise from the image.
     if (is_file($this->tempfile_sample_path())) {
         $path = $this->tempfile_sample_path();
         $ext = 'jpg';
     } else {
         $path = $this->tempfile_image_path();
         $ext = $this->file_ext;
     }
     try {
         Moebooru\Resizer::resize($ext, $path, $this->tempfile_preview_path(), $this->preview_dimensions(), 95);
     } catch (Exception $e) {
         $this->errors()->add('preview', "couldn't be generated: " . $e->getMessage());
         return false;
     }
     return true;
 }
예제 #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;
 }
예제 #5
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;
 }
예제 #6
0
파일: User.php 프로젝트: JCQS04/myimouto
 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;
 }