【问题标题】:Proper foreach loop procedure with resizing image(s)调整图像大小的正确 foreach 循环过程
【发布时间】:2013-10-21 06:07:51
【问题描述】:

即使正在上传多张图片,也只有一张缩略图保存到此脚本的文件夹中。我认为它一定与 foreach 循环的迭代有关。这只是下面发布的调整大小脚本。有没有人有一个快速修复?谢谢

define('THUMBS_DIR', 'C: /inetpub / wwwroot / mysite / images / thumbs / ');
define('MAX_WIDTH', 90);
define('MAX_HEIGHT', 100);
if (array_key_exists('upload', $_POST)) 
{
    $original = $_FILES['image']['tmp_name'];

    foreach ($original as $number => $tmp_file) 
    {
        $original = ($tmp_file);  
    }

    if (!$original) 
    {
        echo 'NoImageSelected'; 
    }
}  else { 
    list($width, $height, $type) = getimagesize($original);

    if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) 
    {
        $ratio = 1; 
    }   elseif ($width > $height) {
        $ratio = MAX_WIDTH/$width;
    } else { 
        $ratio = MAX_HEIGHT/$height; 
    }

    $imagetypes = array(' / . gif$ / ',' / . jpg$ / ',' / . jpeg$ / ',' / . png$ / ');
    $name = preg_replace($imagetypes, '', basename($file[$location]));     
    $moved = @ move_uploaded_file($original[$location], THUMBS_DIR.$file); 

    if ($moved) 
    {
        $result[] = $_FILES['image']['name'].'succesfullyuploaded';
        $original = $ext.$file[$location]; 
    }else{
        $result = 'Problemuploading'.$_FILES['image']['name'];
    }

    if ($type == 1) 
    {
        $source = imagecreatefromgif($original);
    } elseif ($type == 2) {
        $source = imagecreatefromjpeg($original); 
    } elseif ($type == 3) {
        $source = imagecreatefrompng($original);
    } else { 
        $result = 'Cannotidentifythefiletype';
    }
}

【问题讨论】:

  • 计算...需要 $i=0; foreach ($tmp_file as $original){ //代码块 ...else{ $result = '无法识别文件类型'; } $i++; //变量标记为$original[$i]

标签: php image imagecreatefrompng


【解决方案1】:

您能否更详细地描述您希望脚本执行的操作。我认为您的问题可能出在:

$original = ($tmp_file); 

您正在覆盖您在 foreach 循环中使用的变量。

【讨论】:

  • 感谢您的回复。目的是调整从我的 input type="file" input type="upload" 表单上传的任何图像的大小。表单(上面未显示)非常适合单张和多张图片上传。但只有一张调整大小的图像被创建并保存到“缩略图”文件夹中。
  • 想通了...需要 $i=0; foreach ($tmp_file as $original){
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多