【问题标题】:resize multiple images while uploading上传时调整多张图片的大小
【发布时间】:2014-04-29 21:29:56
【问题描述】:

嗨,我在上传时尝试调整多张图片的大小,我有调整大小的功能,但它只适用于一张图片,所以请任何人告诉我如何循环播放多个文件上传

if( $_FILES['image']['size']< $max_file_size ){
    // get file extension
    $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
//  $ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
    if (in_array($ext, $valid_exts)) {
        /* resize image */

        foreach ($sizes as $w => $h) {

            $files[] = resize($w, $h);

                        }

    } else {
        $msg = 'Unsupported file';
    }
} else{
    $msg = 'Please upload image smaller than 200KB';
}

【问题讨论】:

  • 看看 $_FILES 数组的实际样子。 $_FILES['image']['size'][0] 将是第一个图像。增加 0。我没有看到很难给出一个好的答案的循环。
  • 这就是我得到这个的地方,如果你想看看,谢谢source.w3bees.com/resize-image-while-uploading.html

标签: php image upload resize


【解决方案1】:

这是未经测试的,但可能会给你一个想法。 它的工作方式是循环遍历变量$iname 的所有$_FILES,即'image'。我设置了这个,因为它被多次使用,所以如果你改变它,它会更容易。

我创建了一个名为$image 的新变量,它将作为该特定图像的变量。我通过循环遍历$_FILES[$iname] 的所有变量来做到这一点。我将$image 变量设置为$key 和新值,这将是一个数组。我们使用$i 变量引用了正确的数组。

接下来我只是使用您现有的代码。由于resize() 函数只需要宽度和高度,我不确定这里会发生什么。应该传递另一个参数来引用您要调整大小的图像,它将是$image

从我输入的可见代码来看,不知道resize() 是什么,这段代码是不安全的。您应该检查的不仅仅是文件扩展名,因为它很容易更改。我通常使用 exif 来检查图像标题。我也从不存储用户上传的数据,除非我使用 PHP 中的 GDI 函数对其进行重新编码。

希望这能让你开始。

$i = 0;
$iname = 'image';
for($i = 0; $i < count($_FILES[$iname]['size']); $i++) {

   // Create new Image Array
   $image = array();
   foreach($_FILES[$iname] as $key => $val) {
     $image[$key] = $val[$i];
   }


   if( $image['size'] < $max_file_size ) {
      $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
      if (in_array($ext, $valid_exts)) {
         foreach ($sizes as $w => $h) {
            // How is resize getting the $_FILES?
            // Should pass a variable of $image and use it instead
            $files[] = resize($w, $h);
         }
      } else {
        $msg = 'Unsupported file';
       }
    } else{
       $msg = 'Please upload image smaller than 200KB';
    }
}

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 2012-05-28
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2014-08-26
    • 2011-05-28
    相关资源
    最近更新 更多