(Moderator’s note: The original title was: “Shrink full size images in post”)
I built a WordPress site for someone and they entered a ton of posts with images that are wider than the content area. Is there a way to shrink all of the image attachements to use a max width? They are entered as “full size”, not as thumbnail, medium, etc.
Great question! WordPress lacks some of the higher level imaging management features that would make it file maintenance like you need so much easier. The core WordPress team has been threatening to add enhance image features for many versions; eventually they might actually do it! Until then you’ll have to use a patchwork of plugins and/or custom code.
More good news and bad news: The good news is WordPress combined with PHP has tons of lower level imaging handling features but the bad news is it has tons of lower level imaging handling features from which you have to decide which to use! A while back I posted a list of image handling functions found in WordPress and if you’ll scan it you’ll probably see there are several different ways to skin this cat.
That said, I picked one approach and it follows below with copious comments rather than explain here in the text. You can just copy it to the root of your website as something like
/downsize-images.php
and call it from your browser. If it times out, don’t worry; I wrote it to keep track of what it had already done so just keep running it until you see it print “Done!”NOTE: You need to set the constant
MAX_RESIZE_IMAGE_TO
to whatever you want your maximum dimension to be; my example used640
pixels. After it runs you’ll have a bunch of image files with an extension of.save
appended. Once you are happy that it ran like you wanted you can just delete those.save
files. I didn’t automatically delete them in case something went wrong with the script.(IMPORTANT: Be SURE TO BACKUP both your database and your upload directory BEFORE you run this. It worked on my machine but you might have something I didn’t test and it might cause corruption so backup! DO NOT come back later and say I didn’t warn you!!!):
You can do a max-width. In the functions.php do something similar to what is done in Twenty Ten.
Set the size in you style.css
In Settings->Media change the max size to your maximum width, in this example 640.
You need to do all of these things for it to work. Then WP will create a “large” image that is the correct size for the webpage. The user will then have to insert the “large” image rather than the full size image.
Then use Regenerate Thumbnails plugin by Viper007Bond. http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/ which will regenerate all the new image sizes.
Not sure how to deal with the images that have already been inserted at “full size”, aside from re-inserting them manually.
Not sure how the images are being displayed in the post, but you can retrieve a specified size image by using wp_get_attachment_image() – see the codex – I would have to know how the images are being displayed to give more detailed answer.
I would suggest you fix this using the operating system or software.
What i would do is download the images from hosting, use automator in my OSX or record a some photoshop macro to resize the images or use some mass image resizer – resize, reupload
If the blog is on your own server and you have command line access, i guess it would be easier to resize using some commands (i need more details on ur system to be able to tell)
you can also try writing some php script that resize the images – it should be pretty easy to do – there are bunch of image resizing scripts around if you google (make sure you run it on some test images first before ruining the whole gallery)
and good luck 🙂
you could use the gd extension in php, or imagemagick binaries on the linux cli. timthumb.php is widely used for thumbnails etc, you could do something with that as well. ideally for the next post you’ll make sure the client does it properly, instead of you creating a plugin that intercepts all the img calls, and runs em through your resize filter :).