2014-03-06 123 views
0

我是OOP和I'm using this tutorial to resize images的新手,但我無法實現它。我使用DropzoneJS上傳圖片。在OOP中使用變量PHP函數

我的代碼如下所示:

include_once '../core/init.php'; 
$username = $user['username']; 

$ds   = DIRECTORY_SEPARATOR; 

$storeFolder = '../users'; 

if (!empty($_FILES)) { 

    $tempFile = $_FILES['file']['tmp_name'];   

    $targetPath = dirname(__FILE__) . $ds . $storeFolder . $ds . $username . $ds; 

    $tempName = $_FILES['file']['name']; 
    $kaboom = explode(".", $tempName); 
    $fileExt = end($kaboom); 
    $filename = date("DMj-Y-G-i-s-")."".rand(1000,9999).".".$fileExt; 

    $targetFile = $targetPath. $filename; 

    move_uploaded_file($tempFile,$targetFile); 

    include("image_resize.php"); 
    $resizeObj = new resize($targetPath . $targetFile); 
    $resizeObj -> resizeImage(550, 550, 'crop'); 
    $resizeObj -> saveImage($targetPath.'medium-'.$targetFile, 100); 

    $users->upload_image($username, $filename, $time, $ip); 

} 

什麼工作:圖像上傳成功並移動到正確的目錄。 $user->upload_image工作並將新的圖像數據插入到我的數據庫中。

什麼不起作用:image_resize不起作用。教程中的演示效果很好,但是當我更改sample.jpg時,我的targetPathtargetFile沒有任何反應。我試圖創建一個550x550像素的重複圖像,在文件名之前添加了medium-,但沒有任何事情發生。

+0

你知道,如果它使用類似於ImageMagick的作爲,你可能沒有在您的環境中安裝的依賴? –

+0

本教程中的示例有效,因此我知道resize類可以工作。我使用它有什麼問題。 – user2250471

+0

'$ users-> upload_image' - 其中'$ users'對象是實例化的嗎? –

回答

1

你有一些不一致的文件路徑,在這裏你前面加上$targetPath$targetFile

$targetFile = $targetPath. $filename; 

然後上載使用$targetFile文件 - 這已經包含$targetPath

move_uploaded_file($tempFile,$targetFile); 

那麼當你嘗試調整它的大小時,你需要再次預先設置$targetPath變量

$resizeObj = new resize($targetPath . $targetFile); 

嘗試該行沒有$targetPath

$resizeObj = new resize($targetFile); 

編輯:注意到你再次使用$targetPath保存縮略圖的時候,所以在這種情況下,你會真正需要保持這兩個變量分開,只在必要時組合它們 - 因爲當你保存時,你把$targetPath,然後medium-然後文件名已經包含$targetPath

保持的路徑和文件名獨立,並嘗試這個辦法:

move_uploaded_file($tempFile, $targetPath . $filename); 

include("image_resize.php"); 
$resizeObj = new resize($targetPath . $filename); 
$resizeObj -> resizeImage(550, 550, 'crop'); 
$resizeObj -> saveImage($targetPath . 'medium-' . $filename, 100);