Welcome Guest, Not a member yet? Register   Sign In
Htaccess to rewrite images folder
#1

[eluser]flyenig[/eluser]
Hello, i have a script that uploads images, creates a hash for it, creates 3 directories, and puts it inside the last directory like so...

Code:
images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg

(the "108_" is the user id)

my reason for the sub folders is because i am creating an application that is looking at millions of photos being uploaded so they need to be spread across evenly..

so im trying to turn
Code:
images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg
into
Code:
user_images/108/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg

how can i do that? i want that if someone wants to view the photo, they see the new directory in the url, not the one with 3 sub directories.. Thank you!!
#2

[eluser]CroNiX[/eluser]
Just create a user_images controller? Do you need the user_id as a separate dir if your image filename starts with {user_id}_{filename}? If none of your image filenames will have an underscore in them except where it separates the user id, you can explode on that to get the user id along with the filename and do away with the extra directory.

Then you could do something like:
/application/config/routes.php (could also do this using _remap())
Code:
$route['user_images/(:any)'] = 'user_images/view/$1';
$route['user_images'] = 'user_images/error'; //error for no image

Controller
Code:
class User_images extends CI_Controller {
  function __construct()
  {
    parent::__construct();
  }

  function error()
  {
    //display error for no image
  }

  function view($image_name = '')
  {
    if ($image_name !== '')
    {
      $image = explode('_', $image_name);
      if (count($image) !== 2)
      {
        //error, couldn't separate user id from image name
      }
      $user_id = $image[0];
      $image_file = $image[1];

      //now read the image from the filesystem, send appropriate image header and output it
    }
  }
}
Something like that...
#3

[eluser]flyenig[/eluser]
thanks for the reply, i forgot to add i have a helper function that will return the images.....

Code:
function get_image($image_id,$type = '')
{
    $sql = "SELECT * FROM photos where photo_id = {$image_id}";
    $query = $this->db->query($sql);
    $r = $query->row();

    $image = base_url().'images/'.$r->dir_1 . '/' . $r->dir_2 . '/' . $r->dir_3 . '/'.$image_id.'_'.$r->img_hash;

    if($type == 'small')
        return $image.'_sm.jpg';
    if($type == 'reg_thumb')
        return $image.'_t.jpg';
    if($type == '' || $type == 'original')
        return $image.'.jpg';

}


so i dont think this would work because i cant call controllers to the view unless i do a "get_instance" function..... because i plan on doing something like this.

Code:
// the helper function returns the images
<img src="&lt;?php echo get_image($profile_id,'small'); ?&gt;" />

this is why i think it would be best to do this with htaccess. any suggestions?
#4

[eluser]CroNiX[/eluser]
The view would have
Code:
<img src="/user_images/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg" alt="" />
or something similar, which would be pulling the image from the controller as well as showing it in the source.
#5

[eluser]flyenig[/eluser]
oh yea... duh lol. didnt get any sleep last night. thank you! Smile
#6

[eluser]flyenig[/eluser]
wait i remember why i thought this would be better in htaccess....
This site will have 1 million plus images, so i was thinking, yeah i can do this thru php but how efficient would this method be? Speed wise?
because that means if i have 50 images on one page then it has to go to the controller function 50 times? it already has to go to the helper function 50 times.
which is more efficient/less resource hog? this php method or doing a rewrite with htaccess?
#7

[eluser]CroNiX[/eluser]
What is the significance of these dirs and is there any logic to them?
/a3c/65c/297/

I don't know how you would translate that unless that is a constant, which I doubt. I assume you have many seemingly random dirs. Are you going to write a rewrite rule for each one? I just don't know how htaccess would be able to translate since those values most likely are stored in a database somewhere or there is some formula for figuring them out.

I'm kind of assuming that once you have a filename like you say you want like:
108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg
That you can somehow look it up to get the actual dir structure that the image is in like:
images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg
#8

[eluser]XMadMax[/eluser]
Try this rewriterule:

RewriteRule ^images/([a-fA-F0-9]+)/([a-fA-F0-9]+)/([a-fA-F0-9]+)/([a-fA-F0-9]+)_([a-fA-F0-9]+)\.(jpg|png|gif)$ user_images/$4/$4_$5.$6 [L]

Allow (images) / (3 subdirs in hexa) / (hexa) + _ + (hexa) + . extension







Theme © iAndrew 2016 - Forum software by © MyBB