0

I am trying to add a function to reduce filesize when an image is uploaded, I have the code to upload the image and it works fine but am new to this all together.. Would I use imagejpeg for this or is there a better (more simple) way? I have not included my attempts as they have failed so here is the working standard upload script using a basic submit file input..

$file_formats = array("jpg","jpeg","JPEG",'PNG', "png", "gif", "bmp");

$filepath = "upload_images/";


if ($_POST['submitbtn']=="Submit") {
 $name = $_FILES['imagefile']['name'];
 $size = $_FILES['imagefile']['size'];
if (strlen($name)) {
$extension = substr($name, strrpos($name, '.')+1);
if (in_array($extension, $file_formats)) { 
    if ($size < (2048 * 1024)) { 
        $imagename = md5(uniqid() . time()) . "." . $extension;
        $tmp = $_FILES['imagefile']['tmp_name'];


            if (move_uploaded_file($tmp, $filepath . $imagename)) {

                //execute function once uploaded

            } else {
                echo "Could not move the file.";
            }
    } else {
        echo "Your image size is bigger than 2MB.";
    }
} else {
        echo "Invalid file format.";
}
} else {
        echo "Please select image..!";
}

}
Ryan D
  • 741
  • 1
  • 11
  • 29
  • If you want to compress the image and lose some quality, you can look into https://www.apptha.com/blog/how-to-reduce-image-file-size-while-uploading-using-php-code/ – Chan Jun 19 '15 at 01:39
  • If you want to compress the image on the client side first, then look into http://stackoverflow.com/questions/2303690/resizing-an-image-in-an-html5-canvas – Chan Jun 19 '15 at 01:43

1 Answers1

0

ok so I found my solution and its as simple as it gets..

http://www.nimrodstech.com/php-image-resize/

I use my existing upload code posted above and used this plugin to reduce the size after its uploaded.. +1 mate!

Ryan D
  • 741
  • 1
  • 11
  • 29