[英]Image corruption using LWP::UserAgent
以下脚本循环运行,使用LWP::UserAgent
检索图像,然后使用Image::Magick
调整Image::Magick
大小。
读取下载的图像时,我从Image::Magick
得到此错误:
Exception 450: Unsupported marker type 0x54
如果我将下载LWP的图像下载到计算机上,请在照片编辑器中将其打开,另存为.jpg文件,然后上传并尝试使用Image::Magick
进行读取,那么一切都很好。 这会使我相信该图像未正确保存。
我需要使用LWP::UserAgent
因为我连接的服务器不允许下载,除非它认为客户端正在请求数据。
use LWP::UserAgent;
use Image::Magick;
$ua = new LWP::UserAgent;
$ua->agent("$0/0.1 " . $ua->agent);
$ua->agent("Mozilla/8.0");
my $PICURL ="http://www.example.com/img.aspx?pid=cjfsaf79afffafhfah777af7";
my $PICDEST ="/var/vhosts/mysite.com/httpdocs/images";
my $PICNAME ="01.jpg";
my $response = $ua->get("$PICURL");
open(outfile, ">:raw", "$PICDEST/$PICNAME");
binmode outfile;
if ($response->is_success) {
print outfile $response->content;
$Pi++;
$PTOT++;
}
else {
die $response->status_line;
}
$image = new Image::Magick;
$image->Read("$PICDEST/$PICNAME");
$image->Scale(width=>800, height=>600);
$image->Write("$PICDEST/$PICNAME");
$image->Scale(width=>216, height=>163);
$image->Set(quality=>90);
$image->Write("$PICDEST/TH_$PICNAME");
永远不要使用
$response->content()
你要
$response->decoded_content( charset => 'none' )
您可能会得到压缩或以其他方式编码的结果; 请尝试-> decoded_content,而不是-> content。
从HTTP :: Response文档:
$ r-> content($ bytes)
这用于获取/设置原始内容,它是从HTTP :: Message基类继承的。 有关详细信息和可用于访问内容的其他方法,请参见HTTP :: Message。
$ r-> decoded_content(%options)
在对任何Content-Encoding和字符集进行解码之后,这将返回内容。 有关详细信息,请参见HTTP :: Message。
我现在知道这很老了,但是我也碰到了这一点,实际上我是在将映像保存到磁盘之前使用它的,而这样做的时候,我需要设置要流/写入的文件句柄到“ binmode”。
open $fh....
binmode($fh)
print $fh .....
close $fh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.