How to control the image size on the Web - I have experienced everyone knowing that the colorful web page is inseparable from the support of the picture, the image has increased the vividness of the webpage, on the other hand, increase the web size, make the download speed Slow down. How to make a picture can be displayed on the page at an appropriate size and become a mystery that I can't solve.
Not long ago, I developed a teaching research website for the unit. In the news management system, in the newsletter, you need to call the image file from the database as a picture in the picture news, thereby forming a text winding form. Since the entire system includes news upload, news editing, system announcement, etc. is dynamically realized. In the development of the process, I consider the following factors: (1) as the maintainer of the website, the operation of the computer is not very skilled; (2) The website is reasonable, and the content is enriched. Therefore, for the design, the website page, the image news has a certain demand, the width of the picture, the width of this website cannot exceed 200px, even though I understand, we can use photoshop and other image processing software, you can handle the picture in advance Ok, upload it to the server, reach the same effect of the home page call, but as a user, the more hope is the simpler, so I am determined to break this problem. After a few days of fierce battle, I will constantly find the way to solve the problem, and continue to test until success. At this point, I wrote some of my experiences, and I have been engaged in the script preparation.
To make the picture display in an appropriate size, the essence is a problem with a large picture. Of course, the small picture cannot be enlarged in the web page, otherwise the image distortion will appear. How to get the size of the image through the image of the image (width, height) is the key to the problem, I will go to the online search to use JavaScript to write image equivalents, etc., and I have not worried, I finally found "adaptive picture size. The pop-up window "One article, the page effect is, click the text hyperlink, the page pops up a new window, showing pictures, where the form size is equivalent to the picture, the article is dynamically loaded with the JavaScript: Image () object, get pictures The height and width, according to the height of the source picture, the width setting pop-up window is high and width of the width of the pop-up window, and open the window, the main code is as follows:
Test one:
hEAD>
Var imgobj;
Function Checkimg (Turl, WinName) {
// Whether the object has been created
IF (typeof (imgobj) == "Object") {// Has a height and width of the image
IF (IMGOBJ.WIDTH! = 0) && (imgobj.height! = 0)))
/ / Open the height and width of the pop-up window according to the acquired image height and width, and open the window
// The increments 20 and 30 are the spacer between the settings of the window border and the picture.
OpenFullsizeWindow (Turl, WinName, ", Width =" (IMGOBJ.WIDTH 20) "Height =" (IMGOBJ.HEIGHT 30);
Else
// Because the image is dynamically loaded by the image object, it is impossible to get the width and height of the picture immediately, so repeat the call check every 100 milliseconds.
Settimeout ("Checkimg ('" theurl ",'" winname "" ", 100)
}
}
Function OpenFullsizeWindow (Turl, WinName, Features) {
Var anewwin, sbasecmd;
// pop up the window appearance parameters
Sbasecmd = "Toolbar = no, location = no, status = no, menubar = no, scrollbars = no, resizable = no,"
/ / Call from Checkimg
IF (Features == Null || features == "") {
// Create an image object
IMGOBJ = new image ();
/ / Set the image source
IMGOBJ.SRC = theurl;
/ / Start getting image size
Checkimg (THEURL, WINNAME)
}
Else {
// Open the window
Anewwin = window.open (Theurl, WinName, Sbasecmd Features);
/ / Focus window
Anewwin.focus ();
}
}
// ->
script>
body>
html>
With image () objects to get the width, Height, I know how to solve how to achieve the proportional zoom problem that implements image. So, I use K = Width / Height to represent the proportional value of the image, when K> = 1, indicates width> heght, as long as Width does not exceed 200px, Height must <= 200px; opposite K <1, as long as height does not exceed 150px, width must <= 150px (Width / height = 4: 3). Therefore, as long as K> = 1 defines width, K <1 defines Height. With the help of the test one, I will soon have the following code (test 2):
Test two