There is no format suffix for network pictures, how to use PHP to get the picture format correctly

For example: http://inews.gtimg.com/newsapp_match/0/8189513143/0
The url above is a link to a picture, if you want to download and save the picture through php for calling

In fact, you can try to use the PHP getimagesize()function to get the image format, and then use file_get_contents()the function to download the image and save it.
The sample code is as follows:

$url = 'http://inews.gtimg.com/newsapp_match/0/8189513143/0';
$size = getimagesize($url);
if($size) {
    
    
$extension = image_type_to_extension($size[2]); // 获取图片扩展名
$filename = 'image'.$extension; // 设置图片文件名
$content = file_get_contents($url); // 下载图片内容
file_put_contents($filename, $content); // 保存文件
}

First use getimagesize()the function to get the picture information, and then use image_type_to_extension()the function to get the picture extension. Finally, use file_get_contents()a function to download the image content, and file_put_contents()a function to save the file. Note that the file name and save path need to be modified according to the specific situation.
as the picture shows:
insert image description here

Guess you like

Origin blog.csdn.net/qinmin1/article/details/129496494