its_me its_me - 1 year ago 78
PHP Question

How does resize images dynamically?

NOTE: This question is tricky (to decide whether it pertains to SO or not), but this is no simple WordPress question.

I've heard that has a separate layer of infrastructure (Nginx based servers) for images that is pretty much independent of the hosted WordPress blogs.

The thing is, any image on a blog can be resized simply by appending

(width) and/or
(height) parameters like so:

Is the script available on the web? (Because, they always say "We’re strong believers in Open Source, and we try to open source everything we can.")

Most of the dynamic image resizing scripts out there are insecure and very vulnerable to attacks (Tim Thumb), or badly written, or come at the cost of performance. Is/Are there any good ones?

PS: There are probably several other dynamic image resizing scripts, like TimThumb, for example. But I am only interested in what blogs use.

Answer Source

(1) Use TimThumb, then re-sizing your images is as simple as appending some parameters:

(2) Then add these rewrite rules in your site's root .htaccess file:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(gif|png|jpg|jpeg)
RewriteCond %{QUERY_STRING} (w|h)=(.*)$
RewriteRule (.*) /full/path/to/timthumb.php?src=$1&%1=%2&%3=%4 [L]

Quick breakdown of the rules:

  1. Check that the URI exists as a file.
  2. Check that the URI in question is an image. Expand to other formats as needed.
  3. Check that one or both of width and height are being altered.
  4. Pass the file following to timthumb as variables:
    • $1 = filename
    • %1 = w or h
    • %2 = value of w or h
    • %3 = w or h
    • %4 = value of w or h

(3) Now, you can pretty much resize your images as you would on, i.e. like so:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download