Skip to content

Instantly share code, notes, and snippets.

@fabianmarz
Created July 31, 2017 16:06
Show Gist options
  • Save fabianmarz/f58377b4fb98388a31fc79d6c4e81b2c to your computer and use it in GitHub Desktop.
Save fabianmarz/f58377b4fb98388a31fc79d6c4e81b2c to your computer and use it in GitHub Desktop.
<?php
/**
* Regenerate missing image sizes only.
*
* Note: Untested code
*/
function regenerate($image_size = '', $attachment_id = []) {
global $wpdb;
$sql = "SELECT ID FROM {$wpdb->posts} WHERE post_mime_type LIKE '%image%'";
if (!empty($attachment_id)) {
$sql .= "AND ID IN (" . esc_sql(implode(',', $attachment_id)) . ")";
}
$results = $wpdb->get_col($sql);
foreach ($results as $key => $attachment_id) {
echo "Attachment ID: {$attachment_id} \n";
$metadata = [];
$file = _load_image_to_edit_path($attachment_id);
if (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']);
$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
$metadata['file'] = _wp_relative_upload_path($file);
global $_wp_additional_image_sizes;
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = [
'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");
}
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");
}
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");
}
}
$sizes = apply_filters('intermediate_image_sizes_advanced', $sizes);
$attachment_meta = wp_get_attachment_metadata($attachment_id);
foreach ($sizes as $size => $size_data ) {
if (isset($attachment_meta['sizes'][$size])) {
$metadata['sizes'][$size] = $attachment_meta['sizes'][$size];
}
else {
$resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
if ($resized) {
$metadata['sizes'][$size] = $resized;
echo "{$size} regenerated \n";
}
else {
echo "{$size} failed \n";
}
}
}
if (!empty($image_size)) {
$resized = image_make_intermediate_size($file, $sizes[$image_size]['width'], $sizes[$image_size]['height'], $sizes[$image_size]['crop']);
if ($resized) {
$metadata['sizes'][$image_size] = $resized;
echo "{$image_size} regenerated \n";
}
else {
echo "{$image_size} failed \n";
}
}
if ($attachment_meta['image_meta']) {
$metadata['image_meta'] = $attachment_meta['image_meta'];
}
}
wp_update_attachment_metadata($attachment_id, $metadata);
echo "----------\n";
}
return;
}
@fabianmarz
Copy link
Author

This code was written long time ago and may break your page. Please test before running on a production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment