如何在URL中检测图像的文件类型?

joe*_*joe 0 url perl image file

如何在Perl表单网站URL中找到图像文件类型?

例如,

$image_name = "logo";
$image_path = "http://stackoverflow.com/content/img/so/".$image_name 
Run Code Online (Sandbox Code Playgroud)

从这个信息中如何找到那个文件类型.这里应该显示的例子

"png"

http://stackoverflow.com/content/img/so/logo.png . 
Run Code Online (Sandbox Code Playgroud)

如果它有更多的文件,如SO网站,则提供支持.它应该显示所有文件类型

fri*_*edo 7

如果您使用LWP获取图像,则可以查看content-typeHTTP服务器返回的标头.

无论WWW ::机械化LWP :: UserAgent的会给你一个HTTP ::响应任何GET请求对象.所以你可以这样做:

use strict;
use warnings;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new;
$mech->get( "http://stackoverflow.com/content/img/so/logo.png" );
my $type = $mech->response->headers->header( 'Content-Type' );
Run Code Online (Sandbox Code Playgroud)


Bri*_*new 5

你不能轻易说出来.URL不一定反映图像的类型.

要获取图像类型,您必须通过HTTP(GET,或更高效,HEAD)发出请求,并检查Content-typeHTTP响应中的标头.