public static function wp_generate_attachment_metadata_custom($attachment_id, $file, $thumbnails = null)
{
$attachment = get_post($attachment_id);
$meta_datas = get_post_meta($attachment_id, '_wp_attachment_metadata', true);
$metadata = array();
if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
$imagesize = getimagesize($file);
$metadata['width'] = $imagesize[0];
$metadata['height'] = $imagesize[1];
list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
$metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
// Make the file path relative to the upload dir
$metadata['file'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
global $_wp_additional_image_sizes;
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = array('width' => '', 'height' => '', 'crop' => false);
if (isset($_wp_additional_image_sizes[$s]['width'])) {
$sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
} else {
$sizes[$s]['width'] = get_option("{$s}_size_w");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['height'])) {
$sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$sizes[$s]['height'] = get_option("{$s}_size_h");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['crop'])) {
$sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']);
} else {
$sizes[$s]['crop'] = get_option("{$s}_crop");
}
// For default sizes set in options
}
$sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
// Only if not all sizes
if (isset($thumbnails) && is_array($thumbnails) && isset($meta_datas['sizes']) && !empty($meta_datas['sizes'])) {
// Fill the array with the other sizes not have to be done
foreach ($meta_datas['sizes'] as $name => $fsize) {
$metadata['sizes'][$name] = $fsize;
}
}
foreach ($sizes as $size => $size_data) {
if (isset($thumbnails)) {
if (!in_array($size, $thumbnails)) {
continue;
}
}
$resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
if (isset($meta_datas['size'][$size])) {
// Remove the size from the orignal sizes for after work
unset($meta_datas['size'][$size]);
}
if ($resized) {
$metadata['sizes'][$size] = $resized;
}
}
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata($file);
if ($image_meta) {
$metadata['image_meta'] = $image_meta;
}
}
return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
}