Can someone please make this a function..

Discussion in 'PHP' started by adamjblakey, Sep 21, 2007.

  1. #1
    Hi,

    Please can someone turn this into a function for me:

    <?php
    
    
    $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 (isset($_POST['upForm'])){
    
           $file_type = $_FILES['imgfile']['type'];
           $file_name = $_FILES['imgfile']['name'];
           $file_size = $_FILES['imgfile']['size'];
           $file_tmp = $_FILES['imgfile']['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
               echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
            }
    
            //upload the big image
            move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
    
    
    }else{ //if the form hasn't been submitted.
    
          //print the form
          echo "Sorry";
    
    }
    
    
    ?>
    Code (markup):
    I want to be able to go e.g. $bannerimage = resizedimage("$_POST['bannerimage']");

    So i don't have to put the full lot of code all the time.

    Cheers in advanced for your help.

    Adam
     
    adamjblakey, Sep 21, 2007 IP
  2. meetgs

    meetgs Active Member

    Messages:
    957
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    70
    #2
    what is $_POST['bannerimage']" ?

    should be pretty easy to convert,
    you just have to modify this part:
    if (isset($_POST['upForm'])){
    $file_type = $_FILES['imgfile']['type'];
    $file_name = $_FILES['imgfile']['name'];
    $file_size = $_FILES['imgfile']['size'];
    $file_tmp = $_FILES['imgfile']['tmp_name'];

    and put the whole code in
    function resizedimage($param) {
    THE WHOLE CODE
    }

    but i don't know what "upForm" is..
     
    meetgs, Sep 21, 2007 IP
  3. meetgs

    meetgs Active Member

    Messages:
    957
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    70
    #3
    maybe you can replace

    if (isset($_POST['upForm'])){
    $file_type = $_FILES['imgfile']['type'];
    $file_name = $_FILES['imgfile']['name'];
    $file_size = $_FILES['imgfile']['size'];
    $file_tmp = $_FILES['imgfile']['tmp_name'];

    with

    if (isset($param)) {
    $file_type = $param['type'];
    $file_name = $param['name'];
    $file_size = $param['size'];
    $file_tmp = $param['tmp_name'];


    and to call the function:
    $bannerimage = resizedimage($_POST['bannerimage']);

    good luck!
     
    meetgs, Sep 21, 2007 IP