3

The following script runs in a loop, retrieving images using LWP::UserAgent, and resizing them using Image::Magick.

I am getting this error from Image::Magick when reading the downloaded image:

Exception 450: Unsupported marker type 0x54

If I download the LWP-downloaded image to my computer, open it in a photo editor, save as a .jpg file, upload it and attempt to read with Image::Magick then all is fine. This would lead me to believe that the image is not saving correctly.

I need to use LWP::UserAgent because the server I am connecting to won't allow the download unless it thinks a client is requesting the data.

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");  
Borodin
  • 126,100
  • 9
  • 70
  • 144
chrisrth
  • 1,182
  • 3
  • 15
  • 36

3 Answers3

5

Never use

$response->content()

You want

$response->decoded_content( charset => 'none' )
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I agree. But I think you should explain your answer – Borodin Dec 30 '12 at 20:41
  • @Borodin, "Because that's what returns what you want" – ikegami Dec 30 '12 at 20:46
  • changed to decoded_content as in answer but still no good. I printed the content_type to verify I was getting image/jpeg. I am guessing this must be related to the way the website is presenting the image however if I download to my computer directly with chrome and upload to the server all is fine. I have been able to mirror images from this site before with a different URL structure. not sure what else I can do. I can pull the images up from the server but I cannot read to resize. – chrisrth Dec 30 '12 at 21:12
  • I am sure you know better. An answer like that is comparable with a question that says "It doesn't work" – Borodin Dec 30 '12 at 21:12
  • @chrisrth: Have you looked at the saved image file using a binary editor? It would help to know what sort of differences there are in the faulty version of the file. You may be getting HTML, which will be obvious, but at least the [FourCC](http://en.wikipedia.org/wiki/FourCC) should be right. It is likely that the `Referer` header of the request needs to be correct for the server to send what you need. – Borodin Dec 30 '12 at 21:18
  • I can open the UserAgent downloaded image in the browser and the image appears fine, I just can't read it with ImageMagick. Looking into a binary editor. – chrisrth Dec 30 '12 at 21:37
  • @chrisrth, What if you save the image using the browser and pass it I::M? Do you see the image file when you open the file you saved with a your browser? Could be that the browser is ignoring the error, or that the image requires the support of some feature I::M doesn't. – ikegami Dec 30 '12 at 22:56
  • @Borodin, Not it's not. When someone says "`ord` doesn't print", the answer is "Indeed. Use `print`". I really don't know what else I can say. – ikegami Dec 30 '12 at 22:57
  • if you save the useragent-downloaded image as a file and also get the file as fetched directly from a browser, what differences do you see? it almost sounds like the image has extra data at the end that the browser is ignoring but imagemagick (correctly?) complains about – ysth Dec 30 '12 at 23:19
2

You are probably getting a compressed or otherwise encoded result; try ->decoded_content instead of ->content.

From the HTTP::Response doc:

$r->content( $bytes )

This is used to get/set the raw content and it is inherited from the HTTP::Message base class. See HTTP::Message for details and other methods that can be used to access the content.

$r->decoded_content( %options )

This will return the content after any Content-Encoding and charsets have been decoded. See HTTP::Message for details.

ysth
  • 96,171
  • 6
  • 121
  • 214
0

I know this very old at this point, but I just ran into this as well, and I was actually saving the image to disk before working with it and while doing that, I needed to set the file handle that I was streaming/writing to to 'binmode'.

open $fh....
binmode($fh)
print $fh .....
close $fh
Steven
  • 54
  • 5