Home > Code Snippets > Automatic image sizing with static delivery – WordPress Enhancement

Automatic image sizing with static delivery – WordPress Enhancement

November 5th, 2009 Leave a comment Go to comments

Recently I got fed up trying to convince the operators of a client, for whom we had implemented a wordpress site. The operators, even after we repeatedly asked to upload only relevant sizes for certian positions, were trying to make one size fit for all. Hell, places where thumbnails of 1 to 2K would suffice, were being used up by 30K images, resized by our theme constraints. We were running wordpress on lighttpd.

I was already attracted towards the imagemagick, and we are already on ubuntu server os. So instead of using the php-gd library, I created a wrapper which would run the convert tool of imagemagick and do the resizing for us. Also to keep the aspect ratio as far as possible, I dropped in a bit of AI to detect the target image size, and aspect ratio. Deciding how to resize, and where to crop. I asked my associates to use a regexp replace with the dimensions we were going to use in thumbnails. This code was added to the template pages.


preg_replace('@\.(jpg|gif|png)$@',"__82x57$0",$str);


$HTTP["host"] == "wordpress" {
   $HTTP["url"] =~ "^/wp-content/uploads/(.*)(jpg|gif|png)$" {
	server.error-handler-404 = "/img_resize.php"
   }
   server.error-handler-404 = "/index.php"   
}

This was how I configured our wordpress on lighttpd, yea I know there is nothing fancy there, and the hostname is purposefully taken out. The php code is added as a download PHP ImageMagik Resize Tool (238)

Categories: Code Snippets Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

− eight = one