.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 Tricktake 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 itthumb/.htaccess 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 onceNotice: I will use Image Transform class ( @phpclasses.org by Lito) to perform image manipulation operationsGetting Startedthumb/index.phpinclude_once ('../inc/transform.class.php'); if (!$_GET['thumb']) { exit('error'); } $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); break; 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); } break; 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))); } break; case 'flip': case 'flop': $file = $thumb_array[0]; $path = $upload_path.$file; $save = implode(",",array($mode,$file)); $imageTransform->view($mode, $path, null, $save); break; default : exit('This is not valid mode'); }
Resize or CropGo to "/thumb/resize or crop,size(width x height),image file" eg. /thumb/crop,50x50,google.jpgthis will go to $upload_path ('../upload/google.jpg'), then save it in thumb as crop,50x50,google.jpg RotateGo to "/thumb/rotate,degree(intger),image file" eg. /thumb/rotate,90,google.jpgthis will go to $upload_path ('../upload/google.jpg'), then save it in thumb as rotate,90,google.jpg Make it GrayGo to "/thumb/gray,image file" eg. /thumb/gray,google.jpgthis 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.
|
© Advanced Web Core. All rights reserved