2

i have a php script that uploads an image, renames it to a random hash, creates folders based on the hash, stores the original image in the last created folder, and finally, creates 2 other thumbnails for it...

regular image... images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg

thumbnail image... images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_t.jpg

smaller thumbnail... images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_sm.jpg

For each photo that is uploaded, it will store a new record in the DB which contains the photo id and this information

/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b

(notice how i didnt store the ".jpg" or "_t.jpg" etc)

Now in order for me to serve the images(i dont know if this is a good practice), Currently i have this php function that returns the image...

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';

}

basically, i want to do something like twitter where the picture urls look like this

https://twimg0-a.akamaihd.net/profile_images/2392722244/blah.jpg

QUESTIONS...

  1. how can i achieve this type of url structure? Htaccess?
  2. Am I returning the images the right way?
  3. is what im doing safe, smart, and a good practice? if not is there a better method?

thanks!!

Kenny
  • 675
  • 1
  • 7
  • 20

1 Answers1

1

IMO, You can definitely do that with url rewrite indeed. In /etc/apache2/sites-available/yoursite ,

    <Directory />

            RewriteEngine On
            RewriteCond %{HTTP_REFERER}          profile_images/(\w+)/(\w+|.)+$


            RewriteRule ^/profile_images/(\w+)/         index.php?image_id=$1 [L,QSA]

            Options Indexes FollowSymLinks MultiViews
            AllowOverride None

            Order allow,deny
            allow from all
    </Directory>

Try to play around that. I think it is pretty similar directives in htaccess.

Am I returning the images the right way?

What do you mean by that ? Your "return" will work. I would have used a switch/case statement instead of the ifs though.

is what im doing safe, smart, and a good practice? if not is there a better method?

Safe : you should escape your input. But if you redirect only for which the image name is a alphanumeric it shouldn t be a problem

I don't know why you break down into so many directories. One directory per image+thumbnails would be sufficient. Don't worry about indexing with a b-tree or anything like that, using index only in your db would be sufficient.

Last but not least, I would only use apache's rewrite rule to accomplish that, if I don't need to have a link between the user and the image.

RewriteRule ^/profile_images/(\w+)(_\w+)?.(jpg|png)/ images/$1/$1$2.$3

It doesn't involve php nor a db and is done in one line.

https://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

I am not sure if that helps, but you might have learned something. and at least, you have another point of view :)

Community
  • 1
  • 1
Cedric
  • 5,135
  • 11
  • 42
  • 61
  • THanks for the answer, i forgot to mention.. my reason for the number of directories is due to the fact that im building a large scale web application... we are looking at millions of photos being uploaded so i need the images to be spread evenly. Im going to do the HTaccess and im going to change that function to switch case. thanks again! – Kenny Sep 17 '12 at 18:48
  • for the htaccess above, where do i put that? on the main root of the site or in the images folder? Sorry i have no skills with htaccess – Kenny Sep 17 '12 at 19:00
  • Modify the /etc/apache2/sites-available/yoursite file and reload apache. (although these lines could probably be part of .htaccess) – Cedric Sep 18 '12 at 13:53
  • What do you mean by spread evenly ? Is it on different hard drives ? – Cedric Sep 18 '12 at 13:54