View Full Version : My First PHP script - please advise

08-14-2007, 12:05 AM

I just started learning php and only got a small way into my php/mysql bible book and got too excited had an idea for a script using what little I know to solve a problem I have. This script takes the $width and $height of an image and then works out an aspect ratio for it - using the aspect ratio I can extend this script to recommend many sizes of print that my company offers.

I am sure I have written some ugly code and I'm sure this could be done easier - any advice would be appreciated. Its my frst script so cut me a break but I would love some constructive critique - especially on the last statment which you will notice doesnt work since the ratio 1.33333333 (etc) is a recurring number - any ideas how to fix that?

Many thanks, Glyn - script below


// assign values to width and height
$width = 1;
$height = 1;

// This is probably a hack - I am pre-setting these values now to avoid a division by zero later if they are not reset later
$longestside = 1;
$shortestside = 1;

// check to see which is the longest side but first check for zero to avoid div by zero error
if ($width == 0 or $height == 0);
$longestside = $width > $height ? $width : $height;

// I now determine which is the shortest side but first check for zero to avoid div by zero error
if ($width == 0 or $height == 0);
$shortestside = $width < $height ? $width : $height;

// divide the longest side by the shortest to give an image aspect ratio
$ratio = ($longestside / $shortestside);
echo "<br>The image size ratio is $ratio";

// we now use $ratio to recommend print size
if ($ratio == 1) {
echo "<br><BR><B>Your image has an exact 1:1 ratio. You should choose a print sized 10x10, 20x20, 30x30 or 36x36<br> Based on recent sales 30x30 is our most popular customer choice for your image size


// Lets determine some other recommended print sizes based on approximates:

if ( ($ratio > 1) && ($ratio < 1.2) ) {
echo "<br><b>your image will approximately fit a 1:1 ratio best of all. It will also fit a 3:4 ratio with minimal cropping<b>";

if ( ($ratio >= 1.2) && ($ratio < 1.4) ) {
echo "<br><b>your image will approximately fit a 3:4 ratio print size: I recommend a 40x30.<b>";

if ($ratio == 1.3333333333333) {
echo "<br><b>your image is exactly 3:4 ratio and so you should choose a 3:4 print size<b>";


Len Whistler
08-14-2007, 12:34 AM
RE 1.33333333

I would round the numbers: round(1.333333333, 2); will output 1.33

08-14-2007, 12:45 AM
Hi Len

Many thanks - wasnt aware of that feature (as I say total newbie)

So If I do somthing like this first to limit the $ratio to 2 decimals?:

. $ratio = round($ratio,2);


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum