Resize Image using PHP, then move_uploaded_file

Ok guys.

Problem: I am trying to upload jpeg/jpg files to the server using move_upload_file. It can be done using the following code:
========================================================
//this moves image file onto server
                              if(move_uploaded_file($_FILES['the_file']['tmp_name'], $image_path . "/" . $file_name))
                              { update details it database etc

========================================================

What I am trying to do now is resize the image file ('the_file']['tmp_name']) and then upload the new file. I am unsuccessful so far!

Here is the code that I am playing around with: (remember this is the modified code in comparison to the one above!)
-----------

//this moves image file onto server
//store file in another temp one
$tempfilename = $_FILES['the_file']['tmp_name'];                              

$testimage=imagecreatefromjpeg($_FILES['the_file']['tmp_name']);

//resample
$image_p = imagecreatetruecolor(300, 300);
imagecopyresampled($image_p, $testimage, 0, 0, 0, 0, 300, 300, $width_orig, $height_orig);
                 
imagejpeg($image_p, $target_file,80);
                         
imageDestroy($image_p);
imageDestroy($testimage);

if(move_uploaded_file($target_file, $image_path . "/" . $file_name)) {

update database etc:
}

------------------
I asbsolutely think I am going wrong somewhere with move_uploaded_file. Unsure though! :(


I am giving 500 points for this because of ONE simple reason:
1) it is EXTREMELY urgent

Thanks a lot guys!
Dan
Dantheman001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gruntarCommented:
Hi,

you cannot check move uploaded file after you processing. Do your upload file just like before with minor exeption. You move your file to your own temp dir somewhere in your document root.

if(move_uploaded_file($target_file, $image_path . "/" . $file_name)) {

// here do your img resize and save resized image to final destination
// then delete original uploaded file so that your "temp" don't use your space
// update your database

}

that way you check if ifle upload was ok and prosess your image...

hope that helps.

cheers
0
Dantheman001Author Commented:
Ok Gruntar,

A true code example would be nice!

Thanks!
0
Dantheman001Author Commented:
OK.

I have seem to come up with the solution myself -- by not using move_upload_file at all...just using the imagejpeg function. Will post solution later.

But gruntar, would love to see what you have.

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

gruntarCommented:
let me put together the code...
0
gruntarCommented:
change all nessesary paths, dirs...
any more questons just ask

cheers

<?php

$temp_dir = $_SERVER['DOCUMENT_ROOT'] . '/www/testi/tmp';
$dest_dir = $_SERVER['DOCUMENT_ROOT'] . '/www/testi/slike';

$file_name = $_FILES['the_file']['name'];


if (move_uploaded_file($_FILES['the_file']['tmp_name'], $temp_dir . '/' . $file_name)) {
      

   if(resize_img($temp_dir, $dest_dir, $file_name, 300)) {
         echo 'Image processed and saved.';
         // add your database code here
   } else {
         echo 'Cannot process image.';
   }
 
   // here we remove uploaded file that have been moved to temp dir
   // image is already in final destination
   unlink($temp_dir . '/' . $file_name);
   
} else {
      
   echo 'Cannot upload file.';
}

}


 function resize_img($dir_in, $dir_out, $imedat='defaultname.jpg', $max=250) {

   $img = $dir_in . '/' . $imedat;
   $extension = array_pop(explode('.', $imedat));

   switch ($extension){
   
         case 'jpg':
         case 'jpeg':
         $image = ImageCreateFromJPEG($img);
         break;
               
         case 'png':
         $image = ImageCreateFromPNG($img);
         break;
               
         default:
         $image = false;
   }


if(!$image){
      // not valid img stop processing
      return false;
}

 $vis = imagesy($image);
 $sir = imagesx($image);

  if(($vis < $max) && ($sir < $max)) {
     $nvis=$vis; $nsir=$sir;
  } else {
    if($vis > $sir) { $nvis=$max; $nsir=($sir*$max)/$vis;}
    elseif($vis < $sir) { $nvis=($max*$vis)/$sir; $nsir=$max;}
    else { $nvis=$max; $nsir=$max;}
  }

      $out = ImageCreateTrueColor($nsir,$nvis);
      ImageCopyResampled($out, $image, 0, 0, 0, 0, $nsir, $nvis, $sir, $vis);

   switch ($extension){
   
         case 'jpg':
         case 'jpeg':
         imageinterlace($out ,1);
         ImageJPEG($out, $dir_out . '/' . $imedat, 75);
         break;
               
         case 'png':
         ImagePNG($out, $dir_out . '/' . $imedat);
         break;
               
         default:
         $out = false;
   }
   
   if(!$out){
         return false;
   }
   
ImageDestroy($image);
ImageDestroy($out);

return true;
}


?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dantheman001Author Commented:
Well, this is what I did.

//Set max image dimensions
$width = 450;
$height = 400;

//Figure our correct proportions, maintain aspect ratio
   if (($width<=0) or ($width>$width_orig)) {
     $width=$width_orig;
   }
   $height=($width_orig*($width/$width_orig));

   if ($width_orig > $height_orig) {
       $width=$width;
       $height=$height_orig*($height/$width_orig);

   }
   if ($width_orig < $height_orig) {
       $width=$width_orig*($width/$height_orig);
       $height=$height;
   }
   if ($width_orig == $height_orig) {
       $width=$width;
       $height=$height;
   }
      
   //declare and assign         
   $tempfilename = ($_FILES['the_file']['tmp_name']);            
   $testimage =imagecreatefromjpeg($tempfilename);
   $image_p = imagecreatetruecolor($width, $height);
   //resample and save image
   if(imagecopyresampled($image_p, $testimage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)){
                     //save image to dir
                  imagejpeg($image_p, $image_path . "/" . $file_name,80);
                imageDestroy($image_p);
   }                  
      
Thanks for your help though!
0
gruntarCommented:
Hm, I gave you solution so you can check if file was uploaded as you asked in your question. And you have few "holes" in your script. Does it work?

1. Your script doesn't check for $width_orig and and $height_orig, how do you know what are
    dimensions of your uploaded file?
2. it is totally weird that you set both new width and height, as if ALL of your uploaded images
    would have same dimensions (same width, height ratio).
3. "But gruntar, would love to see what you have.", yea, so you could then use my code anyway.

.... pretty lame to save points :|
0
Dantheman001Author Commented:
Gruntar,

I thought I thanked you for your help - however, I had to come up with my own code because I could not wait any longer for the next solution to be posted.

Can you please identify the holes with my script?

Did I copy your code? Not at all.
0
hellos notworkCommented:
Hi I tested the code, it is not working...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.