PHP createThumbnail功能故障排除

时间:2013-03-21 15:29:58

标签: php thumbnails opendir

我正在使用此功能来创建用户上传的图片缩略图,我在此处找到:http://webcheatsheet.com/php/create_thumbnail_images.php

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扩展名的所有图像。但我想在上传时上传的一张图片上执行该功能。是否有人编辑此功能以允许这个?

提前致谢

4 个答案:

答案 0 :(得分:1)

$ pathToImage必须指向图像文件
除去
    $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}{$fname}替换为$pathToImages,因为它是图像文件。

不过,这段代码并不详细。

答案 1 :(得分: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看起来你的解决方案可能有效,但我的工作也是如此,所以我没有测试它。

答案 2 :(得分: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}" );
}

答案 3 :(得分:-1)

你必须使用这样的功能:

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

替换参数。注意单词“path”,它是一个目录,比如“../ images/02”,你可能正在使用路径和图片名称,如下所示:

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

它应该是:

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