?
Solved

Resize Image using PHP, then move_uploaded_file

Posted on 2005-02-26
9
Medium Priority
?
4,152 Views
Last Modified: 2016-08-25
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
0
Comment
Question by:Dantheman001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:gruntar
ID: 13409526
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
 

Author Comment

by:Dantheman001
ID: 13412766
Ok Gruntar,

A true code example would be nice!

Thanks!
0
 

Author Comment

by:Dantheman001
ID: 13413490
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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 9

Expert Comment

by:gruntar
ID: 13413505
let me put together the code...
0
 
LVL 9

Accepted Solution

by:
gruntar earned 2000 total points
ID: 13413597
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
 

Author Comment

by:Dantheman001
ID: 13426192
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
 
LVL 9

Expert Comment

by:gruntar
ID: 13426812
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
 

Author Comment

by:Dantheman001
ID: 13584878
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
 

Expert Comment

by:hellos notwork
ID: 41770137
Hi I tested the code, it is not working...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question