Have an account? Sign in
Login  Register  Facebook
.htaccess Trick that lets you request and generate images of any size
[Edit] What does that mean?
You upload image.jpg to your image folder, upload and want the same image in differant size or you want to crop, rotate it or make it gray. what should you do?

The Trick

take a look at this .htaccess code, we use it to redirect incoming image requests through any Size to a PHP file to resize, crop or rotate it
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]
this code will check if the requested image already in the thumb folder or not, in case of existence the image (already resized, croped or rotated) will open directly, if not the php file will take the image and work on it

This mean the server will dealing with the image once

Notice: I will use Image Transform class ( @phpclasses.org by Lito) to perform image manipulation operations

Getting Started

include_once ('../inc/transform.class.php');
if (!$_GET['thumb']) {
$thumb = $_GET['thumb'];
$thumb_array = explode(',',$thumb);
$mode = array_shift($thumb_array);
$upload_path = '../upload/';
$imageTransform = new imageTransform; 
switch ($mode){ 
    case 'gray':
        $file = $thumb_array[0];
        $path = $upload_path.$file;        
        $save = implode(",",array($mode,$file));   
            $imageTransform->view('gray', $path, null, $save);
    case 'rotate':
        //degrees allowed.
        $degrees = array('90','180');
        $degree = $thumb_array[0];
        $file = $thumb_array[1];
        $path = $upload_path.$file; 
        $save = implode(",",array($mode,$degree,$file));   
        if (in_array($degree,$degrees)) {        
            $imageTransform->view('rotate', $path, $degree, $save);
    case 'resize':
    case 'crop':
        //sizes allowed
        $sizes = array('50x50','100x100','600x400');
        $size = $thumb_array[0];
        $file = $thumb_array[1];
        $path = $upload_path.$file;        
        if (in_array($size,$sizes)) {
            $imageTransform->view($mode, $path, $size,implode(",",array($mode,$size,$file)));
    case 'flip':
    case 'flop':
        $file = $thumb_array[0];
        $path = $upload_path.$file;        
        $save = implode(",",array($mode,$file));   
            $imageTransform->view($mode, $path, null, $save);
    default :
        exit('This is not valid mode');
  • $upload_path the folder which you uploading your images
  • $degrees in rotate case, this Lets you use specific degrees (90,-90,180,etc..)
  • $sizes in case of resize or crop, let you use specific sizes (To prevent the random operations)

Resize or Crop

Go to "/thumb/resize or crop,size(width x height),image file" eg. /thumb/crop,50x50,google.jpg
this will go to $upload_path ('../upload/google.jpg'), then save it in thumb as crop,50x50,google.jpg


Go to "/thumb/rotate,degree(intger),image file" eg. /thumb/rotate,90,google.jpg
this will go to $upload_path ('../upload/google.jpg'), then save it in thumb as rotate,90,google.jpg

Make it Gray

Go to "/thumb/gray,image file" eg. /thumb/gray,google.jpg
this will go to $upload_path ('../upload/google.jpg'), then save it in thumb as gray,google.jpg
Don’t be Afraid to Ask for Help
It’s only human nature to want to hide the fact that we don’t know much about a certain topic. Nobody likes being a n00b! But how are we going to learn without asking? Feel free to use our problems section to ask more seasoned PHP developers questions.
thumb resize htaccess September 22, 2011