我正在使用这个函数来创建用户上传的图像的缩略图,我在这里找到了: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
扩展名的图像。但我想只对上传时上传的一张图片执行该功能。有没有办法编辑这个功能来允许这个?
提前致谢