Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alt attribute lost after resize/crop #6852

Closed
medienbaecker opened this issue Dec 9, 2024 · 0 comments · Fixed by #6855
Closed

Alt attribute lost after resize/crop #6852

medienbaecker opened this issue Dec 9, 2024 · 0 comments · Fixed by #6855
Assignees
Labels
type: bug 🐛 Is a bug; fixes a bug
Milestone

Comments

@medienbaecker
Copy link
Contributor

Description

Returning an image file also handles populating the alt attribute with the alt field. After resizing or cropping the image, this alt text is however lost, returning an empty alt attribute.

Without resizing

echo $page->image();
<img src="image.jpg" alt="My alt text">

Same image after resizing

echo $page->image()->resize(600);
<img src="image.jpg" alt="">

By simply returning an image, Kirby calls html(), as defined in its __toString() function. This method can be found here:

kirby/src/Image/Image.php

Lines 116 to 134 in 94cc37e

public function html(array $attr = []): string
{
// if no alt text explicitly provided,
// try to infer from model content file
if (
$this->model !== null &&
method_exists($this->model, 'content') === true &&
$this->model->content() instanceof Content &&
$this->model->content()->get('alt')->isNotEmpty() === true
) {
$attr['alt'] ??= $this->model->content()->get('alt')->value();
}
if ($url = $this->url()) {
return Html::img($url, $attr);
}
throw new LogicException('Calling Image::html() requires that the URL property is not null');
}

Expected behavior
The alt tag should be returned after resizing/cropping the image.

To reproduce

  1. Make sure your page has an image
  2. Return something like $page->image() and see the alt text
  3. Add ->resize(600) or ->crop(600, 400) to it and see the alt text disappear

Your setup

Kirby Version
Tested in 4.4.1 and 4.5.0

@afbora afbora added the type: bug 🐛 Is a bug; fixes a bug label Dec 9, 2024
@afbora afbora self-assigned this Dec 9, 2024
@afbora afbora added this to the 4.6.0 milestone Dec 9, 2024
afbora added a commit that referenced this issue Dec 9, 2024
@afbora afbora linked a pull request Dec 9, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Is a bug; fixes a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants