【问题标题】:Efficient and fast image uploading高效快速的图片上传
【发布时间】:2014-12-12 12:01:05
【问题描述】:

我一直在玩一个新网站,它允许会员上传一些内容和图片。我已经敲定了代码 html、php 和 mysql(在 google 的帮助下),现在一切正常,但速度不快。 可能是我的代码效率低下,或者我的托管公司限制了上传速度……或者两者兼而有之。我是开发网站的新手!

使用

在我开始考虑实施 DropBox 来存储图像之前……有没有人遇到过这个问题,可以推荐一种不同的方法?

代码html sn-p:

<form name="add-form" action="includes/new_post.php" method="post" enctype="multipart/form-data">
    <input id='id_question_pic' name="upfile1[]" type="file" tabindex="3" multiple accept='image/*' max-uploads=6 />

php 中的代码:

//put all the uploaded images into an array 
    $files=array();
    $fdata=$_FILES['upfile1'];
    for($i=0;$i<count($fdata['name']);++$i){
        $files[]=array(
         'name'    =>$fdata['name'][$i],
         'type'  => $fdata['type'][$i],
         'tmp_name'=>$fdata['tmp_name'][$i],
         'error' => $fdata['error'][$i], 
         'size'  => $fdata['size'][$i]  
        );
    }

     //move to the correct directory, with unique file names
     $directory = 'C:\Inetpub\vhosts\mydomain\form_uploads\\'; //use local server path (hosting company)
     $dbimagepath = 'form_uploads/';
     $result = true;
     foreach ($files as $file) {
        if ($file['error'] == 0) {
            $filename = $file['name'];
            if (strlen($filename) > 20) {$filename = substr($filename, strlen($filename) - 8);}
            $filename = mt_rand() . '_' . $filename;

            //ensure the filename is unique//
            while (@getimagesize($directory . $filename)){$filename = mt_rand() . $filename;}

            $fullpath = $directory . $filename;
            if (exif_imagetype($file['tmp_name'])== 2){
                $image = imagecreatefromstring(file_get_contents($file['tmp_name']));

                //ORIGINAL DIMENTIONS
                list( $width , $height ) = getimagesize($file['tmp_name']);

                //ORIGINAL SCALE
                $xscale=$width/600;
                $yscale=$height/600;

                //NEW DIMENSIONS WITH SAME SCALE
                if ($yscale > $xscale)
                {
                    $new_width = round($width * (1/$yscale));
                    $new_height = round($height * (1/$yscale));
                }
                else
                {
                    $new_width = round($width * (1/$xscale));
                    $new_height = round($height * (1/$xscale));
                }

                //NEW IMAGE RESOURCE
                if(!($imageResized = imagecreatetruecolor($new_width, $new_height)))
                {//error handling}

                //RESIZE IMAGE
                if(! imagecopyresampled($imageResized, $image , 0 , 0 , 0 , 0 , $new_width , $new_height , $width , $height))
                {//error handling}
                $image = $imageResized;


                $exif = exif_read_data($file['tmp_name']);
                if (!empty($exif['Orientation']) || !$exif['Orientation']===null){
                    switch($exif['Orientation'])
                    {
                        case 3: // 180 rotate left
                            $image=imagerotate($image, 180, -1);
                            break;
                        case 6: // 90 rotate right
                            $image=imagerotate($image, -90, -1);
                            break;
                        case 8:    // 90 rotate left
                            $image=imagerotate($image, 90, -1);
                            break;
                        default:
                            break;
                    }
                }

                if (imagejpeg($image, $fullpath, 80)){
                    //call function to update the database
                }

                imagedestroy($image);
            }
            else
            {$result = false;}
        }
    } 

【问题讨论】:

    标签: php file-upload upload


    【解决方案1】:

    查看为解决您的特定问题而构建的第 3 方解决方案。 以Uploadcare 为例。

    有了它,即使没有访问主机的文件系统,您也可以进行快速可靠的上传,上传速度仅受访问者的互联网连接限制。

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 1970-01-01
      • 2014-08-22
      • 2017-05-20
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多