Please help me with this function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Please help me with this function

    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:

    [PHP]function uploadimage($va lue){


    $path_thumbs = "uploads/thumbs";
    $path_big = "uploads/big";

    //the new width of the resized image.
    $img_thumb_widt h = 150; // in pixcel

    $extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
    //allowed Extensions
    $limitedext = array(".gif",". jpg",".png",".j peg",".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_n ame,'.');
    $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,99999999 9);
    //get the new width variable.
    $ThumbWidth = $img_thumb_widt h;

    //keep image type
    if($file_size){
    if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
    $new_img = imagecreatefrom jpeg($file_tmp) ;
    }elseif($file_t ype == "image/x-png" || $file_type == "image/png"){
    $new_img = imagecreatefrom png($file_tmp);
    }elseif($file_t ype == "image/gif"){
    $new_img = imagecreatefrom gif($file_tmp);
    }
    //list width and height and keep height ratio.
    list($width, $height) = getimagesize($f ile_tmp);
    $imgratio=$widt h/$height;
    if ($imgratio>1){
    $newwidth = $ThumbWidth;
    $newheight = $ThumbWidth/$imgratio;
    }else{
    $newheight = $ThumbWidth;
    $newwidth = $ThumbWidth*$im gratio;
    }
    imagecopyresize d($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    //save image
    ImageJpeg ($resized_img," $path_thumbs/$rand_name.$fil e_ext");
    ImageDestroy ($resized_img);
    ImageDestroy ($new_img);

    //upload the big image
    move_uploaded_f ile ($file_tmp, "$path_big/$rand_name.$fil e_ext");
    }

    $value = "$rand_name.$fi le_ext";

    return $value;

    }[/PHP]

    Thanks in advanced.
    Adam
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Don't let us guess. What exactly IS working and what IS NOT working? Have you put any echoes in there to see where it goes wrong?

    Ronald

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Sorry about that i thought i put in the problem.

      It simply goes to a blank screen when it has been posted.

      Where should i put in the echo's?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        I can't see it. But how do you call this function from your code?
        How are you sure that the problem it is in this function and not in the code leading up to, or executing, the call to this function?

        Ronald

        Comment

        • adamjblakey
          New Member
          • Jan 2008
          • 133

          #5
          I just assumed it was the function.

          I am calling the function by:

          [PHP]$image = uploadimage($_F ILES['image']);
          $image2 = uploadimage($_F ILES['image2']);
          $image3 = uploadimage($_F ILES['image3']);[/PHP]

          Is this correct?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by adamjblakey
            I just assumed it was the function.

            I am calling the function by:

            [PHP]$image = uploadimage($_F ILES['image']);
            $image2 = uploadimage($_F ILES['image2']);
            $image3 = uploadimage($_F ILES['image3']);[/PHP]

            Is this correct?
            What I actually wanted to see is the html code that leads up to the function, i.e. the form where you specify the 'type=file'.

            Some remarks:
            1. you pass a $_FILES['image'], [ image2'] and ['image3'] array key to the function but the $_FILES you actually upload are always in $_FILES['image'].

            2. Function parameter variable $value is not used in the function except another $value in the return. Be aware that the variable $value you pass to the function is not the same variable $value you return from the function.

            Ronald

            Comment

            • adamjblakey
              New Member
              • Jan 2008
              • 133

              #7
              This is the form:

              Code:
              <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>
              What would i need to change the function to so that it is not specified to just image?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by adamjblakey
                This is the form:

                Code:
                <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>
                What would i need to change the function to so that it is not specified to just image?
                Not sure what you mean..
                but you'd have to allow more extensions

                Comment

                • adamjblakey
                  New Member
                  • Jan 2008
                  • 133

                  #9
                  Originally posted by markusn00b
                  Not sure what you mean..
                  but you'd have to allow more extensions
                  Right i have built this to test out the function.

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

                  [PHP]<?php

                  function uploadimage($va lue){

                  $file_type = $value['type'];
                  $file_name = $value['name'];
                  $file_size = $value['size'];
                  $file_tmp = $value['tmp_name'];

                  //check file extension
                  $ext = strrchr($file_n ame,'.');
                  $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,99999999 9);
                  //get the new width variable.
                  $ThumbWidth = 150;

                  //keep image type
                  if($file_size){
                  if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                  $new_img = imagecreatefrom jpeg($file_tmp) ;
                  }elseif($file_t ype == "image/x-png" || $file_type == "image/png"){
                  $new_img = imagecreatefrom png($file_tmp);
                  }elseif($file_t ype == "image/gif"){
                  $new_img = imagecreatefrom gif($file_tmp);
                  }
                  //list width and height and keep height ratio.
                  list($width, $height) = getimagesize($f ile_tmp);
                  $imgratio=$widt h/$height;
                  if ($imgratio>1){
                  $newwidth = $ThumbWidth;
                  $newheight = $ThumbWidth/$imgratio;
                  }else{
                  $newheight = $ThumbWidth;
                  $newwidth = $ThumbWidth*$im gratio;
                  }
                  imagecopyresize d($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                  //save image
                  ImageJpeg ($resized_img," uploads/thumbs/$rand_name.$fil e_ext");
                  ImageDestroy ($resized_img);
                  ImageDestroy ($new_img);

                  //upload the big image
                  move_uploaded_f ile ($file_tmp, "uploads/big/$rand_name.$fil e_ext");
                  }

                  $value = "$rand_name.$fi le_ext";

                  return $value;

                  }


                  if ($_POST){

                  $image = uploadimage($_F ILES['image']);
                  $image2 = uploadimage($_F ILES['image2']);
                  $image3 = uploadimage($_F ILES['image3']);

                  print "Image 1: <a href='uploads/big/$image'>$image</a>";
                  print "<br>";
                  print "Image 2: <a href='uploads/big/$image3'>$image 2</a>";
                  print "<br>";
                  print "Image 3: <a href='uploads/big/$image2'>$image 3</a>";

                  }

                  ?>

                  <form action="" method="post" enctype="multip art/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]

                  Comment

                  Working...