图像上传器使用增量图像名称

时间:2014-12-04 08:19:32

标签: php

我正在使用php多图像上传器 - 代码如下。我正在尝试修改它以将增量图像名称分配给批量图像,例如。 Image1.jpg,Image2.jpg,Image3.jpg等,而不是随机名称。我已经尝试将随机代码更改为变量但没有运气。任何人都可以帮忙。

$ThumbSquareSize        = 125; //Thumbnail will be 200x200
$BigImageMaxSize        = 600; //Image Maximum height or width
$ThumbPrefix            = "thumb_"; //Normal thumb Prefix
$Reference_No           = $_POST['Reference_No'];
$DestinationDirectory   = 'properties/'.$Reference_No.'/'; //Upload Directory ends with / (slash)
$Quality                = 75;

//ini_set('memory_limit', '-1'); // maximum memory!

foreach($_FILES as $file)
{
// some information about image we need later.
$ImageName      = $file['name'];
$ImageSize      = $file['size'];
$TempSrc        = $file['tmp_name'];
$ImageType      = $file['type'];


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

    echo  '<ul>';

    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
            list($CurWidth,$CurHeight)=getimagesize($TempSrc[$i]);

            //Get file extension from Image name, this will be re-added after random name
            $ImageExt = substr($ImageName[$i], strrpos($ImageName[$i], '.'));
            $ImageExt = str_replace('.','',$ImageExt);

            //Construct a new image name (with random number added) for our new image.
            $NewImageName = $RandomNumber.'.'.$ImageExt;

            //Set the Destination Image path with Random Name
            $thumb_DestRandImageName    = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
            $DestRandImageName          = $DestinationDirectory.$NewImageName; //Name for Big Image

            //Resize image to our Specified Size by calling resizeImage function.
            if($processImage && resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
            {
                //Create a square Thumbnail right after, this time we are using cropImage() function
                if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
                    {
                        echo 'Error Creating thumbnail';
                    }
                    /*
                    At this point we have succesfully resized and created thumbnail image
                    We can render image to user's browser or store information in the database
                    For demo, we are going to output results on browser.
                    */

                    //Get New Image Size
                    list($ResizedWidth,$ResizedHeight)=getimagesize($DestRandImageName);

$DestinationDirectory   = 'properties/'.$Reference_No;

                    echo '<li><table width="100%" border="0" cellpadding="4" cellspacing="0">';
                    echo '<tr>';
                    echo '<td align="center"><img src="'.$DestinationDirectory.$ThumbPrefix.$NewImageName.'" alt="Thumbnail" height="'.$ThumbSquareSize.'" width="'.$ThumbSquareSize.'"></td>';
                    echo '</tr><tr>';
                    echo '<td align="center"><img src="'.$DestinationDirectory.$NewImageName.'" alt="Resized Image" height="'.$ResizedHeight.'" width="'.$ResizedWidth.'"></td>';
                    echo '</tr>';
                    echo '</table></li>';
                    /*
                    // Insert info into database table!
                    mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
                    VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
                    */

            }else{
                echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName[$i].'</strong>! Please check if file is supported</div>'; //output error
            }

        }

    }
    echo '</ul>';
    }
}

2 个答案:

答案 0 :(得分:0)

你有两个选择

  1. 将号码保存到文件并下次读取文件并添加到文件中
  2. 保留数据库表中的文件列表并获取最后的文件索引并添加到​​其中

答案 1 :(得分:0)

你已经有一个for循环递增整数变量$ i,所以改变这个:

//Construct a new image name (with random number added) for our new image.
$NewImageName = $RandomNumber.'.'.$ImageExt;

到以下代码:

//Construct a new image name (with random number added) for our new image.
$NewImageName = 'Image'.$i.'.'.$ImageExt;