S3-Media-Storage
S3-Media-Storage copied to clipboard
Meta Data Loss
When uploading via the media library, all image meta data (thumbnail sizes) are lost, the resized images are uploaded to cloudfront, but the meta data does not exist, See example:
[thumbnail] => https://xxxxxxxxxxxxx.cloudfront.net/2014/07/ [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => https://xxxxxxxxxxxxx.cloudfront.net/2014/07/ [medium-width] => 300 [medium-height] => 199 [large] => https://xxxxxxxxxxxxx.cloudfront.net/2014/07/
as you can see the image path is not complete.
DATABASE: {s:9:"thumbnail";a:3:{s:4:"file";s:0:"";s:5:"width";i:150;s:6:"height";i:150;}
This is probably a really messy way of doing this, but ive created a temp fix:
function cloudfront_fix( $attachmentid, $item_img_details){
// Get attachment metadata
$attachment_metadata = wp_get_attachment_metadata( $attachmentid );
// if metadata broken apply fix
if( $attachment_metadata['sizes'][$item_img_details]['file'] == '' || empty( $attachment_metadata['sizes'][$item_img_details]['file'] )){
$main_file = $attachment_metadata['file'];
$thumb_height = $attachment_metadata['sizes'][$item_img_details]['height'];
$thumb_width = $attachment_metadata['sizes'][$item_img_details]['width'];
$edit_main_file = explode('.', $main_file);
$remove_dates = explode('/', $edit_main_file[0]);
$updated_meta = end( $remove_dates ) . '-' . $thumb_width . 'x' . $thumb_height . '.' . $edit_main_file[1];
$attachment_metadata['sizes'][$item_img_details]['file'] = $updated_meta;
wp_update_attachment_metadata( $attachmentid, $attachment_metadata );
}
}
function fix_all_image_metadata(){
if ( isset($_GET['fix']) && $_GET['fix'] == 'thumbs' ) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'any',
'posts_per_page' => -1
);
// get results
$the_query = new WP_Query( $args );
while( $the_query->have_posts() ){
$the_query->the_post();
$attachment_metadata = wp_get_attachment_metadata( get_the_ID() );
foreach( $attachment_metadata['sizes'] as $key => $value ){
cloudfront_fix( get_the_ID(), $key );
}
}
wp_reset_postdata();
}
}
add_action('init', 'fix_all_image_metadata');