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(); } }
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; }
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; }
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; }
public function set_avatar($params) { $post = Post::find($params['id']); if (!$post->can_be_seen_by($this)) { $this->errors()->add('access', "denied"); return false; } 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; } $tempfile_path = Rails::root() . "/public/data/" . $this->id . ".avatar.jpg"; $use_sample = $post->has_sample(); if ($use_sample) { $image_path = $post->sample_path(); $image_ext = "jpg"; $size = $this->_reduce_and_crop($post->sample_width, $post->sample_height, $params); # If we're cropping from a very small region in the sample, use the full # image instead, to get a higher quality image. if ($size['crop_bottom'] - $size['crop_top'] < CONFIG()->avatar_max_height or $size['crop_right'] - $size['crop_left'] < CONFIG()->avatar_max_width) { $use_sample = false; } } if (!$use_sample) { $image_path = $post->file_path(); $image_ext = $post->file_ext; $size = $this->_reduce_and_crop($post->width, $post->height, $params); } try { Moebooru\Resizer::resize($image_ext, $image_path, $tempfile_path, $size, 95); } catch (Moebooru\Exception\ResizeErrorException $x) { if (file_exists($tempfile_path)) { unlink($tempfile_path); } $this->errors()->add("avatar", "couldn't be generated (" . $x->getMessage() . ")"); return false; } rename($tempfile_path, $this->avatar_path()); chmod($this->avatar_path(), 0775); $this->updateAttributes(array('avatar_post_id' => $params['post_id'], 'avatar_top' => $params['top'], 'avatar_bottom' => $params['bottom'], 'avatar_left' => $params['left'], 'avatar_right' => $params['right'], 'avatar_width' => $size['width'], 'avatar_height' => $size['height'], 'avatar_timestamp' => date('Y-m-d H:i:s'))); return true; }