1

I store the original image path(included file name) in db when upload an image.

For example:
img/uploaded/photo.jpg

Then I generate its thumbnail and store in below directory NOT in db.

/img/uploaded/thumbs/photo_thumb.jpg

And I have following function but no idea how to get the thumb which belong to the url in db.

//ie: $file is img/uploaded/photo.jpg
public function get_thumb($file)
{
    //get the path without filename
    $get_path = dirname($file) . "/thumbs/";

    //result img/uploaded/thumbs/  (how can i get the photo_thumb.jpg) here?
    return $get_path; 
}

Edit basename($file) to get filename from path but how to add _thumb.jpg?

vzhen
  • 11,137
  • 13
  • 56
  • 87
  • Can you specify the language you're using (add it as a tag perhaps)? I can't make out if this is JavaScript, Perl, PHP, ... and the `dirname` function exists in all three listed. You need a string operation and there are different possiblities for the different languages. – Sander Jan 17 '13 at 15:28
  • This is codeigniter (php). I added php tag. – vzhen Jan 17 '13 at 15:32
  • What are you wanting to do with the thumb when you have it? –  Jan 17 '13 at 15:38
  • @chris to display thumbnail for viewers instead of using large image – vzhen Jan 17 '13 at 15:41
  • so you store the filename in a field in the db yeh? –  Jan 17 '13 at 15:42
  • No, I store the path '/path_to_the_filename/foo.jpg' @Chris – vzhen Jan 17 '13 at 15:44

3 Answers3

2

Don't have much experience with PHP but using this as starting point, I get:

$pattern = '/^.*\/(.*)$/'; // match everything after the last slash and store it in $1
$replacement = '$1';
$filename = preg_replace($pattern, $replacement, $file);

$get_path = $file . '/thumbs/' . $filename;

As I said, not much experience with PHP, but this should do it...

A more easy way to do this, could be:

  1. Find the last / in $file
  2. Insert thumbs/ after it or replace it with /thumbs/
  3. Find the last . in the edited $file
  4. Insert _thumb after it

You can find the postitions of / and . with the strrchr function (documented here).

Sander
  • 3,942
  • 2
  • 17
  • 22
  • I just found there is a basename() which can get the filename from path but I need to add _thumb.jpg after the filename. Example foo_thumb.jpg – vzhen Jan 17 '13 at 15:47
  • @vzhen Strip the extension with [rtrim](http://php.net/manual/en/function.rtrim.php) -> `rtrim($get_path,'.jpg')` (assuming all images are JPG's) and add `_thumb.jpg` – Sander Jan 17 '13 at 16:00
2

You can do this :

    public function get_thumb($file) {

      $infos = pathinfo($file);
      $path = $infos['dirname'] . '/thumbs/' . $infos['filename'] . '_thumb.' . $infos['extension'];
      return $path;

   }    
Jeremie Ges
  • 2,747
  • 3
  • 22
  • 37
0

Usually, the way I do this is, is to store just the filename in the db. (assuming that they are all in the same directory).

Then query the database and get the filename, store it in:

$filename;

Then I just echo out something like

 echo base_url('images/thumbs/' . $filename);

Hope this helps!

Jeremie Ges
  • 2,747
  • 3
  • 22
  • 37