Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP createThumbnail函数疑难解答_Php_Thumbnails_Opendir - Fatal编程技术网

PHP createThumbnail函数疑难解答

PHP createThumbnail函数疑难解答,php,thumbnails,opendir,Php,Thumbnails,Opendir,我正在使用此功能,创建用户上传的图像的缩略图,我在这里找到:: 函数createThumbs($pathToImages、$pathToThumbs、$thumbWidth) { //打开目录 $dir=opendir($pathToImages); //循环浏览,查找任何/所有JPG文件: if(false!=($fname=readdir($dir))){ //解析扩展的路径 $info=pathinfo($pathToImages.$fname); //仅当这是JPEG图像时才继续 如果(

我正在使用此功能,创建用户上传的图像的缩略图,我在这里找到::

函数createThumbs($pathToImages、$pathToThumbs、$thumbWidth)
{
//打开目录
$dir=opendir($pathToImages);
//循环浏览,查找任何/所有JPG文件:
if(false!=($fname=readdir($dir))){
//解析扩展的路径
$info=pathinfo($pathToImages.$fname);
//仅当这是JPEG图像时才继续
如果(strtolower($info['extension'])=='jpg')
{
echo“为{$fname}
创建缩略图”; //加载图像并获取图像大小 $img=imagecreatefromjpeg(“{$pathToImages}{$fname}”); $width=imagesx($img); $height=imagesy($img); //计算缩略图大小 $new_width=$thumbWidth; $new_height=地板($height*($thumbWidth/$width)); //创建新的临时映像 $tmp\u img=ImageCreateTureColor($new\u width,$new\u height); //将旧图像复制并调整大小为新图像 imagecopyresampled($tmp_img,$img,0,0,0,$new_width,$new_height,$width,$height); //将缩略图保存到文件中 图像jpeg($tmp_img,“{$pathToThumbs}{$fname}”); } } //关闭目录 closedir($dir); }
这个函数工作得很好,完全符合我的要求,但是,尽管如此,我仍然会从中得到错误。请参见下面的错误:

警告:opendir(images/008/01/0000288988r.jpg,images/008/01/0000288988r.jpg)[]:目录名无效。(代码:267)

警告:opendir(images/008/01/00002888r.jpg)[]:无法打开dir:无错误

警告:readdir()期望参数1是给定的布尔值资源

我认为问题在于,我正在将一个实际的文件,而不仅仅是一个目录,传递到函数的参数中。这是
$pathtoimages
$pathtothumbs
的情况。该函数应该搜索传递给它的目录,以查找扩展名为
.jpg
的所有图像。但是我只想在上传的时候对上传的一张图片执行这个功能。是否有方法编辑此函数以允许此操作

提前感谢

快脏:

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}" );
}
函数createThumb($pathToImage、$pathToThumb、$thumbWidth)
{
$fname=$pathToImage;
echo“为{$fname}
创建缩略图”; //加载图像并获取图像大小 $img=imagecreatefromjpeg(“{$pathToImage}{$fname}”); $width=imagesx($img); $height=imagesy($img); //计算缩略图大小 $new_width=$thumbWidth; $new_height=地板($height*($thumbWidth/$width)); //创建新的临时映像 $tmp\u img=ImageCreateTureColor($new\u width,$new\u height); //将旧图像复制并调整大小为新图像 imagecopyresampled($tmp_img,$img,0,0,0,$new_width,$new_height,$width,$height); //将缩略图保存到文件中 imagejpeg($tmp_img,“{$pathToThumb}{$fname}”); }
又快又脏:

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}" );
}
函数createThumb($pathToImage、$pathToThumb、$thumbWidth)
{
$fname=$pathToImage;
echo“为{$fname}
创建缩略图”; //加载图像并获取图像大小 $img=imagecreatefromjpeg(“{$pathToImage}{$fname}”); $width=imagesx($img); $height=imagesy($img); //计算缩略图大小 $new_width=$thumbWidth; $new_height=地板($height*($thumbWidth/$width)); //创建新的临时映像 $tmp\u img=ImageCreateTureColor($new\u width,$new\u height); //将旧图像复制并调整大小为新图像 imagecopyresampled($tmp_img,$img,0,0,0,$new_width,$new_height,$width,$height); //将缩略图保存到文件中 imagejpeg($tmp_img,“{$pathToThumb}{$fname}”); }
您必须像这样使用该功能:

createThumbs("path_to_image", "path_to_thumb", "thumb_width");
替换参数。请注意单词“path”,它是一个目录,如“./images/02”,您可能正在使用路径和图片名称,如下所示:

createThumbs("images/008/01/0000288988r.jpg", " ......
它应该是:

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

您必须像这样使用该函数:

createThumbs("path_to_image", "path_to_thumb", "thumb_width");
替换参数。请注意单词“path”,它是一个目录,如“./images/02”,您可能正在使用路径和图片名称,如下所示:

createThumbs("images/008/01/0000288988r.jpg", " ......
它应该是:

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

$pathToImage必须指向图像文件
删除
$dir=opendir($pathToImages)
if(false!=($fname=readdir($dir)){

//解析扩展的路径

$info=pathinfo($pathToImages.$fname);

为文件名添加
$info=pathinfo($pathtoImages);//文件名

$fname=$info['filename']

仅将
{$pathToImages}{$fname}
替换为
$pathToImages
,因为它是图像文件


顺便说一句,这段代码并不冗长。

$pathToImage必须指向图像文件
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}" );

}
删除
$dir=opendir($pathToImages);

if(false!=($fname=readdir($dir)){

//解析扩展的路径

$info=pathinfo($pathToImages.$fname);

为文件名添加
$info=pathinfo($pathtoImages);//文件名

$fname=$info['filename']

仅将
{$pathToImages}{$fname}
替换为
$pathToImages
,因为它是图像文件

顺便说一句,这段代码并不冗长

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看起来您的解决方案可能有效,但我的解决方案也有效,所以我没有测试它

我想我提前发布了这个问题。谢谢大家的帮助


@csw看起来您的解决方案可能有效,但我的解决方案也有效,因此我没有对其进行测试。

这不起作用。现在不再创建更多错误的缩略图:(我找到了。再次感谢您的帮助和检查,这不起作用。现在不再创建更多错误的缩略图:(我找到了。)