从 Web 检索图像后,我在缓冲区中有图像:
my $img = $webapi->get('http://myserver.com/image/123423.jpg');
调用后,原始数据在 $img 中。我想确保数据代表图像而不是文本,因此我将其保存到磁盘上的文件并运行文件命令:
open my $fh, '>', '/tmp/images/rawdata.bin';
print $fh $img;
close $fh;
$res = `file /tmp/images/rawdata.bin`;
if ($res =~ 'GIF|JPEG|PNG') print "Image";
else "Not image";
如何避免将原始数据保存到文件并在内存中完成工作?