imagecreatefromjpeg生成相同的文件(多个图像)

时间:2015-01-04 23:55:41

标签: php jquery ajax

我试图将一个formData对象从jquery(ajax)发送到php。 formData包含图像文件。我需要它们上传和调整大小(两者都曾用过)。现在的问题是,当我在php中创建一个imagecreatefromjpeg对象时,似乎他只拍摄了第一张图像并将其用于formdata中的图像数量。

foreach($_FILES as $file)
{
    $ImageName      = $file['name'];
    $ImageSize      = $file['size'];
    $TempSrc        = $file['tmp_name'];
    $ImageType      = $file['type'];

    $DestinationDirectory .= $CategoryName . "/";

    if (is_array($ImageName)) 
    {
        $c = count($ImageName);

        for ($i=0; $i < $c; $i++)
        {
            $processImage           = true; 
            $RandomNumber           = rand(0, 9999999999);  // We need same random name for both files.

            if(!isset($ImageName[$i]) || !is_uploaded_file($TempSrc[$i]))
            {
                echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName[$i].'</strong>, may be file too big!</div>'; //output error
            }
            else
            {
                //Validate file + create image from uploaded file.
                switch(strtolower($ImageType[$i]))
                {
                    case 'image/png':
                        $CreatedImage = imagecreatefrompng($TempSrc[$i]);
                        break;
                    case 'image/gif':
                        $CreatedImage = imagecreatefromgif($TempSrc[$i]);
                        break;
                    case 'image/jpeg':
                    case 'image/pjpeg':
                        $CreatedImage = imagecreatefromjpeg($TempSrc[$i]);
                        break;
                    default:
                        $processImage = false; //image format is not supported!
                }           //get Image Size
                    /*
                    $outputArr[] = array('image' => $ImageName[$i]);
                    header('Content-Type: application/json');
                    echo json_encode($outputArr);
                    */

                    echo $ImageName[$i];
            }
        }
    }
}

因此,这与同一图像的$i次相呼应。一旦我取下开关盒,它就会回显正确的图像......

0 个答案:

没有答案