1

My question is quick and simple.

I am creating a gallery and I am fetching my full-size images and my thumbnails from a directory using glob.

I have a variable for the path to my thumbnail images : images/photo-strip/thumb/*.jpg

I am reducing the path images/photo-strip/thumb/*.jpg to *.jpg using basename() and I save that to $filename

Then I re-build the path to the full size image like this: $pathToFull = 'images/photo-strip/' . $filename;

My files inside the thumb folder are named like so: th_*.jpg and the full size images are *.jpg (exactly matching but without the th).

I then have an echo that puts the HTML, with thumbnail path in an <img> tag and fullsize path to an <a> tag.

Problem is the path to my full-size images is wrong because of the th_ in the filenames of the images.

Is there a way to trim the th_ from $filename and save it to something like $full_filename so I can correct the filename for the full-size images?

I could easily rename the thumbnail images to exclude th_ and this would solve my problem but I would prefer to leave the file names as is.

EDIT:

I am using BCMCFC's method as it get's rid of two lines of code that I don't neccessarily have to have.

Thanks for the speedy answers!

Michael
  • 7,016
  • 2
  • 28
  • 41

6 Answers6

1

Just use the str_replace function to replace "th_" by empty string

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • This is what I ended up using but to some further extent as shown in bcmcfc's answer. +1 and thanks! – Michael Apr 02 '13 at 15:57
1

Look no further than $full_filename = substr($filename,3);

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Great answer and it works but I like bcmcfc's answer better. It allows me to cut out two lines of code. Thanks though!! – Michael Apr 02 '13 at 15:54
1

Instead of rebuilding the path, it sounds like you could simplify the process as follows:

$img = str_replace('images/photo-strip/thumb/th_', 'images/photo-strip/', $originalImage);

Or just:

$img = str_replace('thumb/th_', '', $originalImage);
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • This is very nice, it allows me to skip using basename to find the filename and allows me to cut out the line that I had "rebuilding" the filepath. – Michael Apr 02 '13 at 15:52
1

This would be the most elegant way to do it:-

$filename = 'th_myimage.jpg';
$pathToFull = 'images/photo-strip/' . preg_replace('/^(th_)?/', '', $filename);
// $filename would be 'myimage.jpg'

This regular expression pattern would replace only the first occurrence of th_ and if it was at the start of the string.

Suleman C
  • 783
  • 4
  • 17
  • +1 for a very good answer. I am using bcmcfc's answer as it cuts down on the amount of code needed to achieve what I want. I would use this if it weren't for not being worried about other occurences of th_ later in the filename. I very specific with my naming conventions so there is no need to be worried about that happening. Thanks! – Michael Apr 02 '13 at 15:56
0

This has an implementation for batch renaming files in a directory.

Bulk Rename Files in a Folder - PHP

Community
  • 1
  • 1
wazy
  • 1,065
  • 1
  • 10
  • 23
0

You could use "explode" to split the string on the "_" character to an array, then get the latter element of the array.

$myarray = explode("_", $filename);
if(count($myarray)>1){
    $full_filename = $myarray[1];
}
Crwydryn
  • 840
  • 6
  • 13