user3209603
user3209603

Reputation: 175

Efficient and fast image uploading

I've been playing around with a new website that will allow members to upload some content and images. I've knocked up the code html, php & mysql (with the help of google) and it now all works but not fast. It could be my code is inefficient or my hosting company is restricting the uploading speed ... or both. I am very new to developing websites!

Using small size images <300kb is not an issue, but as soon as I use 6mb ones its can take up to 5minutes to upload the images even though I am reducing there size before upload. I've tested without database inserts so I know its the images part thats causing the issue.

Before I start looking at implementing DropBox to store the images ... has anyone come across this problem before that could recommend a different approach?

Code html snippet:

<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 />

Code in 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;}
        }
    } 

Upvotes: 1

Views: 2138

Answers (1)

Dmitry Mukhin
Dmitry Mukhin

Reputation: 6947

Take a look at 3rd party solutions that are build to solve your particular problem. Uploadcare for example.

With it you can have fast reliable uploads even without access to your host's filesystem and upload speed is limited only by your visitor's internet connectivity.

Upvotes: 1

Related Questions