使用自定义流程调整图像大小

时间:2015-08-02 18:17:10

标签: image perl

我创建了一个小子程序,想知道需要添加什么才能调整几乎任何类型的图像(不包括模糊类型):

######################
sub printImage($) {
  # be sure to do your error checking BEFORE calling this. it'll just
  # blindly rip along.
  my $fn = $_[0];
  my $type = getType($fn); # see sub below
  my $buffer = "";

  print "content-type: image/$type\n"; # these are awful, but ok for now
  print "\n"; # separate just in case we want to add more to the header.

  binmode STDOUT;

  open my $FH, "<", $fn or die "$!";
  while (read ($FH, $buffer, 10240)) {
    print $buffer; # prefer NOT to print as I read...
  }
  close $FH;

  # return $OUTPUT; # this would be better, no?
}

“getType”只查看文件扩展名,并返回扩展名。

我想知道的是如何使用上述功能调整图像大小。我不想安装任何额外的模块或包,或做任何类似的事情。

1 个答案:

答案 0 :(得分:3)

此功能不会操纵图像内容,也不能轻易转换为这样做。调整图像大小要比这复杂得多。

由于您特别要求我们不建议使用其他模块,因此我无需提供任何服务。使用纯Perl代码调整图像大小会非常慢,并且需要的代码远远多于我在答案中可能包含的代码。如果要对图像内容执行操作(例如调整图像大小),请使用图像处理模块,例如GDImage::Magick