function set_avatar($params) { $post = Post::find($params['post_id']); if (!$post->can_be_seen_by($this)) { $this->record_errors->add('access', "denied"); return false; } // vde($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->record_errors->add('parameter', "error"); return false; } $tempfile_path = 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 { Danbooru::resize($image_ext, $image_path, $tempfile_path, $size, 95); } catch (Exception $x) { if (file_exists($tempfile_path)) { unlink($tempfile_path); } $this->record_errors->add("avatar", "couldn't be generated (" . $x->getMessage() . ")"); return false; } rename($tempfile_path, $this->avatar_path()); chmod($this->avatar_path(), 0775); $this->update_attributes(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' => gmd())); return true; }
function generate_preview() { if (!$this->is_image() && !$this->width && !$this->height) { return true; } $size = Danbooru::reduce_to(array('width' => $this->width, 'height' => $this->height), array('width' => 300, 'height' => 300)); # Generate the preview from the new sample if we have one to save CPU, otherwise from the image. if (file_exists($this->tempfile_sample_path())) { list($path, $ext) = array($this->tempfile_sample_path(), "jpg"); } elseif (file_exists($this->sample_path())) { list($path, $ext) = array($this->sample_path(), "jpg"); } elseif (file_exists($this->tempfile_path)) { list($path, $ext) = array($this->tempfile_path, $this->file_ext); } elseif (file_exists($this->file_path())) { list($path, $ext) = array($this->file_path(), $this->file_ext); } else { return false; } try { Danbooru::resize($ext, $path, $this->tempfile_preview_path(), $size, 85); } catch (Exception $e) { $this->record_errors->add("preview", "couldn't be generated ({$e->getMessage()})"); return false; } $this->actual_preview_width = $this->raw_preview_dimensions('w'); $this->actual_preview_height = $this->raw_preview_dimensions('h'); $this->preview_width = $this->preview_dimensions('w'); $this->preview_height = $this->preview_dimensions('h'); return true; }