2009-06-13 46 views
0

我試圖用圖像縮放玩,我得到了下面的代碼如何使縮略圖進入/ tmp/thumb /並將圖像原始圖像保留在/ tmp /上?

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])){ 
     $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']); 
     move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']); 
     ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/" . $targetfilename, $width, $height); 
    } 

現在,原始圖像和縮略圖將在同一文件夾中放置。

讓我知道..

Source

+0

男人,像這樣的一個標題並不是真正意義上的。你能想到更多與你的問題有關的東西嗎? – 2009-06-13 08:11:53

+0

好吧。讓我知道什麼是最好的標題.. – wow 2009-06-13 08:16:37

回答

2

那麼,答案是:

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])) 
{ 
     $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']); 
     move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']); 
     ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height); 
} 

但是,也許你想了解一點從網上覆制的代碼和過去在使用之前。使用$ _瓦爾沒有逃逸系統和@隱藏的錯誤是不是真的呼喚信任...

編輯:我給意見,但也許這是更好地給一些解釋爲好。

// first you check if the is done uploading in the tmp directory with is tmp name 
if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])) 
{ 
    // here, you rebuild a explicit name using the original filename and a 
    // unique ID to avoid erasing another one 
    $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']); 

    // you rename the file an put it in ./tmp, a subdir of the 
    // script file (because of dirname(__FILE__)) 
    move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']); 

    // Here create a rezided copy 
    // so it's here you can decide to make it go to ./tmp/thumb 
    // make sure the dir exists before because you have no clue here 
    // if ImageHelper will create it for you if not 
    ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/thumb/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height); 
} 
0

嗨,哥們,這看起來很強悍,但可以簡單地使用Thumbnailer庫及其助手上傳進行:

function callback(& $thumb) { 
    $thumb->thumbSquare(100)->save("/tmp/thumb/".$thumb->filename); 
} 

Thumbnailer::upload('ulimage', 'callback'); 

很容易:)

相關問題