# Upload and resize two images at the same time

**URL:** <https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036>\
**Category:** Beginners - Learning PHP\
**Created:** [January 18, 2021, 11:56am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036 "2021-01-18T11:56:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 18, 2021, 11:56am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/1 "2021-01-18T11:56:18Z")

</div>

Hello,  
I have code that resizes the images  
images\_resize.php

There are two image upload options in the member profile editing area  
1.Brand logo upload option  
`<input type="file" name="brand_logo">`

2.Profile picture upload option  
`<input type="file" name="profile_picture">`

################################################################################

```
if($_FILES['brand_logo']['size'] > 0){

//For Brand Logo Images
$width = "260";
$height = "113";
$home_directory = "images/";
$subdirectory = "logo_pictures/";

   include(images_resize.php);
   // Output variable
   $brand_logo_resize;
}

```

################################################################################

```
if($_FILES['profile_picture']['size'] > 0){

//For Profile Pictures
$width = "80";
$height = "80";
$home_directory = "images/";
$subdirectory = "profile_pictures/";

   include(images_resize.php);
   // Output variable
   $profile_picture_resize;
}

```

Image processing code is below

```
if (isset($_FILES["fileToUpload"]) && $_FILES["fileToUpload"]["size"] > 0) {

$target_dir = $home_directory.$subdirectory;

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    //echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

############### Image resizing ################################################
function boyutla($resim, $tresim, $max_en, $max_boy){  
              
  $arraylar = explode('.',$resim);
  $sonuc = $arraylar[(count($arraylar) -1)];
  $sonuc = trim($sonuc);
  $sonuc = strtolower($sonuc);
  
  switch ($sonuc){
      case "jpeg": $islem=imagecreatefromjpeg($resim); break;
      case "JPEG": $islem=imagecreatefromjpeg($resim); break;
      case "jpg": $islem=imagecreatefromjpeg($resim); break;
      case "gif": $islem=imagecreatefromgif($resim); break;
      case "png": $islem=imagecreatefrompng($resim); break;
      case "JPG": $islem=imagecreatefromjpeg($resim); break;
      case "GIF": $islem=imagecreatefromgif($resim); break;
      case "PNG": $islem=imagecreatefrompng($resim); break;            
  }
  
  $boyut = getimagesize($resim);  
  $en = $boyut[0];
  $boy = $boyut[1];
   
  $x_oran = $max_en / $en;  
  $y_oran = $max_boy / $boy;  
   
          if (($en <= $max_en) and ($boy <= $max_boy)){  
              $son_en = $en;  
              $son_boy = $boy;  
          }  
          else if (($x_oran * $boy) < $max_boy){  
              $son_en = $max_en;  
              $son_boy = ceil($x_oran * $boy);  
          }  
          else {  
              $son_en = ceil($y_oran * $en);  
              $son_boy = $max_boy;  
          }  
  
  $yeni = ImageCreateTrueColor($son_en,$son_boy);
  imagealphablending($yeni, false);
  imagesavealpha($yeni, true);
  $transparent = imagecolorallocatealpha($yeni, $son_en, $son_boy, $en, $boy);
  imagefilledrectangle($yeni, 0, 0, $son_en, $son_boy, $transparent);   
  imagecopyresampled($yeni,$islem,0,0,0,0,$son_en,$son_boy,$en,$boy);  

  switch ($sonuc){
      case "jpeg": imagejpeg($yeni,$tresim); break;
      case "JPEG": imagejpeg($yeni,$tresim); break;
      case "jpg": imagejpeg($yeni,$tresim); break;
      case "gif": imagegif($yeni,$tresim); break;
      case "png": imagepng($yeni,$tresim); break;
      case "JPG": imagejpeg($yeni,$tresim); break;
      case "GIF": imagegif($yeni,$tresim); break;
      case "PNG": imagepng($yeni,$tresim); break;            
  }
  return $resim; 
}
############### Image resizing ################################################

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && 
   $imageFileType != "png" && 
   $imageFileType != "jpeg"&& 
   $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  $temp = explode(".", $_FILES["fileToUpload"]["name"]);
  $newfilename = round(microtime(true)) . '.' . end($temp);

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $newfilename)) {

    boyutla($target_dir . $newfilename, $target_dir.$newfilename, $width, $height);
    $output_variable = $target_dir.$newfilename;
    
    echo "<br />The file ". $newfilename. " has been uploaded.";

  } else {
    echo "<br />Sorry, there was an error uploading your file.";
  }
}
  echo "<br />Output image name: ".$output_variable;

} // if ($_FILES["fileToUpload"]["size"] > 0) {

```

I want to resize when either or both are loaded at the same time  
However, it doesn’t resize when both are installed at the same time  
How do I do this?

---

<div class="post-metadata">

**Author:** ![Mchojrin](https://www.phphelp.com/user_avatar/www.phphelp.com/mchojrin/32/3455_2.png) [@Mchojrin](https://www.phphelp.com/u/Mchojrin)\
**Post date:** [January 19, 2021, 2:11pm UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/2 "2021-01-19T14:11:27Z")

</div>

I’m a little confused here, in the form you are creating two input files () but in your images\_resize.php code you’re only referring to `$_FILES['fileToUpload']`… how are you mapping the inputs to the file used in the resizing process?

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [January 20, 2021, 9:27pm UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/3 "2021-01-20T21:27:32Z")

</div>

You are loading the resizing code twice. You should not. Load it once as a function and then call it twice using the two filenames once they are uploaded. Hope that helps you rewrite it.

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 9:24am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/4 "2021-01-23T09:24:56Z")

</div>

> [@Mchojrin](#):
>
> I’m a little confused here, in the form you are creating two input files () but in your images\_resize.php code you’re only referring to `$_FILES['fileToUpload']` … how are you mapping the inputs to the file used in the resizing process?

It is not important,  
Two pictures are uploaded after all  
If we write the installation name of one, what will the other be

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 9:30am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/5 "2021-01-23T09:30:57Z")

</div>

> [@ErnieAlex](#):
>
> You are loading the resizing code twice. You should not. Load it once as a function and then call it twice using the two filenames once they are uploaded. Hope that helps you rewrite it.

Can you write in a little more detail how I should do it?  
You can upload only logo pictures, or only profile pictures, or both.  
This should work in case of 3 options.  
I’ll write these uploaded filenames into the database  
table column: profile\_picture\_resize = $ profile\_picture\_resize  
table column: brand\_logo\_resize = $ brand\_logo\_resize

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 9:36am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/6 "2021-01-23T09:36:21Z")

</div>

I was able to reply late because of my other work, excuse me

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 11:18am UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/7 "2021-01-23T11:18:13Z")

</div>

ErnieAlex, now I know what you said and I solved the problem.

```
$loaded_images = [];
      if(isset($_FILES["logo_pictures"]) && $_FILES["logo_pictures"]["size"] > 0){
        $loaded_images["logo"] = ['logo_name'=>$_FILES["logo_pictures"]["name"],'logo_size'=>$_FILES["logo_pictures"]["size"],'logo_tmp_name'=>$_FILES["logo_pictures"]["tmp_name"]];
      }

      if(isset($_FILES["profile_pictures"]) && $_FILES["profile_pictures"]["size"] > 0){
        $loaded_images["profile"] = ['profile_name'=>$_FILES["profile_pictures"]["name"],'profile_size'=>$_FILES["profile_pictures"]["size"],'profile_tmp_name'=>$_FILES["profile_pictures"]["tmp_name"]];
      }

// Resize code I have included here or include   
 
    foreach($loaded_images AS $images => $image){
      if($images == "logo"){
variables:
$image['logo_name']
$image['logo_size']
$image['logo_tmp_name']
// Other upload codes here
      }elseif($images == "profile"){
variables:
$image['profile_name']
$image['profile_size']
$image['profile_tmp_name']
// Other upload codes here
      }
    }
```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [January 23, 2021, 2:36pm UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/8 "2021-01-23T14:36:02Z")

</div>

Nice! I was not here for the last day or two. Just got to read your previous note.

Glad you solved it. Always nice to solve a programming puzzle! Good for you!

See you in your next post! Enjoy your day!

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 3:33pm UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/9 "2021-01-23T15:33:58Z")

</div>

Unfortunately, I have a problem,

There was no problem on the test page  
I ran into a problem when I tried it in the user edit area  
Problems with changing upload folders of images  
For Logo image: “…/images”  
For Profile image: “images”

The second row upload image cannot be loaded because the path is different  
What would be the reason?

---

<div class="post-metadata">

**Author:** ![Adem](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@Adem](https://www.phphelp.com/u/Adem)\
**Post date:** [January 23, 2021, 5:21pm UTC](https://www.phphelp.com/t/upload-and-resize-two-images-at-the-same-time/33036/10 "2021-01-23T17:21:27Z")

</div>

I solved the problem

include\_once(“image\_upload\_code.php”); ===\> require(“image\_upload\_code.php”);

Thank you
