[ENTITY][AttachmentThumbnail] Every image should have width and height attributes

This commit is contained in:
Diogo Peralta Cordeiro 2021-09-25 13:12:32 +01:00
parent 808da203ad
commit a681acae67
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
9 changed files with 60 additions and 15 deletions

View File

@ -44,7 +44,7 @@ use Symfony\Component\HttpFoundation\Request;
* @copyright 2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Embed extends Controller
class OEmbed extends Controller
{
/**
* Handle OEmbed server requests

View File

@ -118,8 +118,7 @@ class Embed extends Plugin
*/
public function onAddRoute(RouteLoader $m): bool
{
$m->connect('oembed', 'main/oembed', Controller\Embed::class);
$m->connect('embed', 'main/embed', Controller\Embed::class);
$m->connect('oembed', 'main/oembed', Controller\OEmbed::class);
return Event::next;
}
@ -141,7 +140,7 @@ class Embed extends Plugin
'link' => [
'rel' => 'alternate',
'type' => "application/{$format}+oembed",
'href' => Router::url('embed', ['format' => $format, 'url' => $url]),
'href' => Router::url('oembed', ['format' => $format, 'url' => $url]),
'title' => 'oEmbed',
], ];
}

View File

@ -57,8 +57,6 @@ class AttachmentEmbed extends Entity
private ?string $author_name;
private ?string $author_url;
private ?string $thumbnail_url;
private ?int $thumbnail_width;
private ?int $thumbnail_height;
private \DateTimeInterface $modified;
public function setLinkId(int $link_id): self
@ -188,12 +186,13 @@ class AttachmentEmbed extends Entity
{
$attr = ['class' => 'u-photo embed'];
$attachment = DB::find('attachment', ['id' => $this->getAttachmentId()]);
$thumbnail = $attachment->getThumbnail('medium');
if (is_null($attachment) || is_null($attachment->getWidth()) || is_null($attachment->getHeight())) {
$attr['has_attachment'] = false;
} else {
$attr['has_attachment'] = true;
$attr['width'] = $attachment->getWidth();
$attr['height'] = $attachment->getHeight();
$attr['width'] = $thumbnail->getWidth();
$attr['height'] = $thumbnail->getHeight();
}
return $attr;
}

View File

@ -3,7 +3,7 @@
{% if attributes['has_attachment'] != false %}
{% set thumbnail_parameters = {
'id': embed.getAttachmentId(),
'size': 'small'
'size': 'medium'
} %}
<img alt="{{embed.getTitle() | escape}}" class="{{attributes['class']}}"
width="{{attributes['width']}}" height="{{attributes['height']}}"

View File

@ -1,7 +1,10 @@
<figure>
{% set thumbnail = attachment.getThumbnail() %}
<img class="u-photo"
alt="{{ attachment.getBestTitle(note) }}"
src="{{ attachment.getThumbnailUrl() }}">
src="{{ attachment.getThumbnailUrl() }}"
width="{{ thumbnail.getWidth() }}"
height="{{ thumbnail.getHeight() }}">
<figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption>

View File

@ -178,7 +178,7 @@ class StoreRemoteMedia extends Plugin
if (!$this->getStoreOriginal()) {
$thumbnail = AttachmentThumbnail::getOrCreate(
attachment: $attachment,
size: 'small',
size: 'medium',
crop: $this->getSmartCrop()
);
$attachment->deleteStorage();

View File

@ -174,12 +174,12 @@ class VideoEncoder extends Plugin
*
* @return bool
*/
public function onViewAttachmentVideo(array $vars, array &$res): bool
public function onViewAttachment(array $vars, array &$res): bool
{
$res[] = Formatting::twigRenderFile('videoEncoder/videoEncoderView.html.twig',
['attachment' => $vars['attachment'],
'thumbnail_parameters' => $vars['thumbnail_parameters'],
'note' => $vars['note'],
[
'attachment' => $vars['attachment'],
'note' => $vars['note'],
]);
return Event::stop;
}

View File

@ -30,6 +30,7 @@ use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException;
@ -345,6 +346,21 @@ class Attachment extends Entity
return Router::url('attachment_view', ['id' => $this->getId()]);
}
/**
* @param null|string $size
* @param bool $crop
*
* @throws ClientException
* @throws NotFoundException
* @throws ServerException
*
* @return AttachmentThumbnail
*/
public function getThumbnail(?string $size = null, bool $crop = false): AttachmentThumbnail
{
return AttachmentThumbnail::getOrCreate(attachment: $this, size: $size, crop: $crop);
}
public function getThumbnailUrl(?string $size = null)
{
return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'size' => $size ?? Common::config('thumbnail', 'default_size')]);

View File

@ -65,6 +65,8 @@ class AttachmentThumbnail extends Entity
private ?string $mimetype;
private int $size = self::SIZE_SMALL;
private string $filename;
private int $width;
private int $height;
private \DateTimeInterface $modified;
public function setAttachmentId(int $attachment_id): self
@ -122,6 +124,28 @@ class AttachmentThumbnail extends Entity
return $this->modified;
}
public function getWidth(): int
{
return $this->width;
}
public function setWidth(int $width): self
{
$this->width = $width;
return $this;
}
public function getHeight(): int
{
return $this->height;
}
public function setHeight(int $height): self
{
$this->height = $height;
return $this;
}
// @codeCoverageIgnoreEnd
// }}} Autocode
@ -199,6 +223,8 @@ class AttachmentThumbnail extends Entity
$filename = "{$predicted_width}x{$predicted_height}{$ext}-" . $attachment->getFilehash();
$thumbnail->setFilename($filename);
$thumbnail->setMimetype($mimetype);
$thumbnail->setWidth($predicted_width);
$thumbnail->setHeight($predicted_height);
DB::persist($thumbnail);
DB::flush();
$temp->move(Common::config('thumbnail', 'dir'), $filename);
@ -326,6 +352,8 @@ class AttachmentThumbnail extends Entity
'mimetype' => ['type' => 'varchar', 'length' => 129, 'description' => 'resource mime type 64+1+64, images hardly will show up with long mimetypes, this is probably safe considering rfc6838#section-4.2'],
'size' => ['type' => 'int', 'not null' => true, 'default' => 0, 'description' => '0 = small; 1 = medium; 2 = big'],
'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thumbnail filename'],
'width' => ['type' => 'int', 'not null' => true, 'description' => 'width in pixels, if it can be described as such and data is available'],
'height' => ['type' => 'int', 'not null' => true, 'description' => 'height in pixels, if it can be described as such and data is available'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
],
'primary key' => ['attachment_id', 'size'],