Convert this to a function

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

  1. #1
    Hi,

    I want to convert this into a function. How would i go about doing this?

    $path_thumbs = "images/thumbs";
    $path_big = "images/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");
    
    
    //check if folders are Writable or not
    //please CHOMD them 777
    if (!is_writeable($path_thumbs)){
       die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
    }
    if (!is_writeable($path_big)){
        die ("Error: The directory <b>($path_big)</b> is NOT writable");
    }
    
    //if the for has submittedd
    if ($_POST){
    
           $file_type = $_FILES['image']['type'];
           $file_name = $_FILES['image']['name'];
           $file_size = $_FILES['image']['size'];
           $file_tmp = $_FILES['image']['tmp_name'];
    
           //check if you have selected a file.
           if(!is_uploaded_file($file_tmp)){
              echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit(); //exit the script and don't do anything else.
           }
           //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;
               }
               //function for resize image.
               if (function_exists(imagecreatetruecolor)){
               $resized_img = imagecreatetruecolor($newwidth,$newheight);
               }else{
                     die("Error: Please make sure you have GD library ver 2+");
               }
               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);
               //print message
            }
    
            //upload the big image
            move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
    			
    	$imagename = "$rand_name.$file_ext";
    Code (markup):

    Cheers,
    Adam
     
    adamjblakey, Feb 8, 2008 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok. I would first start out by putting together a list of arguments. Then I would compile them into the function like so.

    
    function imagefunction (arg 1, arg 2)
    {
    
    }
    PHP:
    Then just take and switch up your variables to match the arguments and then decide what you want to return. Probably in your case the filename on success of move_uploaded_file and FALSE on failure.
     
    daman371, Feb 8, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    is the problem solved or still having problems buddy

    Regards

    Alex
     
    kmap, Feb 9, 2008 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Hi,

    I am still having problems with converting this into a class yes.

    If you could give me a hand i would be grateful.

    Adam
     
    adamjblakey, Feb 11, 2008 IP
  5. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A function or a class? It's not the same, you know.
     
    CreativeClans, Feb 11, 2008 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Sorry i am not that clear am i, i would like this to be converted into a class please.
     
    adamjblakey, Feb 11, 2008 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Why re-invent the wheel?

    There are so many functions/classes for file/image uploads. You should build on it instead.

    Peace,
     
    Barti1987, Feb 11, 2008 IP