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:

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.
resize( 800, 800, '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:

resize( 'gray' );

So I will need to put values in for everything even though some of them are the same as the default values?

resize( 600, 600, '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.

$width = 600;
$height = 600;
$background = 'gray';
$text = 'Watermark';

resize( $width, $height, $background, $text);

What is a good way to go about this?

You could reorder the parameters based upon your experience of the likelihood that you are not going to override the defaults.

function resize( $background = 'White',  $text = 'Watermark', $width = 600, $height = 600); 
// Resize code 



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

function resize( $width = NULL, $height = NULL, $background = 'White', $text = 'Watermark'); 
if( $width===NULL ) $width = 800;
if( $height===NULL ) $height = 800;

// Resize code 

resize(NULL, NULL, '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.

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.


// 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.

$i = new Resizer($path_to_file);
$i->setWatermark('My text');
// where because only one size is defined, a square can be assumed.

This carries much of the IDE benefits too.

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 :frowning:

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.