my $white; # Create background my $image = new GD::Image(600, 450); $white = $image->colorAllocate(255, 255, 255); # Resize uploaded image to 600 wide my $picture = GD::Image->new($file{'image', 'file'}); my ($srcw, $srch) = $picture->getBounds(); my $newh = ($srch * 600 / $srcw) - 1; my $resize = GD::Image->new(599, $newh - 1); $resize->copyResized($picture, 0, 0, 0, 0, 600, $newh, $srcw, $srch); # Copy onto background image offset to crop or center $image->copy($resize, 0, 0, 0, ($newh - 450) / 2, 600, 450); $white = $image->colorAllocate(255, 255, 255); open my $fh, '>' ,"$root/images/property/unit/$filename.png"; binmode $fh; print $fh $image->png; close $fh;

The background is set to white but it nearly always comes out as black and occasionally dark green.

So, you bought a can of white color and a brush, but for some strange reason, the walls of the room you are standing in still show a nasty 1960s style flower decor. What has gone wrong?

Hint: $white, as shown in your code, is a write-only variable. You assign to it, twice, but you do not use it.


When $white is checked it is always a positive number. 16777215 for colorAllocate(255, 255, 255) and different positive numbers for different colours.

Sure. 16777215 is 0x00FFFFFF, RGB for white. Other colors use other bit patterns. colorAllocate() is mainly needed for paletted image formats (like GIF), that can only display a limited selection ("palette") of colors. For those formats, colorAllocate() allocates one of the few possible palette entries (trueColor in GD speak is off). Other image formats allow using three (or four) color channels, each with a color depth of typically 8 bit (more or less bits are possible), and for those formats, colorAllocate() just translates RGB to a bit pattern (trueColor in GD speek is on).

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: GD colorAllocate not changing colour by afoken
in thread GD colorAllocate not changing colour by Bod

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.