2013-03-21 110 views
1

我使用這個功能,創造由用戶上傳的圖像的縮略圖,我發現在這裏:http://webcheatsheet.com/php/create_thumbnail_images.phpPHP createThumbnail功能故障排除

function createThumbs($pathToImages, $pathToThumbs, $thumbWidth) 
{ 
    // open the directory 
    $dir = opendir($pathToImages); 

// loop through it, looking for any/all JPG files: 
if (false !== ($fname = readdir($dir))) { 
    // parse path for the extension 
    $info = pathinfo($pathToImages . $fname); 

// continue only if this is a JPEG image 
if (strtolower($info['extension']) == 'jpg') 
{ 
    echo "Creating thumbnail for {$fname} <br />"; 

    // load image and get image size 
    $img = imagecreatefromjpeg("{$pathToImages}{$fname}"); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    $new_height = floor($height * ($thumbWidth/$width)); 

    // create a new temporary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 
    imagejpeg($tmp_img, "{$pathToThumbs}{$fname}"); 
} 
} 
// close the directory 
closedir($dir); 
} 

此功能工作正常,不正是我希望它,但儘管如此,我仍然從中得到了錯誤。請參閱下面的錯誤:

Warning: opendir(images/008/01/0000288988r.jpg,images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: The directory name is invalid. (code: 267)

Warning: opendir(images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: failed to open dir: No error

Warning: readdir() expects parameter 1 to be resource, boolean given

的問題,我想,是我傳遞一個實際的文件,而不只是一個目錄中,將參數的函數。這是$pathtoimages$pathtothumbs的情況。該功能應該搜索傳遞給它的目錄以查找擴展名爲.jpg的所有圖像。但我只想在上傳時上傳的一張圖片上執行該功能。有沒有辦法編輯這個功能來允許這個?

在此先感謝

回答

1
function createThumbs($pathToImages, $pathToThumbs, $thumbWidth) 
{ 

// load image and get image size 
$img = imagecreatefromjpeg("{$pathToImages}"); 
$width = imagesx($img); 
$height = imagesy($img); 

// calculate thumbnail size 
$new_width = $thumbWidth; 
$new_height = floor($height * ($thumbWidth/$width)); 

// create a new temporary image 
$tmp_img = imagecreatetruecolor($new_width, $new_height); 

// copy and resize old image into new image 
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// save thumbnail into a file 
imagejpeg($tmp_img, "{$pathToThumbs}"); 

} 

想我過早地發佈了這個問題。感謝大家的幫助。

@csw看起來像你的解決方案可能工作,但我得到了我的工作,所以我沒有測試它。

0

快速和骯髒的:

function createThumb($pathToImage, $pathToThumb, $thumbWidth) 
{ 
    $fname = $pathToImage; 
    echo "Creating thumbnail for {$fname} <br />"; 

    // load image and get image size 
    $img = imagecreatefromjpeg("{$pathToImage}{$fname}"); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    $new_height = floor($height * ($thumbWidth/$width)); 

    // create a new temporary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 
    imagejpeg($tmp_img, "{$pathToThumb}{$fname}"); 
} 
+0

這沒有奏效。更多錯誤現在和現在縮略圖不再被創建:( – reubenCanowski 2013-03-21 15:50:17

+0

哪些錯誤?將它們粘貼在這裏 – 2013-03-21 16:04:44

+0

我想通了,再次感謝幫助和檢查 – reubenCanowski 2013-03-21 16:08:44

-1

你必須使用像這樣的功能:

createThumbs("path_to_image", "path_to_thumb", "thumb_width"); 

更換參數。注意單詞「路徑」,這是一個目錄,如」 ../images/02" ,並且您正在使用可能的路徑與圖片名一起,就像這樣:

createThumbs("images/008/01/0000288988r.jpg", " ...... 

它應該是:

createThumbs("images/008/01/" ... 
1

的$映像路徑必須指向圖像文件
刪除
$dir = opendir($pathToImages);
if (false !== ($fname = readdir($dir))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);

添加$info = pathinfo($pathtoImages); // for the file name
$fname = $info['filename']

只有$pathToImages取代{$pathToImages}{$fname},因爲它的圖像文件。

順便說一句,這段代碼並不冗長。