Several ways to get picture size by URL: js and PHP

Source: Internet
Author: User
Tags php tutorial
The first is the JS method, through the new Image object, set the Src property, and listen to the complete and onload events, the picture after the completion of loading the output width and height

function Checkpicurl (URL) {

var img = new Image ();
img.src = URL;
Img.onerror = function () {
Alert (name+ "picture failed to load, please check the URL is correct");
return false;
};

if (img.complete) {
Console.log (img.width+ "" +img.height);
}else{
Img.onload = function () {
Console.log (img.width+ "" +img.height);
Img.onload=null; Avoid repeated loading
}
}

}

Php Method:

The use of PHP is because of the need to put in a batch of data, the URL field value of the data is not tiled picture width and high information, and the demand is to record, so that the table from the recognition of the image is not the width of the data, and to obtain each image URL size information, in the update to the table. The Java implementation is relatively cumbersome, and PHP provides the getimagesize function, you can get local and network image size information, the function is described as follows:

GetImageSize: Get the picture's length and width.
Syntax: array getimagesize (string filename, array [imageinfo]);
return value: Array
Function type: Graphics processing
Content Description
This function can be used to obtain the height and width of GIF, JPEG and PNG three WWW slices, and it is not necessary to install GD library to use this function. The returned array has four elements. The first element of the returned array (index value 0) is the height of the picture, in pixels (pixel). The second element (index value 1) is the width of the picture. The third element (index value 2) is the file format of the picture, with a value of 1 in GIF format, 2 in jpeg/jpg format, and 3 in PNG format. The fourth element (index value 3) is the height and width of the picture string, Height=xxx width=yyy. The omitted parameter imageinfo is used to obtain some information about the picture, such as the APP13 mark of the IPTC (HTTP://WWW.XE.NET/IPTC), which can be added to the picture, and can be parsed using iptcparse ().
Usage examples
function Myimg ($imgfile) {
$size = getimagesize ($imgfile);
echo "";
}
Myimg ("Img/img1.gif");
Myimg ("Http://img02.sogoucdn.com/app/a/100520052/5a1f885e25b7af28822b14dc069a5f16");
?>

The above describes several ways to get the image size by URL: js and PHP, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.