Dear Monks,

The essence of this question is: what could cause two virtually identical pieces of code to be behaving differently?

The first piece, a complete script, is this:

#!/usr/bin/perl -- use File::Spec; use Image::Magick; my $imbase = Image::Magick->new(magick=>'png') or die("Image creation +failed."); # Read the predefined letter PNG for each letter my $x = $imbase->Read(map { File::Spec->catfile('/my/web/httpd/htdocs/ +mt/images/captcha-source/',$_.'.png') } (a..z)); if ($x) { die($x); }
The second piece, (an excerpt from a module), is this:
require Image::Magick; my $imbase = Image::Magick->new(magick=>'png') or return $app->error($app->translate("Image creation failed.")); # Read the predefined letter PNG for each letter in $code my $x = $imbase->Read(map { File::Spec->catfile($base, $_ . '.png') } +split(//, $code)); if ($x) { return $app->error($app->translate("Image error: [_1]", $x)); }
The first piece runs from the command line without error.

The second piece, running in a web browser, returns this error:

close Image error: Exception 425: Corrupt image `/my/web/httpd/htdocs/mt/images/captcha-source/k.png'

I've done some debugging, and have confirmed that:
- The image file in question is valid
- There is only one copy of Image::Magick on the system
- There are 2 copies of File::Spec, but I tested both in the first script, and neither caused an error

So I'm wondering: what else could cause the error? I'm not aware of any possible cause to make these two code snippets respond differently.

The module in question is from a third party piece of open source software (Movable Type Open Source). I have pasted the complete subroutine
sub _generate_captcha { my $self = shift; my ($app, $code, $format) = @_; $format ||= 'png'; my $len = LENGTH(); my $cfg = $app->config; my $base = $cfg->CaptchaSourceImageBase; unless ($base) { require File::Spec; $base = File::Spec->catfile(MT->instance->config_dir, 'mt-stat +ic', 'images', 'captcha-source'); $base = undef unless (-d $base); } return $app->error($app->translate('You need to configure CaptchaS +ourceImageBase.')) unless $base; require Image::Magick; my $imbase = Image::Magick->new(magick=>'png') or return $app->error($app->translate("Image creation failed." +)); # Read the predefined letter PNG for each letter in $code my $x = $imbase->Read(map { File::Spec->catfile($base, $_ . '.png' +) } split(//, $code)); if ($x) { return $app->error($app->translate("Image error: [_1]", $x)); } # Futz with the size and blurriness of each letter foreach my $i (0..($len - 1)) { my $a = int rand int(WIDTH() / 14); my $b = int rand int(HEIGHT() / 12); $imbase->[$i]->Resize(width => $a, height => $b, blur => rand( +3)); } # Combine all the individual tiles into one block my $tile_geom = join('x', $len, 1); my $geometry_str = join('x', WIDTH(), HEIGHT()); my $im = $imbase->Montage(geometry => $geometry_str, tile => $tile_geom); $im->Blur(); # Add some lines and dots to the image for my $i (0..($len * WIDTH() * HEIGHT() / 14+200-1)) { my $a = int rand($len * WIDTH()); my $b = int rand HEIGHT(); my $c = int rand($len * WIDTH()); my $d = int rand HEIGHT(); my $index = $im->Get("pixel[$a, $b]"); if ($i < ($len * WIDTH() * HEIGHT() / 14+200) / 100) { $im->Draw(primitive => 'line', stroke => $index, points => "$a, $b, $c, $d"); } elsif ($i < ($len * WIDTH() * HEIGHT() / 14+200) / 2) { $im->Set("pixel[$c, $d]" => $index); } else { $im->Set("pixel[$c, $d]" => "black"); } } # Read in the background file my $a = int rand(5) + 1; my $background = Image::Magick->new(); $background->Read(File::Spec->catfile($base, 'background' . $a . ' +.png')); $background->Resize(width => ($len * WIDTH()), height => HEIGHT()) +; $im->Composite(compose => "Bumpmap", tile => 'False', image => $background); $im->Modulate(brightness => 105); $im->Border(fill => 'black', width => 1, height => 1, geometry => join('x', WIDTH() * $len, HEIGHT())); my @blobs = $im->ImageToBlob(magick=>$format); return $blobs[0]; }
I'm not asking for full debugging (though if you happen to know the cause that's great). It's more of a perl theory question for me: what is capable of causing those 2 calls to $imbase->Read to respond differently when it seems to me they should be running identical procedures on identical data?

Thank you!

In reply to Strange behaviour from two pieces of similar code in different running environments by Likeless

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.