tags:

views:

78

answers:

3

This PHP code below will take an image and add the word _thumb in between the filename and file extension

This file is called up to 50 times on some pages to show user photo, please not I only linked to a full path image on another server for this example, the file path is usually pulled from a Mysql DB that stores the path to a users photo

I am currently restructuring a lot of things on my site though and I am wanting to know if this is a bad way?

I store a thumbnail image like this:
filename_thumb.jpg

So on a page to view a thumbnail, I only have the path to the full size image and I must add the _thumb part into the filename before displaying the image to screen.

Would it be better performance to do something like
t_filename.jpg

The data that come from the database is like this:
user/photos/1/23/45/34/filename.jpg

So if I changed where I add the thumb part to the fron of the name would it still have to run all the code below to get a result? Please notice how the path I get from the DB is not JUST a filename it has the folder structure saved into it as well and the folders are always different.

<?php

$file = 'http://file-managers.net/skins/blue/images/actions/view_tree.png';
$fil_ext1 = pathinfo($file);
$fil_ext = $fil_ext1['extension']; 
$fil_explode = '.' . $fil_ext;
$arr = explode($fil_explode, '/skins/blue/images/actions/view_tree.png');
$pic1 = $arr[0] . "_thumb" . $fil_explode;

// will show;
// /skins/blue/images/actions/view_tree_thumb1.png
echo $pic1;

?>
A: 

If you are worried about processing time why not store two different paths in the database or store the file extension and file path without the extension in the database. This way you can do the processing once when adding to the database. Personally I like the second method.

MitMaro
your second method might be better since I have multiple different thumnail sizes for each image also
jasondavis
A: 

I don't think the simple string operations you're doing will add a ton of overhead even you do them 50 times. The way you're doing it seems reasonable to me.

If your PHP is 5.2 or above you can skip all that business with explode and get the file name with:

$fil_ext1['filename']

instead.

Robert
+1  A: 

I'd recommend a folder structure like this:

images/
    originals/
    small/
    middle/
    big/

Every image gets uploaded into images/originals/, smaller versions will be created in the respective folders (add as needed). In the database you only store the original* filename.

*) Note that you should create a new random filename, don't use the actual original user-supplied filename.

When including images you simply do

echo "images/small/".$filename;  // (pseudocode)

If you ever want to change your design and need to resize images, it's as simple as cleaning out the small/, middle/ etc folders and re-creating them with a small script, or even on-the-fly with something like:

// pseudocode
if (!file_exists('images/small/'.$filename)) {
    create_resized_image($filename, 'small');  // looks for "images/originals/$filename"
}
echo "images/small/".$filename;

You'll need to see for yourself if/how you can map this to your "user/photos/1/23/45/34/filename.jpg" scheme.

deceze
yeah I thought about this but seeing it wrote down it looks even better, thanks
jasondavis