How can I resize an image using HTML/CSS only (i.e no server code) while keeping its proportions and have a crop effect. Details: I wish to resize it to a specified width, keep the proportions and if the height is bigger than a specified value to crop it to the specified height ?
Actually, I know how to do that using some server code but I don't want to do this. It would imply using a different file reference and refer the picture something like <img src="picture.php" />
. I need to process the image in the same page that displays it.
What "bothers" me is that I have to send the image header so nothing else on the page will be displayed.
Is there a way to do something like that? Maybe from pure HTML/CSS? :P
I made a function that does something like that (except that "crop" thing) and it returns just width="" height=""
and I use it like this <img src="image.jpg" resize("image.jpg") />
, but I don't get how can i do that crop...
Thanks
The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.
Prevent an image from appearing stretched by locking the aspect ratio when changing the width or height. With the lock activated, Snagit maintains the image's original proportions. Now, you can adjust the height or width of your image to the desired dimensions without stretching or warping it.
Resize images with the CSS width and height properties Another way of resizing images is using the CSS width and height properties. Set the width property to a percentage value and the height to "auto". The image is going to be responsive (it will scale up and down).
Option 2: Resize with the max-width property By setting the max-width at 100%, the image will now fit to the full width of the container regardless of how narrow or wide the screen is. That said, in order to prevent image distortion, you'll need to set the height to auto.
ok, so i managed to find a way to do that from html/css. The whole idea was to get rid of that "extraheight" :
<div style="width:200px;height:200px;overflow:hidden;" >
<img src="image.jpg" width="200px" height="auto">
</div>
first my image is resized to desired width(keeping the proportions) and then giving a fixed height to containing div and setting overflow to hidden
makes the script to display just the desired portion of the image.
You can't render an image and HTML in the same file because browsers depend on its content-type
header to interpret it.
The content-type
header of a HTML document is generally set to text/html
whereas the content-type of an image is image/*
(substitute * for image format). You could print image data to a HTML document, but since the browser is instructed to interpret it as text/html
rather than an image its contents would be garbled.
You will need to link your <img>
tag to a PHP file like you described. I'm not sure why that constitutes a problem; it's the right way to go about it. You could of course crop the image by manipulating its dimensions in HTML, but I don't recommend you do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With