Why is WordPress not generating Image thumbnails that are specifically set using add_image_size()
I've had this problem before and it just happened again today.
I was importing posts from another WordPress install and the image thumbnails were not being generated. I even tried uploading new images but still no thumbnails.
Themes and plugins can use a function called add_image_size()
to register a new image size. For example we could include this in our theme:
add_image_size('medium-thumbnail', 160, 160)
That would tell WordPress that when the user uploads a new image WordPress should create a version of that image in the sizes that are inserted in the function, in this case a 160x160 pixel image.
Regenerate Thumbnail
My first thought was to install a plugin like Regenerate Thumbnail which enables you to run through all the images you have uploaded and regenerate the extra image sizes needed. This is very useful especially when you're importing posts from another site.
If you're having issues with image sizes when migrating from another theme I would try this plugin.
The Regenerate Thumbnails plugin seemed to run fine and it looked like all the images were resized and ready to go but after reloading the page, where the images were wrong, I saw it didn't work.
GD PHP extension
After a few minutes it hit me, WordPress uses a PHP extension called GD to manipulate images and since I was working on a new server setup I haven't installed it.
All I needed to do was run this via SSH:
apt-get install php5-gd
After restarting Apache I ran Regenerate Thumbnails again and it worked perfectly. Your hosting provider can help you enable this if you don't have SSH access to your server.
It's strange that WordPress doesn't notify you about this, it accepts the add_image_size
function and when you upload an image it goes through it like everything is all right but in reality it fails silently to create the necessary thumbnails, all without notice.
How to check if GD is installed
Paste this PHP code in your functions.php file and reload the front page.
if(extension_loaded('gd') && function_exists('gd_info')){
echo 'GD is installed!';
}else{
echo 'GD is NOT installed!';
}
exit();
You should see a white page with text that says either that GD is installed or not.
I hope this helps.
If you've had image issues in WordPress that were caused by something else, please comment below and share your experience.