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. Thanks, Jeremy.
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.
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?
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):
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).
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):
Shouldn't the dimensions in the else-statement be switched? Seems height is in the width-declaration, and vice versa?
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?
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):
What about a desired width? $originalHeight = 400; $originalWidth = 250; $aspectRatio = $originalWidth / $originalHeight; $desiredWidth = 256; $adjustedHeight = $desiredWidth * $aspectRatio; Code (markup):
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).