Question

We will be uploading images that are about 3 megabytes in size in excess of 2,500px. I have my code working for uploading multiple images and would now like to have them resized or down sized to about 500px wide web viewing size. I would like it to keep a good quality but not take long to download. I'll have a preview and when you click on it open in lightbox for a bigger image which will be 500px or whatever I can get away with and still have it download fast.

I would be grateful if someone could help me modify my code to resize to 500px wide proportionally.

  $valid_formats = array("jpg", "png", "gif", "bmp");
$max_file_size = 100 * 1024 * 1024; //100 mb
$path = "PW-Files/$listingid/images/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to execute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
    if ($_FILES['files']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }          
    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }
        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }
        else{ // No error found! Move uploaded files 
        $ext = end(explode('.', $name));
        $newname = rand(10,10000) . rand(10,1000) . "." . "$ext";
            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$newname))   {
                $count++; // Number of successfully uploaded files
            }
        }
    }
    }
}
Was it helpful?

Solution

To resize your image, you can use WideImage ( php library for image manipulation ).

It provides easy way to resize your image like:

$image = WideImage::load( 'yourImage.jpg' );
$resizedImage = $image->resize( $width, $height );

You can also save the file after resizing.

$resizedImage->saveToFile( 'filename.jpg' );

For more info, see their documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top