Change this to a function?

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

    Change this to a function?

    Hi,

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

    [PHP]$path_thumbs = "images/thumbs";
    $path_big = "images/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");


    //check if folders are Writable or not
    //please CHOMD them 777
    if (!is_writeable( $path_thumbs)){
    die ("Error: The directory <b>($path_thumb s)</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_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;
    }
    //function for resize image.
    if (function_exist s(imagecreatetr uecolor)){
    $resized_img = imagecreatetrue color($newwidth ,$newheight);
    }else{
    die("Error: Please make sure you have GD library ver 2+");
    }
    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);
    //print message
    }

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

    $imagename = "$rand_name.$fi le_ext";[/PHP]


    Cheers,
    Adam
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Hi,

    I would create a class then add the code as a function on that class. Then simply instantiate the class and reference call the function on the object:
    [php]
    class myObject
    {
    // property list
    var $porperty1 ;
    var $property2 ;
    public function myObject($pPara 1, $pPara2)
    {
    $this->property1 = $pPara1 ;
    $this->property2 = $pPara2 ;
    }
    function function1()
    {
    // your code goes here
    }

    }
    [/php]
    Then in the calling code:
    [php]
    <?php
    function __autoload($pcC lassName)
    {
    require_once '../lib/' . $pcClassName . '.php' ;
    }
    session_start() ;
    $loObject = new myObject("value 1", "value2") ; // the file name used must be myObject in this case

    $loObject->function1() ;
    ?>
    [/php]
    If the function returns anything then the call should be $lvReturn = $this->fuction1();

    I know this is only psuedo code but it should help.

    Cheers
    nathj

    PS a google search on PHP Object, or PHP functions should help too.

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Thank you for your reply, i very much doubt i am at a level thought to write my own class.

      Could someone please convert this to a class/function so i can use it?

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by adamjblakey
        Thank you for your reply, i very much doubt i am at a level thought to write my own class.
        Why so negative about your own ability? Anyone can achieve anything they really set their mind to. Nine months ago I had never developed a web site, and now 'm using PHP classes and building full web systems. You can do this!
        Originally posted by adamjblakey
        Could someone please convert this to a class/function so i can use it?
        I've pretty much done this for you already. Take the code sample I gave you, save the class definition to a php file with the same name as the class - this is case sensitive.
        Then where you want to use this class load the other lines I gave you.

        It's that easy and I've already done more than most - this is a help forum not a have my code written for me forum.

        Have a go at it and post back with specific problems when/if you get stuck - I'm happy to help.

        Cheers
        nathj

        Comment

        Working...