acf-image-aspect-ratio-crop icon indicating copy to clipboard operation
acf-image-aspect-ratio-crop copied to clipboard

Automatically pull in alt-text from original image

Open benjibee opened this issue 3 years ago • 17 comments

I can't think of many use cases for having different alt-text for different image sizes, or for different aspect-ratios, for that matter. Therefore I'd suggest adding a hook directly to wp_get_attachment_image_attributes that checks for missing alt-text and attempts to set the alt-text directly so the image tag displays as expected.

Below is an example of such a function:

/**
 * Checks for a missing or empty alt tag and attempts to find the alt text
 * from the "original image" meta value provided by the ACF Image Aspect Ratio Crop plugin
 */

add_filter('wp_get_attachment_image_attributes', function ($attr, $attachment) {
    if (!isset($attr['alt']) || empty($attr['alt'])) {
        // attempt to get the ID of the original image which may contain the original alt text
        $original_id = get_post_meta($attachment->ID, 'acf_image_aspect_ratio_crop_original_image_id', true);
        $original_alt = get_post_meta($original_id, '_wp_attachment_image_alt', true);

        // assign this value to the alt key (will be empty if either get_post_meta call above fails)
        $attr['alt'] = $original_alt;
    }

    return $attr;
}, 10, 2);

benjibee avatar Jul 01 '21 12:07 benjibee

hi @benjibee maybe you will be interested in this solution which copies the alt text of the original when the new cropped image is created: #58

dylantuohy avatar Jul 03 '21 18:07 dylantuohy

Hey @dylantuohy thanks for the suggestion. That would actually create another issue as far as I'm concerned, namely duplicate contact that would then have to be updated if the original changes.

WordPress currently shares alt-texts between image sizes because logically there are very few reasons (outside of very specific art-direction) that you'd want different alt-text for different images, so I can't see why one would want to copy text over when it should actually remain connected to the original image's meta data.

benjibee avatar Jul 05 '21 10:07 benjibee

Hey, thanks for the PR. I'm thinking of including this feature in the next major version of the plugin. I like this approach better than copying the values.

I'll probably add an option to enable and disable this functionality.

I have just a few questions.

With the wp_get_attachment_image_attributes filter, where are the values visible? Are they visible when using get_field ? Are they visible in the WordPress image edit modal?

It's possible to overwrite the values so that cropped image has different meta information than the original, right?

joppuyo avatar Oct 21 '21 16:10 joppuyo

Hey @joppuyo I'm glad to hear this might be helpful!

From my experience so far, it looks like wp_get_attachment_image_attributes pretty much filters the caption everywhere. I've since stopped using this plugin, however, so I can't test it right away. If you don't have time to test it yourself I can setup a test case in the coming weeks.

As for overwriting the values, it should only attempt to fecth the original alt if no alt text is present on the cropped image, so that shouldn't be an issue at all.

benjibee avatar Oct 22 '21 10:10 benjibee

Wordpress uses the same photo data when it has users crop images. Since the image topic is typically the same or similar the user would appreciate the head start on editing what was there. Please add this feature. https://developer.wordpress.org/reference/functions/wp_ajax_crop_image/

mbateam avatar Aug 08 '22 00:08 mbateam

I personally need the description for my purposes and would appreciate this.

mbateam avatar Aug 08 '22 00:08 mbateam

Hey, unfortunately I haven’t had very much time lately so progress on the plugin has been a bit slow. However, right now I’m focused on the new version 7.0.0 of the plugin which is a partial rewrite of the plugin.

The current code of the plugin is a bit of a mess and after the version 7.0.0 is complete, it will be much easier to add new features to the plugin. I’m pretty sure I will be able to complete the rewrite in upcoming months and after I can focus on new features such as this.

I acknowledge that it would be nice to have this feature as soon as possible but it wouldn’t make much sense to add features to the current codebase because it’s in need of major improvement right now.

joppuyo avatar Aug 13 '22 13:08 joppuyo

Thanks for responding. Is there an email list I can subscribe to so I know when it is ready or if you need testing?

James


From: Johannes Siipola @.> Sent: Saturday, August 13, 2022 9:49:08 AM To: joppuyo/acf-image-aspect-ratio-crop @.> Cc: James Kennedy @.>; Comment @.> Subject: Re: [joppuyo/acf-image-aspect-ratio-crop] Automatically pull in alt-text from original image (#71)

Hey, unfortunately I haven’t had very much time lately so progress on the plugin has been a bit slow. However, right now I’m focused on the new version 7.0.0 of the plugin which is a partial rewrite of the plugin. The current code of the plugin is a bit of a mess and after the version 7.0.0 is complete, it will be much easier to add new features to the plugin. I’m pretty sure I will be able to complete the rewrite in upcoming months and after I can focus on new features such as this. I acknowledge that it would be nice to have this feature as soon as possible but it wouldn’t make much sense to add features to the current codebase because it’s in need of major improvement right now.

— Reply to this email directly, view it on GitHubhttps://github.com/joppuyo/acf-image-aspect-ratio-crop/issues/71#issuecomment-1214162938, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAHEOQ3TQFZEGAAGLRG422TVY6RVJANCNFSM47UNHOKA. You are receiving this because you commented.Message ID: @.***>

mbateam avatar Aug 13 '22 23:08 mbateam

I will be updating this ticket when there’s progress on this issue so you should get a notification since you are participating in this issue.

joppuyo avatar Aug 14 '22 09:08 joppuyo

I will be updating this ticket when there’s progress on this issue so you should get a notification since you are participating in this issue.

In your development on the new version. Are you creating a feature for front end editing like this: https://fengyuanchen.github.io/cropperjs/ where the user does not have to leave the page to crop. Right now users have to go to the backend, open media gallery or modal window click multiple times then save and then revisit the page to get a look at what they did. This is way to cumbersome. It would be great if the user could have a visual of the entire page on the front end and see how it looks with the proposed cropped version, before actually cropping the image. If the user doesn't like how it looks they can reset the cropped view to what was there before, then when they like the look click crop. There is no reason to have to restore an image to previous state until after they actually crop it. The open in modal or visit the media gallery and click multiple times save and then revisit the page is way to cumbersome.

mbateam avatar Aug 24 '22 11:08 mbateam

In your development on the new version. Are you creating a feature for front end editing like this

Hey, this kind of functionality sounds so complicated it's out of scope for this plugin.

Have you tried to use this plugin in conjunction with ACF blocks? This would allow you to see the field controls in the sidebar and a preview of the element inside the block editor.

joppuyo avatar Aug 24 '22 11:08 joppuyo

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

mbateam avatar Aug 24 '22 11:08 mbateam

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

Since this is an ACF field, I'm limited by the way ACF works. That's why it's not possible to build this sort of integration with the front end.

I'm not planning on moving the cropper "inline" on the page because there simply isn't enough space for it. The modal allows the image to take up the entire screen which makes the cropping process easier.

I think ACF blocks would be your best bet for previewing how the image looks in the context of a particular element.

joppuyo avatar Aug 24 '22 12:08 joppuyo

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

Since this is an ACF field, I'm limited by the way ACF works. That's why it's not possible to build this sort of integration with the front end.

I'm not planning on moving the cropper "inline" on the page because there simply isn't enough space for it. The modal allows the image to take up the entire screen which makes the cropping process easier.

I think ACF blocks would be your best bet for previewing how the image looks in the context of a particular element.

Is this the resource on how to implement this solution: https://www.advancedcustomfields.com/resources/blocks/

mbateam avatar Aug 24 '22 12:08 mbateam

Is this the resource on how to implement this solution: https://www.advancedcustomfields.com/resources/blocks/

Yes, this is the functionality I'm talking about. If you need more support with this, I would recommend opening another ticket since this isn't related to the alt text that this issue is about.

joppuyo avatar Aug 24 '22 12:08 joppuyo

Done, sorry for making a mess!

mbateam avatar Aug 24 '22 12:08 mbateam

As pointed out here, this could cause more issues than it solves in my opinion. We always have original_image to access the data.

jameelmoses avatar Sep 02 '22 14:09 jameelmoses