我有PHP代碼如下,幫助我在腳本中得到照片的縮略圖路徑我該如何改進這個PHP代碼?
這將需要提供的值這樣從MySQL數據庫「2/34/12/thepicture.jpg」 它將然後把它變成這個「2/34/12/thepicture_thumb1.jpg」
我相信有這樣做的更好的表現方式,我願意接受任何幫助,請
而且與50頁用戶的這將運行50次以獲得50張不同的照片
// the photo has it is pulled from the DB, it has the folders and filename as 1
$photo_url = '2/34/12/thepicture_thumb1.jpg';
//build the full photo filepath
$file = $site_path. 'images/userphoto/' . $photo_url;
// make sure file name is not empty and the file exist
if ($photo_url != '' && file_exists($file)) {
//get file info
$fil_ext1 = pathinfo($file);
$fil_ext = $fil_ext1['extension'];
$fil_explode = '.' . $fil_ext;
$arr = explode($fil_explode, $photo_url);
// add "_thumb" or else "_thumb1" inbetween
// the file name and the file extension 2/45/12/photo.jpg becomes 2/45/12/photo_thumb1.jpg
$pic1 = $arr[0] . "_thumb" . $fil_explode;
//make sure the thumbnail image exist
if (file_exists("images/userphoto/" . $pic1)) {
//retunr the thumbnail image url
$img_name = $pic1;
}
}
1我很好奇的是它如何使用pathinfo()來獲取文件的擴展名,因爲擴展名總是3位數,其他方法會讓這個值的性能更好嗎?
爲什麼使用3位數的擴展名?那麼「thepicture.jpeg」呢?雖然沒有經常使用,但仍然... – 2009-08-17 00:48:39
是的,我想到了後遺症,但是我從來沒有在我的網站上看到100,000多個用戶提交的圖像中的一個,也許他們現在不常見? – JasonDavis 2009-08-17 00:51:44
不要忘記在文件名中放置多個點是有效的。我經常會將文件命名爲:'myLogo.24bit.png','myLogo.bw.png'等 - 此代碼僅在第一個點('$ arr [0]')之前考慮文件名,所以我的文件將會是變成'myLogo.png' – nickf 2009-08-17 01:29:05