Home > Backend Development > PHP Tutorial > Several ways to get image size through url: JS and php

Several ways to get image size through url: JS and php

WBOY
Release: 2016-08-08 09:29:13
Original
1249 people have browsed it

The first is the js method. Use a new Image object, set the src attribute, and listen to the complete and onload events. After the image is loaded, the width and height of the image are output.

function checkPicurl(url){

var img = new Image ();
img.src = url;
img.onerror = function(){
alert(name+" Image loading failed, please check if 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:

Use PHP because there is such a demand: a batch of data has been stored in the database. The url field value of these data does not have information about the width and height of the spliced ​​image, and the demand is to record it, so it must be identified from the table There is no data on the width and height of the puzzle pieces, and the size information of each image URL is obtained separately, and then updated to the table. Implementation through java is relatively cumbersome, and PHP provides the GetImageSize function, which can obtain the size information of local and network images. The function description is as follows:

GetImageSize: Get the length and width of the image.
Syntax: array getimagesize(string filename, array [imageinfo]);
Return value: array
Function type: Graphics processing
Content description
This function can be used to get the height and width of GIF, JPEG and PNG images on the WWW. You can use this function without installing the GD library. The returned array has four elements. The first element of the returned array (index value 0) is the height of the image, in pixels. The second element (index 1) is the width of the image. The third element (index value 2) is the file format of the image, with a value of 1 for GIF format, 2 for JPEG/JPG format, and 3 for PNG format. The fourth element (index value 3) is the height and width string of the image, height=xxx width=yyy. Omissible parameters imageinfo Used to obtain some picture-related information, such as the APP13 tag of IPTC (http://www.xe.net/iptc), which can be added to the picture and can be parsed using iptcparse().
Usage example
function MyImg($imgfile) {
$size = GetImageSize($imgfile);
echo "";
}
MyImg("img/img1.gif");
MyImg("http://img02.sogoucdn.com/app/a/100520052/5a1f885e25b7af28822b14dc069a5f16");
?>

The above introduces several methods of obtaining image size through URL: JS and PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template