Support » Fixing WordPress » hook into completed image upload filter

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘hook into completed image upload filter’ is closed to new replies.