5

I'm trying to run a little function to determine the luminance of an image and store that in the postmeta 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?

ive looked 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 this isnt working.

Thanks in advance for any assistance


SOLUTION Thanks to s_ha_dum

function get_avg_luminance($filename, $num_samples=10) {
    $img = imagecreatefromjpeg($filename);

    $width = imagesx($img);
    $height = imagesy($img);

    $x_step = intval($width/$num_samples);
    $y_step = intval($height/$num_samples);

    $total_lum = 0;

    $sample_no = 1;

    for ($x=0; $x<$width; $x+=$x_step) {
        for ($y=0; $y<$height; $y+=$y_step) {

            $rgb = imagecolorat($img, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;

            // choose a simple luminance formula from here
            // http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
            $lum = ($r+$r+$b+$g+$g+$g)/6;

            $total_lum += $lum;

            $sample_no++;
        }
    }

    // work out the average
    $avg_lum  = $total_lum/$sample_no;
    return $avg_lum;
    // assume a medium gray is the threshold, #acacac or RGB(172, 172, 172)
    // this equates to a luminance of 170
}

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);

this creates a number between 0 and 255 that represents the luminance of the image.. this is particularly useful if you are layering text over a background image and wish to know if the image is mostly light or mostly dark.

1 Answer 1

5

WordPress' media handling strikes me as scattered and inconsistent. I say that only to say that I can't promise this will work in all cases. However, I think I'd use the add_attachment hook from wp_insert_attachment.

You will get a post ID, so you will have to ...

  1. retrieve the image src with wp_get_attachment_imge_src, probably,
  2. retrieve the image itself and process it (not sure how you intend to do that),
  3. and save the extra data.
3
  • thanks so much, this helped heaps, i will update my question with the full solution to what i was trying to do :)
    – haxxxton
    Commented Oct 8, 2013 at 16:09
  • ok,wanted to ask will this run for every attachment if a post has an image gallery?
    – 10wtaylor
    Commented May 21, 2014 at 10:06
  • 1
    If you have written a solution, please post it in the "Answer" box, not as a edit to your question.
    – s_ha_dum
    Commented May 21, 2014 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.