Is There a Way to Automatically Reduce the Size of an Image During the Upload Process?

Last post 07-30-2011, 10:42 AM by LenJacobson. 2 replies.
Sort Posts: Previous Next
  •  07-27-2011, 9:06 AM 68917

    Is There a Way to Automatically Reduce the Size of an Image During the Upload Process?

    I am using the PHP File Uploader to upload pictures.  I would like to know if there is a way to reduce the size of the pictures automatically while they are being uploaded.
     
    I, of course, realize that I could use something like Adobe Photoshop to reduce the sizes before uploading them; I would like it if the PHP File Uploader could do that during the upload process.
     
    Thank you.
  •  07-29-2011, 6:55 AM 69002 in reply to 68917

    Re: Is There a Way to Automatically Reduce the Size of an Image During the Upload Process?

    Hi LenJacobson,
     
    For now, can not achieve it at the uploading progress, you should change the size after upload success by other code.
     
    The example below shows you how to create a  thumbnail in the save folder after uploaded. hope it help.
     
    <?php require_once "phpuploader/include_phpuploader.php" ?>
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>
    example
        </title>
    </head>
    <body>
     
                <form id="form1" method="POST">
                    <?php                
                        $uploader=new PhpUploader();
                        $uploader->MaxSizeKB=10240;
                        $uploader->Name="myuploader";
                        $uploader->InsertText="Select multiple files (Max 10M)";
                        $uploader->AllowedFileExtensions="*.jpg,*.png,*.gif";    
                        $uploader->MultipleFilesUpload=true;
                        $uploader->Render();
                        
                        
                        
                function PhpGallery_Impl_LoadImage($imagefile)
                    {
                    $ext=pathinfo($imagefile,PATHINFO_EXTENSION);
                    switch(strtolower($ext))
                    {
                    case "png":
                    return imagecreatefrompng($imagefile);
                    case "gif":
                    return imagecreatefromgif($imagefile);
                    case "jpg":
                    case "jpeg":
                    default:
                    return imagecreatefromjpeg($imagefile);
                    }
                    }
                function PhpGallery_GenerateThumbnail($imagefile,$thumbpath,$width,$height)
                {
                $img=PhpGallery_Impl_LoadImage($imagefile);
                $thumb=imagecreatetruecolor($width,$height);
                imagecopyresized($thumb,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
                imagejpeg($thumb,$thumbpath);
                imagedestroy($img);
                imagedestroy($thumb);
                }
                ?>
        </form>
                
            
    <?php
    $fileguidlist=@$_POST["myuploader"];
    if($fileguidlist)
    {
    $guidlist=split("/",$fileguidlist);     
    $path="D:/2005/phpuploader/Photos/";
        foreach($guidlist as $fileguid)    
        {    
        
            $mvcfile=$uploader->GetUploadedFile($fileguid);       
            if($mvcfile)       
            {       
            $mvcfile->MoveTo($path.$mvcfile->FileName);
            PhpGallery_GenerateThumbnail($path.$mvcfile->FileName,$path."thumbnail".$mvcfile->FileName,128,128);
            }    
        }    
        }
    ?>
                    
        </div>
    </body>
    </html>
     
     
    Regards,
     
    ken
  •  07-30-2011, 10:42 AM 69050 in reply to 69002

    Re: Is There a Way to Automatically Reduce the Size of an Image During the Upload Process?

    Ken,
     
    It still seems easier to use Adobe Photoshop BEFORE uploading the images to reduce their size, and then upload the smaller images.
     
    But, thank you, Ken, for this response.  I will hang onto this code to learn from it and quite likely use it in the future.
View as RSS news feed in XML