English 中文(简体)
How to resize an image in pure HTML/CSS while keeping its proportions?
原标题:

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

最佳回答

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.

I think ImageMagick supports "resize to fit" and "resize to fill" methods, the latter being what you are looking for.

Before writing your img tag to the output buffer, save the image to the resulting proposed file name and let identify extract the width and height of this image for you.

If you don t want to work with a seperate file but instead want the image data inline, try the data-uri method which newer web browsers support. This inlines the binary data stream of the image within the html file, so it is embedded.

You can use phpThumb, to resize and cropping the images on the fly. It uses the GD library to create thumbnails from images JPEG, PNG, GIF, etc.

create helper like:

<?php
echo  <img src="  . image_resize($path_to_image, SIZE_X, SIZE_Y) .  " /> ;
?>

so this helper will check if resized image already created or create new one. and in both cases it returns proper url to the new image.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签