Upload and resize two images at the same time

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?

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?

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.

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

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

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

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
      }
    }

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!

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?

I solved the problem

include_once(“image_upload_code.php”); ===> require(“image_upload_code.php”);

Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service