Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My First PHP script - please advise

    Hi,

    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

    <?php

    // 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);
    else
    $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);
    else
    $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

    ratio<B>";
    }

    // 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>";
    }

    ?>

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    RE 1.33333333

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

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);

    Glyn


  •  

    Posting Permissions

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