[ImageThumbnail] Finish image thumbnailing functionality

This commit is contained in:
2021-04-16 15:57:25 +00:00
committed by Diogo Peralta Cordeiro
parent f6dea6e162
commit c8915df31e
7 changed files with 63 additions and 36 deletions

View File

@@ -25,6 +25,7 @@ use App\Core\DB\DB;
use App\Core\Entity;
use App\Core\Event;
use App\Core\Log;
use App\Util\Common;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException;
use Component\Media\Media;
@@ -98,17 +99,34 @@ class AttachmentThumbnail extends Entity
// }}} Autocode
private Attachment $attachment;
public function setAttachment(Attachment $attachment)
{
$this->attachment = $attachment;
}
public function getAttachment()
{
if (isset($this->attachment)) {
return $this->attachment;
} else {
return $this->attachment = DB::findOneBy('attachment', ['id' => $this->attachment_id]);
}
}
public static function getOrCreate(Attachment $attachment, ?int $width = null, ?int $height = null, ?bool $crop = null)
{
try {
return DB::findOneBy('attachment_thumbnail', ['attachment_id' => $attachment->getId(), 'width' => $width, 'height' => $height]);
} catch (NotFoundException $e) {
$outpath = $attachment->getFileHash() . '-' . $width . 'x' . $height;
$thumbnail = self::create(['attachment_id' => $attachment->getId(), 'width' => $width, 'height' => $height, 'attachment' => $attachment]);
$event_map = ['image' => 'ResizeImage', 'video' => 'ResizeVideo'];
$major_mime = Media::mimetypeMajor($attachment->getMimetype());
if (in_array($major_mime, array_keys($event_map))) {
Event::handle($event_map[$major_mime], [$attachment, $outpath, $width, $height, $crop]);
Event::handle($event_map[$major_mime], [$attachment, $thumbnail, $width, $height, $crop]);
return $thumbnail;
} else {
Log::debug($m = ('Cannot resize attachment with mimetype ' . $attachment->getMimetype()));
throw new ServerException($m);
@@ -116,6 +134,16 @@ class AttachmentThumbnail extends Entity
}
}
public function getFilename()
{
return $this->getAttachment()->getFileHash() . "-{$this->width}x{$this->height}.webp";
}
public function getPath()
{
return Common::config('thumbnail', 'dir') . $this->getFilename();
}
/**
* Delete a attachment thumbnail. This table doesn't own all the attachments, only itself
*/