Please help me with this function.

Discussion in 'PHP' started by adamjblakey, Feb 20, 2008.

  1. #1
    Hi,

    I am trying to build a function and from a script but it does not seem to be working. Can anyone see anything wrong with this?

    Also i need it to convert the large image to a specific size also, like the thumbnail so i need to put something in there so i can enter a width of the large image and this will be resized also.

    Here is the function:

    function uploadimage($value){
    
    
    $path_thumbs = "uploads/thumbs";
    $path_big = "uploads/big";
    
    //the new width of the resized image.
    $img_thumb_width = 150; // in pixcel
    
    $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
    //allowed Extensions
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
    
    
           $file_type = $_FILES['image']['type'];
           $file_name = $_FILES['image']['name'];
           $file_size = $_FILES['image']['size'];
           $file_tmp = $_FILES['image']['tmp_name'];
    
           //check file extension
           $ext = strrchr($file_name,'.');
           $ext = strtolower($ext);
           if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
              echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
           }
    	   
           //get the file extension.
           $getExt = explode ('.', $file_name);
           $file_ext = $getExt[count($getExt)-1];
    
           //create a random file name
           $rand_name = md5(time());
           $rand_name= rand(0,999999999);
           //get the new width variable.
           $ThumbWidth = $img_thumb_width;
    
           //keep image type
           if($file_size){
              if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                   $new_img = imagecreatefromjpeg($file_tmp);
               }elseif($file_type == "image/x-png" || $file_type == "image/png"){
                   $new_img = imagecreatefrompng($file_tmp);
               }elseif($file_type == "image/gif"){
                   $new_img = imagecreatefromgif($file_tmp);
               }
               //list width and height and keep height ratio.
               list($width, $height) = getimagesize($file_tmp);
               $imgratio=$width/$height;
               if ($imgratio>1){
                  $newwidth = $ThumbWidth;
                  $newheight = $ThumbWidth/$imgratio;
               }else{
                     $newheight = $ThumbWidth;
                     $newwidth = $ThumbWidth*$imgratio;
               }
               imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
               //save image
               ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
               ImageDestroy ($resized_img);
               ImageDestroy ($new_img);
    
            //upload the big image
            move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
    		}	
    		
    	$value = "$rand_name.$file_ext";
    	
    	return $value;
    	
    }
    PHP:
    Thanks in advanced.
    Adam
     
    adamjblakey, Feb 20, 2008 IP
  2. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Right i have built this to test out the function.

    It is uploading the normal image now but is not uploading the thumbs.

    <?php
    
    function uploadimage($value){
    
    	   $file_type = $value['type'];
           $file_name = $value['name'];
           $file_size = $value['size'];
           $file_tmp = $value['tmp_name'];
    
           //check file extension
           $ext = strrchr($file_name,'.');
           $ext = strtolower($ext);
    
    	   
           //get the file extension.
           $getExt = explode ('.', $file_name);
           $file_ext = $getExt[count($getExt)-1];
    
           //create a random file name
           $rand_name = md5(time());
           $rand_name= rand(0,999999999);
           //get the new width variable.
           $ThumbWidth = 150;
    
           //keep image type
           if($file_size){
              if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                   $new_img = imagecreatefromjpeg($file_tmp);
               }elseif($file_type == "image/x-png" || $file_type == "image/png"){
                   $new_img = imagecreatefrompng($file_tmp);
               }elseif($file_type == "image/gif"){
                   $new_img = imagecreatefromgif($file_tmp);
               }
               //list width and height and keep height ratio.
               list($width, $height) = getimagesize($file_tmp);
               $imgratio=$width/$height;
               if ($imgratio>1){
                  $newwidth = $ThumbWidth;
                  $newheight = $ThumbWidth/$imgratio;
               }else{
                     $newheight = $ThumbWidth;
                     $newwidth = $ThumbWidth*$imgratio;
               }
               imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
               //save image
               ImageJpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
               ImageDestroy ($resized_img);
               ImageDestroy ($new_img);
    		 
            //upload the big image
            move_uploaded_file ($file_tmp, "uploads/big/$rand_name.$file_ext");
    		}	
    		
    	$value = "$rand_name.$file_ext";
    	
    	return $value;
    	
    }
    
    
    if ($_POST){
    
    	$image = uploadimage($_FILES['image']);
    	$image2 = uploadimage($_FILES['image2']);
    	$image3 = uploadimage($_FILES['image3']);
    	
    	print "Image 1: <a href='uploads/big/$image'>$image</a>";
    		print "<br>";
    	print "Image 2: <a href='uploads/big/$image3'>$image2</a>";
    		print "<br>";
    	print "Image 3: <a href='uploads/big/$image2'>$image3</a>";
    
    }
    
    ?>
    
    <form action="" method="post" enctype="multipart/form-data">
      <table width="100%" border="0" cellspacing="0" cellpadding="3">
        <tr>
          <td><input name="image" type="file" id="image" /></td>
        </tr>
        <tr>
          <td><input name="image2" type="file" id="image2" /></td>
        </tr>
        <tr>
          <td><input name="image3" type="file" id="image3" /></td>
        </tr>
        <tr>
          <td><input type="submit" name="Submit" value="Submit" /></td>
        </tr>
      </table>
    </form>
    PHP:
     
    adamjblakey, Feb 20, 2008 IP
  3. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Well here's one problem.

    ImageJpeg and ImageDestroy shouldn't be capitalized like that. They should be all lower case - imagejpeg() and imagedestroy(). That should cause a fatal error, though - "Call to undefined function."

    - Walkere
     
    walkere, Feb 20, 2008 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Thanks for that, but still has not resolved issue with the small image upload.

    Any ideas?
     
    adamjblakey, Feb 21, 2008 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    Try just moving it while you do the larger one aswell.
     
    HuggyStudios, Feb 21, 2008 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    I have tried adding:

    move_uploaded_file ($resized_img, "uploads/thumbs/$rand_name.$file_ext");

    After ImageDestroy ($new_img);

    But that does not work.
     
    adamjblakey, Feb 21, 2008 IP
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    Why adding it afterwards?
     
    HuggyStudios, Feb 21, 2008 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Where would you suggest it going?

    I have put it here and this still doesn't work.

    //save image
    		   move_uploaded_file ($resized_img, "uploads/thumbs/$rand_name.$file_ext");
               imagejpeg ($resized_img,"uploads/thumbs/$rand_name.$file_ext");
               imagedestroy ($resized_img);
               imagedestroy ($new_img);
    PHP:
     
    adamjblakey, Feb 21, 2008 IP