Skip to content

Instantly share code, notes, and snippets.

@joostvanveen
Created January 30, 2013 08:18
Show Gist options
  • Save joostvanveen/4671601 to your computer and use it in GitHub Desktop.
Save joostvanveen/4671601 to your computer and use it in GitHub Desktop.
Say I have an image path 'image_path/123.jpg' stored in my database and I need that image's thumbnail, which is in 'image_path/200x200/123.jpg'. Just pass the path and subfolder name to this helper function and it will insert the subfolder in the string.
<?php
function get_thumbnail($img_path, $folder = ''){
// Should we just return the given $img_path?
if(empty($folder) || !strstr($img_path, '/')){
return $img_path;
}
// Insert folder name right before image filename
$img_path = explode('/', $img_path);
$file_name = array_pop($img_path);
array_push($img_path, $folder);
array_push($img_path, $file_name);
return implode('/', $img_path);
}
// Try it out
echo get_thumbnail('/image_path/thumbs/12312.jpg', '200x200') . PHP_EOL;
echo get_thumbnail('/image_path/thumbs/12312.jpg') . PHP_EOL;
echo get_thumbnail('/som/other/path/test.jpg', 'folder/300') . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment