Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Is null Less Than a Positive Integer

images php coding issues or problems here.

Moderators: macek, egami, gesf

Is null Less Than a Positive Integer

Postby mikeh0001 » Tue Sep 11, 2012 5:59 pm

Hi,

I'm working with the code below which comes out of a wordpress function. I'm just learning php and I'm trying to figure out some of the code. On line 2 and 3, $new_w and $new_h are set to be the lowest value passed into the min() function. Then there are a couple of "if" statements which test for "!$new_w" and "!$new_h". Now in order for !$new_w or !$new_h to to TRUE, $new_w or $new_h has to be FALSE. But they will only be false if they hold a value of 0 or null. So does anyone get what lines 4 and 5 are trying to test for, and what they are trying to do if a condition is TRUE?


Thanks, Mike



function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {

if ($orig_w <= 0 || $orig_h <= 0)
return false;

if ($dest_w <= 0 && $dest_h <= 0)
return false;


if ( $crop ) {

1. $aspect_ratio = $orig_w / $orig_h;

2. $new_w = min($dest_w, $orig_w);
3. $new_h = min($dest_h, $orig_h);

4. if ( !$new_w ) {$new_w = intval($new_h * $aspect_ratio);}

5. if ( !$new_h ) {$new_h = intval($new_w / $aspect_ratio);}


}
mikeh0001
New php-forum User
New php-forum User
 
Posts: 4
Joined: Tue Sep 11, 2012 5:26 pm

Re: Is null Less Than a Positive Integer

Postby seandisanti » Tue Oct 02, 2012 11:39 am

Code tags help readability.
Code: Select all

if ( $crop ) {

1. $aspect_ratio = $orig_w / $orig_h;

2. $new_w = min($dest_w, $orig_w);
3. $new_h = min($dest_h, $orig_h);

4. if ( !$new_w ) {$new_w = intval($new_h * $aspect_ratio);}

5. if ( !$new_h ) {$new_h = intval($new_w / $aspect_ratio);}


}

lines 2 and 3 determine new width and height based on original or destination size.

lines 4 and 5 look at new_w and new_h (since you're probably only specifying a new width or a new height) and assign the corresponding value for the whichever is not set.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => Images

Who is online

Users browsing this forum: Google [Bot] and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.