WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] hook into completed image upload filter (3 posts)

  1. haxxxton
    Member
    Posted 6 months ago #

    Heya,

    I'm trying to run a little function to determine the luminance of an image and store that in the post_meta for that image..

    I have the function working, but id like it to fire when an image has finished uploading.. does anyone know the function/filter/action i should hook into for this?

    perhaps the one that creates the '_wp_attachment_metadata' postmeta?

    Thanks in advance

    PS. will happily post my code when i can get this all sorted :)

  2. haxxxton
    Member
    Posted 6 months ago #

    update:

    ive now had a look at

    • image_attachment_fields_to_save
    • attachment_fields_to_save
    • wp_handle_upload
    • media_upload_form_handler
    • wp_generate_attachment_metadata

    and i just cant seem to make any of them simply run an addition to the postmeta table for the image

    currently my code looks something like:

    function insert_luminance_data($post, $attachment) {
        if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
            $lum = 'TEST';
            add_post_meta( $post['ID'], 'image_lum', $lum, true ) || update_post_meta( $post['ID'], 'image_lum', $lum );
        }
        return $post;
    }
    
    add_filter('image_attachment_fields_to_save', 'insert_luminance_data', 10, 2);

    but like i say.. this isnt working

  3. haxxxton
    Member
    Posted 6 months ago #

    SOLUTION thanks to s_ha_dum (over at wordpressexchange)

    function insert_luminance_data($post_ID) {
        $src = wp_get_attachment_image_src( $post_ID, 'large' )[0];
        $lum = get_avg_luminance($src, 10, true);
        add_post_meta( $post_ID, 'image_lum', $lum, true ) || update_post_meta( $post_ID, 'image_lum', $lum );
        return $post_ID;
    }
    
    add_filter('add_attachment', 'insert_luminance_data', 10, 2);

    the get_avg_luminance function (due to its size) can be found at this pastebin: http://pastebin.com/T97Mj5m8

Reply

You must log in to post.

About this Topic