WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] hook after saving an image in the editor (6 posts)

  1. nosilver4u
    Member
    Posted 1 year ago #

    I was originally posting this in Hacks, but as it seems this could be an unintentional regression, I'm posting here (feel free to move it if I'm wrong).
    I've written a plugin that optimizes images when they are uploaded. However, if a user edits an image after uploading it, the new image does not get optimized. I discovered an 'edit_attachment' action hook that almost works, but if the user clicks Save, but not Update, then the hook doesn't fire.

    Then, I found this:
    http://adambrown.info/p/wp_hooks/hook/wp_save_image_file?version=3.4&file=wp-admin/includes/image-edit.php

    However, it seems to have disappeared in 3.5 with the new WP_Image_Editor class.
    Is this the hook I'm looking for? If so, can it be added back in? If not, is there another usable hook?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with the add_attachment hook:

    this is from the function wp_insert_attachment():

    if ( $update) {
    		do_action('edit_attachment', $post_ID);
    	} else {
    		do_action('add_attachment', $post_ID);
    	}
  3. nosilver4u
    Member
    Posted 1 year ago #

    That's the exact code I was looking at. I already have the optimize process hooked onto when an attachment is added/uploaded (actually right after the metadata is generated), so the add_attachment hook doesn't do anything new for me.

    I'm specifically looking to re-optimize after a user goes into the WordPress Image Editor, and clicks save/crop/scale (anything that causes the image to be modified) WITHOUT clicking the Update button.

  4. Mike Schroder
    WP Contributing Dev.
    Posted 1 year ago #

    What hook(s) were you previously using?

    It's intentional that there is no longer direct access to the GD Resources of images that are in the editor.

    Instead, while not quite the same thing, there are new hooks that will return WP_Image_Editor objects that you can perform operations on:

    • image_save_pre is now image_editor_save_pre
    • wp_save_image_file is now wp_save_image_editor_file
    • image_edit_before_change is now wp_image_editor_before_change

    The reason behind this is that GD had to be abstracted out in order to support other Image Manipulation engines (like Imagick).

    The cool thing here is that it means you can also add your own engine now (to support things like jpegtran or OptiPNG natively).

    More in-depth tutorials on using the API are yet to come, but there's now a quick intro here (including the filter changes noted above):
    http://make.wordpress.org/core/2012/12/06/wp_image_editor-is-incoming/

  5. nosilver4u
    Member
    Posted 1 year ago #

    I wasn't previously using any hook, so edited images never get optimized unless the user manually optimizes them.

    The one I thought might do the trick was wp_save_image_file, but I couldn't find it in 3.5, so you answered part of my problem. Unfortunately, that hook is too early in the process.

    My plugin waits until the metadata has been generated for an image, so that we can figure out filenames for thumbnails and other resizes and optimize those as well. When the wp_save_image_editor_file filter is run, the metadata has not yet been updated. So I'm apparently back to the beginning of trying to find an applicable hook. If anyone has ideas for that, please please let me know.

    All that said, you seem to have introduced a new solution going forward. If I understand correctly, my plugin could now run the optimizers directly on the images when they are created including thumbnails/resizes. I'll be looking forward to that tutorial, but hopefully I can find a solution for those still using old versions of wordpress too.

  6. Mike Schroder
    WP Contributing Dev.
    Posted 1 year ago #

    Great, glad to hear it!

    Indeed, WP_Image_Editor should let you do just that, for 3.5+ either through extending the existing editors, or by creating a few -- one for each of the backends you want to support.

Topic Closed

This topic has been closed to new replies.

About this Topic