SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)

    User functions and parameter use

    I find I seem to write a few image modify programs and so I was going to create a function:
    PHP Code:
    function resize$width 600$height 600$background 'White'$text 'Watermark');
    {
    // Resize code
    }

    // Will give me a new image 600 wide 600 heigh on a white background watermarked with the text watermark.
    resize( );

    // Will give me a new image 800 wide 800 heigh on a gray background watermarked with the text Anthony.
    resize800800'gray''Anthony'); 
    If I want an image 600 wide 600 heigh on a gray background watermarked with the text watermark.
    I can not do this as I will get an error:
    PHP Code:
    resize'gray' ); 
    So I will need to put values in for everything even though some of them are the same as the default values?
    PHP Code:
    resize600600'gray''watermark'); 
    It looks like it would be easier to define the variables and just use them in the function call and not have any defaults in the function?
    Thinking about this if I wanted to create two different thumbnail sizes or similar I would need to set two lots of variables.

    PHP Code:
    $width 600;
    $height 600;
    $background 'gray';
    $text 'Watermark';

    resize$width$height$background$text); 
    What is a good way to go about this?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could reorder the parameters based upon your experience of the likelihood that you are not going to override the defaults.

    PHP Code:
    function resize$background 'White',  $text 'Watermark'$width 600$height 600); 

    // Resize code 


    resize('gray'); 
    OR,

    hardcode the defaults in the function, and leave the param default as NULL (yuk)

    PHP Code:
    function resize$width NULL$height NULL$background 'White'$text 'Watermark'); 

    if( 
    $width===NULL $width 800;
    if( 
    $height===NULL $height 800;

    // Resize code 


    resize(NULLNULL'gray'); 
    The benefits of these 2 is that your IDE may well prompt you to remind you of the order and help you fill them in as soon as it sees you write resize( ...

    OR pass in an array of values and do say, a ternary check otherwise use a hardcoded default.

    PHP Code:
    function resize$params ); 

    $background = (isset($params['background'])) ? $params['background'] : 'white';
    $background = (isset($params['height'])) ? $params['height'] : 800;
    $background = (isset($params['width'])) ? $params['width'] : 800;

    // Resize code 


    $params = array('background' => "gray");
    resize$params ); 
    This has the drawback that you have to remember the values names but not the order. It is however useful if your input is coming from a form, say, which is already in a post or get array - simply unset() the form elements you do NOT want to send to the resize - or the params array can be an sql result as an array.

    PHP Code:
    <?php

    // if this is spoofing your POST input ...
    $_POST['background'] = 'green';
    $_POST['height'] = '500';
    $_POST['width'] = '500';
    $_POST['submit'] = 'yes';

    unset( 
    $_POST['submit']);// ko this one

    resize$_POST );
    This is one of those areas that OOP might excel if you wanted to load even more smarts into it.

    PHP Code:
    $i = new Resizer($path_to_file);
    $i->setWatermark('My text');
    $i->setSize(300);
    // where because only one size is defined, a square can be assumed. 
    This carries much of the IDE benefits too.

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thanks for the detailed reply; I did think about OOP but after our last discussion on the subject I was a bit unsure I could write the code

    I think the most straight forward way of doing it is to leave out the defaults in the function and include them all in the function call. I thought there may be something I was missing in the function or function call method.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •