JS dynamically get picture width and height pixel value __js

Source: Internet
Author: User

One, background
1, the background datasheet in the design, for each picture, there is no record of its width and height of the pixel value.
2, picture width and height have different size, for example, a type of picture is 700*500 pixels, B type of picture is 700*1000 Pixel
3, in the foreground there is a 700*500 pixel of a fixed size picture display area, used to display a variety of size pictures
4, a type of picture normal display, B type of graph limit high display, that is, displayed as a 350*500 pixel
5, can not use other fields to distinguish between a-type picture and B-type pictures (if you are designing a datasheet when the width and height of the picture should be recorded ...).

Two, workaround
you need to use JS in the foreground based on the image width and height of the pixel value, to determine whether the picture is a type A or B type.
Consider using AJAX to load asynchronously, calculate the pixel values for width and height after the picture has been successfully loaded, and then set the width and height properties of the picture in the HTML img tag. The
code is as follows:

/** The pixel value of the width and height of the picture dynamically * * @param surl the URL * of the picture @param fcallback callback function, fcallback at least one parameter of type Object type is used to receive the picture's width, height, url * * USAG E: * var url = "Http://mat1.gtimg.com/datalib_img//11-05-26/c/c21ff1138e7349859b49b99312d05baf.jpg"; * FGETIMG (URL, Function (IMG) {alert (' Width: ' +img.width+ '; height: "+img.height+"; URL: "+img.url);}"; * */var fgetimg = function (sURL, fcallback) {var img = new Image (); img.src = sURL + '? t= ' + math.random ()//ie, Ajax will slow down Saves, causing the onreadystatechange function to not be triggered, so you need to add a random number if (Fbrowser.isie) {img.onreadystatechange = function () {if (this.readystate = = "Loaded" | | this.readystate== "complete") {Fcallback ({width:img.width, height:img.height, Url:surl});}; }else if (Fbrowser.isfirefox | | Fbrowser.issafari | | Fbrowser.isopera | | Fbrowser.ischrome) {img.onload = function () {Fcallback ({width:img.width, height:img.height, Url:surl});};} else {fcal Lback ({width:img.width, height:img.height, url:surl}); } };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.