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 delete_file() { # If several inlines use the same image, they'll share the same file via the MD5. Only # delete the file if this is the last one using it. $exists = InlineImage::where('id <> ? AND md5 = ?', $this->id, $this->md5)->first(); if ($exists) { return; } if (is_file($this->file_path())) { unlink($this->file_path()); } if (is_file($this->preview_path())) { unlink($this->preview_path()); } if (is_file($this->sample_path())) { unlink($this->sample_path()); } }
public function copy() { $inline = Inline::find($this->params()->id); $new_inline = Inline::create(['user_id' => current_user()->id, 'description' => $inline->description]); foreach ($inline->inline_images as $image) { $new_attributes = array_merge($image->attributes(), ['inline_id' => $new_inline->id]); unset($new_attributes['id']); $new_image = InlineImage::create($new_attributes); } $this->respond_to_success('Image copied', ['#edit', 'id' => $new_inline->id]); }