1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Calculate Desired Height

Discussion in 'PHP' started by Jeremy Benson, Aug 8, 2017.

  1. #1
    I'm wondering if anyone knows any php for getting desired height of an image when the width is known. Been checking Google, but it's barren on this topic. Only finding resizing images, and other calculations, lol.
    SEMrush
    Thanks,
    Jeremy.
     
    Jeremy Benson, Aug 8, 2017 IP
    SEMrush
  2. GFX2

    GFX2 Well-Known Member

    Messages:
    764
    Likes Received:
    79
    Best Answers:
    8
    Trophy Points:
    125
    #2
    Does this help you?
    http://php.net/manual/en/function.getimagesize.php
    Code (markup):
     
    GFX2, Aug 8, 2017 IP
  3. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #3
    You need to define what desired height means first. Actual height? Some imaginary height?
     
    Blank ™, Aug 9, 2017 IP
    sarahk likes this.
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    If any dimension of an image is known (assuming that the image is some form of four-corner with 90 degree angles), getting whatever else you want is trivial. Depends entirely on what you WANT. Keeping the ratio is simple, so is getting the actual dimensions from the image itself. It becomes slightly trickier if you want the image to fit into a pre-defined space of some proprietary size. Then we would need a bit more information.
     
    PoPSiCLe, Aug 9, 2017 IP
  5. Jeremy Benson

    Jeremy Benson Active Member

    Messages:
    349
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Lol, sorry guys. I didn't mean to write that way. Was stressed right out. I have masonry divs and I know the widths the images should be, but I need to fetch the height. I understand using getimagesize, but not the mathematical calculations.

    aspect ratio = original width ÷ original height

    adjusted width = <user-chosen height> * aspect ratio

    adjusted height =

    I need adjusted height I think. Imagine users are popping in any image size. I'm setting the width by hand for the width of masonry div, knowing what they need to be, but not the height. The height will have to be calculated to keep aspect ratio.

    As a side note. I'm just wondering is there a way to get an image size when set to 100%, or would I have to fetch it from the attribute using jquery?
     
    Jeremy Benson, Aug 14, 2017 IP
  6. Jeremy Benson

    Jeremy Benson Active Member

    Messages:
    349
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #6
    Hey, I've got some info here.

    Check this image for the problem right now. The image should go in at a width of 256, and the height for the image needs to be calculated to keep aspect ratio.

    https://imgsafe.org/image/3281677210

    This is the image part of my code.

    $dimensions = getimagesize($sleuths[$key]['link']);
                                           
                                            if($dimensions[0] > 256)
                                            {
                                                // find a new height for the image   
                                                    // keep aspect ratio
                                                   
                                                    $aspect = $dimensions[0] / $dimensions[1];
                                                    $height = 256 * $dimensions[1] / $dimensions[0];
                                                   
                                                    echo '<div style="text-align:center;">
                                                            <img src="'.$sleuths[$key]['link'].'" width="'.$dimensions[0].'" height="'.$height.'" />
                                                        </div>';
                                               
                                            }else{
                                               
                                                echo '<div style="text-align:center;">
                                                            <img src="'.$sleuths[$key]['link'].'" width="'.$dimensions[0].'" height="'.$dimensions[1].'" />
                                                </div>';
                                               
                                            }
    Code (markup):
     
    Jeremy Benson, Aug 15, 2017 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Shouldn't it be enough to just do
    
    $height = 256 * $aspect;
    
    Code (markup):
    Albeit you need to switch the $dimension[0] / $dimension[1] to $dimension[1] / $dimension[0];

    Just did a quick test with this code:
    
    $dimension_h = 512;
    $dimension_v = 1024;
    
    $aspect = $dimension_h / $dimension_v;
    
    echo $new_h = 256 * $aspect;
    
    PHP:
    And it outputs 128, which is correct. You might need to do floor or something, depending on what numbers you want to use, but seems to be working? Note that this might not always work (you would need some sort of determination to know which dimension to use for what, depending on what you want to do).
     
    PoPSiCLe, Aug 15, 2017 IP
  8. Jeremy Benson

    Jeremy Benson Active Member

    Messages:
    349
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #8
    This seems to work fine, I had to put 256 in the image width attribute, which was one error. Thanks for the aid. I'm wondering, the second part is a bit weird, but haven't tested it.

    Is there a better way of handling what's in the else statement?

    if($dimensions[0] > 256)
                                            {
                                                // find a new height for the image   
                                                    // keep aspect ratio
                                                   
                                                    $aspect = $dimensions[1] / $dimensions[0];
                                                    $height = floor(256 * $aspect);
                                                   
                                                    echo '<div style="text-align:center;">
                                                            <img src="'.$sleuths[$key]['link'].'" width="256" height="'.$height.'" />
                                                        </div>';
                                               
                                            }else{
                                               
                                                echo '<div style="text-align:center;">
                                                            <img src="'.$sleuths[$key]['link'].'" width="'.$dimensions[1].'" height="'.$dimensions[0].'" />
                                                </div>';
                                               
                                            }
    Code (markup):
     
    Jeremy Benson, Aug 15, 2017 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Shouldn't the dimensions in the else-statement be switched? Seems height is in the width-declaration, and vice versa?
     
    PoPSiCLe, Aug 16, 2017 IP
  10. Jeremy Benson

    Jeremy Benson Active Member

    Messages:
    349
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #10
    I need some help on the math. I'm seeing aspect ratio = width / height. I thought width was stored in [0] element of imagegetsize. In the aspect calculation $aspect = $dimensions[1] / $dimensions[0]; it looks like aspect = height / width, which works fine as you gave it. I tested and looks good. In that sense, you are right about changing

    <img src="'.$sleuths[$key]['link'].'" width="'.$dimensions[1].'" height="'.$dimensions[0].'" />
    Code (markup):
    To

    
    <img src="'.$sleuths[$key]['link'].'" width="'.$dimensions[0].'" height="'.$dimensions[1].'" />
    
    Code (markup):
    But it seems logically wrong with the expressions I'm finding on the web for calculating aspect ratio, lol.

    Other than that, is centering the image when less width than 256 the only other option?
     
    Jeremy Benson, Aug 16, 2017 IP
  11. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #11
    I find it hard to understand the issue.

    Aspect ratio = width / height. If height or width needs to change, you just multiply the corresponding parameter with the aspect ratio. For example:

    
    $originalHeight = 400;
    $originalWidth = 250;
    
    $aspectRatio = $originalWidth / $originalHeight;
    
    $desiredHeight = 200;
    
    $adjustedWidth = $desiredHeight * $aspectRatio;
    Code (php):
     
    Blank ™, Aug 16, 2017 IP
  12. Jeremy Benson

    Jeremy Benson Active Member

    Messages:
    349
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    73
    #12
    What about a desired width?

    $originalHeight = 400;
    $originalWidth = 250;
    
    $aspectRatio = $originalWidth / $originalHeight;
    
    $desiredWidth = 256;
    
    $adjustedHeight = $desiredWidth * $aspectRatio;
    Code (markup):
     
    Jeremy Benson, Aug 16, 2017 IP
  13. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #13
    The same, just swap height and width variables.
     
    Blank ™, Aug 16, 2017 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,399
    Likes Received:
    1,875
    Best Answers:
    245
    Trophy Points:
    515
    #14
    This reeks of flawed thinking and methodology top to bottom since:

    1) you're using PHP to do CSS' job

    2) you're slopping style into the markup

    3) you've got a bunch of DIV for nothing.

    http://thenewcode.com/844/Easy-Masonry-Layout-With-Flexbox

    The only 'problem' is they won't always line up to a perfect height unless you figure that out BEFORE you even serve the IMG tags, but it's still a far better solution than throwing PHP at something that's really none of it's business (aka LAYOUT).
     
    deathshadow, Aug 17, 2017 IP