How to batch resize images and change image url?
-
17-04-2021 - |
Domanda
I'm making a new theme for a blog that will be slightly narrower. Thus I need to resize every image and also change the image url in every post. Is there any kind of plugin handling this?
Soluzione
This plugin might help you : Regenerate Thumbnails
Just change the image dimensions in WP's Media Settings and run the plugin.
Altri suggerimenti
in function.php you can define your default thumbnail size :
// Set featured image sizes
add_theme_support( 'post-thumbnails');
set_post_thumbnail_size( 320, 320);
And then you can even define different format which you can use in your custom theme
add_image_size( 'sidebar', 75, 75, true);
add_image_size( 'gallery', 159, 159, true);
function custom_gallery(){
global $post;
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$images = get_posts($args);
foreach($images as $image){
if($images){
// use gallery size thumbnails
$thumbnail_array = image_downsize( $image->ID, 'gallery' );
$thumbnail_url = $thumbnail_array[0];
}
}
}
Wait, wait… How do you tell your SQL replace what height the thumbnail generated by wordpress is?
So far the only solution I found is to searh/replace all the filenames and get rid of the width, making it ending like picturename-100x.jpg and then either batch replace all the urls in the posts contents or by creating an .htaccess redirect to send any image like picturename-100x\d\d\d?.jpg to picturename-100.jpg
If you come up with a better solution, let me know!
Ideally, I'd like a plugin that figures out the height from the existing same-name images (disregarding 000x000.jpg) and to write the proper width-height.