调整图像大小的正确循环过程


Proper foreach loop procedure with resizing image(s)

即使

正在上传多个图像,也只有一个缩略图图像保存到此脚本的文件夹中。我认为这一定与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';
    }
}

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

$original = ($tmp_file); 

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