vzhen
vzhen

Reputation: 11157

Get thumbnail image from the original image path

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?

Upvotes: 1

Views: 1747

Answers (3)

user1157393
user1157393

Reputation:

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!

Upvotes: 0

Jeremie Ges
Jeremie Ges

Reputation: 2745

You can do this :

    public function get_thumb($file) {

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

   }    

Upvotes: 2

Sander
Sander

Reputation: 4042

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).

Upvotes: 2

Related Questions