Delete full size image after upload


#1

Hi there, I use this fonction in my template (buddypress) :

function replace_uploaded_image($image_data) {  
    if (!isset($image_data['sizes']['large'])) return $image_data;  

    $upload_dir = wp_upload_dir();  
    $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];  
    $large_image_location = $upload_dir['basedir'] . '/' . substr($image_data['file'], 0, 8) . $image_data['sizes']['large']['file'];  

    unlink($uploaded_image_location);  
    rename($large_image_location,$uploaded_image_location);  

    $image_data['width'] = $image_data['sizes']['large']['width'];  
    $image_data['height'] = $image_data['sizes']['large']['height'];  
    unset($image_data['sizes']['large']);  

    return $image_data;  
}  

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');  

The aim is to improve my server space storage. So I automaticaly delete full image and replace it by the large one. Everything works fine with all my functions and home-made plugins but not with rtmedia. :-(

I'm not sure, but I think it worked before the last update. Is it a hook problem ? In insert_attachment() you wrote a comment "FIX WORDPRESS 3.6 METADATA", and call media.php... At the begining I thaught it was the reason wy my filter was overriden, but I tried to remove it and there is no change.

Thanks.


#2

@capobianco.brice - We are having a look to check if it is some issue from our end. Will let you know regarding this soon.


#3

Hi @joshuaabenazer, don't waste your time. I think I found the solution by reading the first line of my function and inspecting my media gallery (in back office)... In fact rtmedia do not use the default wordpress thumbs size. So, there is no ['large'] thumb an my function return the default data if so. I'll try to night to duplicate/adapt the function with the rtmedia thumb 'large' equivalent. Can you please give the name of the different thumbs used in rtmedia ? Thanks a lot.


#4

Well, after some tweeks, it works.

the new function (works for rtmedia and other thumnails too)

function replace_uploaded_image($image_data) {  
    if (!isset($image_data['sizes']['large']) && !isset($image_data['sizes']['rt_media_single_image'])) {  
        return $image_data;  
    } else {  
        if (isset($image_data['sizes']['large'])) {  
            //wordpress default thumbnails  
            $the_image = $image_data['sizes']['large'];  
        } else if (isset($image_data['sizes']['rt_media_single_image'])) {  
            //rtmedia default thumbnails  
            $the_image = $image_data['sizes']['rt_media_single_image'];  
        }  
    }  

    $upload_dir = wp_upload_dir();  
    $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];  
    //$large_image_location = $upload_dir['basedir'] . '/' . substr($image_data['file'], 0, 8) . $the_image['file'];  
    $large_image_location = $upload_dir['path'] . '/' . $the_image['file'];  

    unlink($uploaded_image_location);  
    rename($large_image_location,$uploaded_image_location);  

    $image_data['width'] = $the_image['width'];  
    $image_data['height'] = $the_image['height'];  
    unset($the_image);  

    return $image_data;  
}  

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');  

And I had to modify the way tje image is called in media-single.php template: replace this <?php rtmedia_media ( true ); ?>

by this <?php rtmedia_media ( true, true, 'full' ); ?>

Hope this help


#5

#6

#7