PHP code to get the size and size of local and remote pictures

user:visitors Date:2021-11-03 16:44:485932

When using php code to manipulate pictures, you generally need to get the parameters such as the length and height of the picture first, so what are the commonly used methods to view the size of the picture?

1. In the php code, there is a built-in function getimagesize to get the image size, this function is a relatively simple function

The getimagesize function can return the size and file type of any image file such as jpg, jpeg, gif, swf, swc, psd, bmp, tiff, iff, jpc, jpx, jp2, xbm, wbmp, etc., and the height= that can be used for the img tag "" width="" characters.

code show as below:

<?php

$picarr = getimagesize("picture.jpg");

?>

Output array: Array([0] => 500,[1] => 300,[2] => 2,[3] => width="500" height="300",[bits] => 8,[ channels] => 3,[mime] => image/jpeg)

The result obtained is an array: $picarr[0] is the obtained picture width, $arr[1] is the picture height, $arr[2] is the picture format: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, $arr[3] is a string of width and height: width="" height=""

$picarr['bits'] is the number of bits and binary format of each color of the picture, $picarr['channels'] is the channel value of the picture, $picarr['mime'] is the MIME information of the picture

When the getimagesize function successfully obtains the image information, it will automatically return an array. If it fails, it will return FALSE and generate an error message. It can obtain the basic information of the local and remote images, but you need to use curl and other functions to obtain the remote image information. Get whether the remote picture exists, otherwise it will slow down the program.

2. Imagesx() function and imagesy() function

Before using imagesx and imagesy, you need to use functions such as imagecreatefromjpeg, imagecreatefrompng, and imagecreatefromgif to return image resources.

<?php

$pictupian = imagecreatefromjpeg("Picture.jpg");

echo "The width of the picture:".imagesx( $pictupian );

echo "The height of the picture:".imagesy( $pictupian );

?>

The returned result can directly output the width and height of the image.

3. The above are all the obtained image sizes, so how do I obtain the image size?

There are many ways to get the size of a picture, here are some commonly used ones.

filesize() function

$filename="picture.jpg";

echo "File size is".filesize($filename) /1000;

The get_headers function, relative to the filesize, it can get the size of the remote picture file

$tuhead = get_headers("picture.jpg",true);

echo "Image size".$tuhead['Content-Length'];

Use fread mode or use socket binary mode to read

$wenjian = fopen("picture address",'rb');

$tumdata = stream_get_meta_data($wenjian);

//apache is directly in wrapper_data, nginx is in headers

$tuinfo = isset($tumdata['wrapper_data']['headers'])? $tumdata['wrapper_data']['headers']: $tumdata['wrapper_data'];

foreach ($tuinfo as $ddd) {

if (preg_match('/length/iU', $ddd)) {

$huoqusize = explode(':', $ddd);

echo trim(array_pop($huoqusize));

break;

}

}

fclose($wenjian); //Close the file

First, fopen opens the picture at the specified address to get the file data stream information, stream_get_meta_data gets the head information. The nginx information is in the headers and apache is in the wrapper_data, so after the judgment is $tuinfo, the file size information is extracted through the regular, and then the useless characters are filtered out. The size of the picture file can be obtained.

Popular articles
latest article