0

I am trying to resize multiple images through a fileupload. the current code takes several images but only resizes one.Im new in php

I have researched a lot in google in several languages ​​but I have not found anything that is just php

php7, apache 2

<form class="form-horizontal" action="resise2.php" enctype="multipart/form-data" method="POST">
<input name="archivo" type="file" accept="image/png, image/jpg, 
 image/jpeg " multiple   /> 
<input type="submit" name="submit"value=enviar>
</form>

<?php
 if(isset($_POST["submit"])) {
 if(is_array($_FILES)) {
 $uploadedFile = $_FILES['archivo']['tmp_name']; 
 $nombre=$_FILES["archivo"]['name'];
 $sourceProperties = getimagesize($uploadedFile);
 $newFileName = time();
 $dirPath = "/fotospagina/";
 $ext = pathinfo($_FILES['archivo']['name'], PATHINFO_EXTENSION);
 $imageType = $sourceProperties[2];

    switch ($imageType) {


        case IMAGETYPE_PNG:
            $imageSrc = imagecreatefrompng($uploadedFile); 
            $tmp = 

imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagepng($tmp,$dirPath. $newFileName. "_thump.". $ext);
            break;           

        case IMAGETYPE_JPEG:
            $imageSrc = imagecreatefromjpeg($uploadedFile); 
            $tmp = 
imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagejpeg($tmp,$dirPath. $newFileName. "_thump.". $ext);

            break;

        case IMAGETYPE_GIF:
            $imageSrc = imagecreatefromgif($uploadedFile); 
            $tmp = 
imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
            imagegif($tmp,$dirPath. $newFileName. "_thump.". $ext);
            break;

        default:
            echo "Invalid Image type.";
            exit;
            break;
    }


    move_uploaded_file($newFileName, $dirPath. $newFileName. ".". 
 $ext);
    echo "Image Resize Successfully.";


?>
  • 1
    Possible duplicate of [Multiple file upload in php](https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php). Also see [Uploading multiple files @ php.net](https://www.php.net/manual/en/features.file-upload.multiple.php). – showdev Jul 29 '19 at 20:11
  • Your question implies you're able to resize a single image. Are you asking how to resize images in general? See [Resize image in PHP](https://stackoverflow.com/questions/14649645/resize-image-in-php). If you're looking for PHP libraries, see [class.upload.php](https://www.verot.net/php_class_upload.htm), [Intervention Image](http://image.intervention.io/), [Grafika](https://github.com/kosinix/grafika), [etc](https://www.google.com/search?q=php+image+resize+library). – showdev Jul 29 '19 at 20:17
  • As it says in the description. the code allows the resizing of a single image, I need 1 to n – Wladimir Arnaldo Briceo Mirand Jul 29 '19 at 20:24
  • [Multiple file upload in php](https://stackoverflow.com/a/12006515/924299) and [Uploading multiple files](https://www.php.net/manual/en/features.file-upload.multiple.php#114235) both explain how to handle multiple fie uploads, specifically by naming your input like `name="archivo[]"`. – showdev Jul 29 '19 at 20:29
  • if I add [] I see the following error Warning: getimagesize() expects parameter 1 to be string, array given in C:\AppSe – Wladimir Arnaldo Briceo Mirand Jul 29 '19 at 20:43
  • `$_FILES` will be an array of upload images. Note the part of [this code](https://stackoverflow.com/a/12006515/924299) that says "// Loop through each file" and [this comment](https://www.php.net/manual/en/features.file-upload.multiple.php#53240) discussing the structure of the `$_FILES` array. – showdev Jul 29 '19 at 20:47
  • thank you very much for your answers I added the loop and now it takes more than one file, the problem is that for example if I add 3 jpg and 2 png files just upload a jpg and a png – Wladimir Arnaldo Briceo Mirand Jul 29 '19 at 21:01

0 Answers0