in reply to Re: GD colorAllocate not changing colour
in thread GD colorAllocate not changing colour
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?
OK - so the handyman has been summoned and instructed to use the brush to put the paint on the wall:
The wall looks much better in white than it did in the 1960's flower decor. However, much to the handyman's frustration, I've decided that for testing purposes I don't want white or black and will have grey instead. Grey makes it easy to know the black has gone without the white blending into the background:my $white; # Create background my $image = new GD::Image(600, 450); $white = $image->colorAllocate(255, 255, 255); $image->filledRectangle(0, 0, 600, 450, $white);
Calling the grey $white ensures that the handyman knows he has to return the grey wall back to white someday!$white = $image->colorAllocate(127, 127, 127); $image->filledRectangle(0, 0, 600, 450, $white);
Now that there is a nice grey wall we can look at it and check it is indeed grey:
And yes, it is grey as expected :)open my $fh, '>' ,"$root/images/property/unit/$filename.png"; binmode $fh; print $fh $image->png; close $fh;
Next we get the handyman to take the picture that was hanging on the wall and resize it to it is 600px wide. Being good with a tape measure and a saw, our handyman is able to resize the picture without altering it's aspect ratio:
Despite having lots of faith in our handyman it's still a good idea to check his work occasionally...# Resize uploaded image to 600 wide my $picture = GD::Image->new($file{'image', 'file'}); my ($srcw, $srch) = $picture->getBounds(); $newh = int ($srch * 600 / $srcw); my $resize = GD::Image->new(600, $newh); $resize->copyResized($picture, 0, 0, 0, 0, 600, $newh, $srcw, $srch);
And sure enough his tape measure and sawing skills live up to expectations and he has created a perfect resized picture all ready for the newly painted grey wall.open my $fh, '>' ,"$root/images/property/unit/$filename.png"; binmode $fh; print $fh $resize->png; close $fh;
Now for the final part of the redecoration. All that is left is to hang the picture of the wall and we are done.
All done but let's have a quick check again before the hammer is put away...# Copy onto background image offset to crop or center $image->copy($resize, 0, 0, 0, ($newh - 450) / 2, 600, 450);
Oh no!open my $fh, '>' ,"$root/images/property/unit/$filename.png"; binmode $fh; print $fh $image->png; close $fh;
As if by magic, the nice grey wall has turned into a black wall. Far better than the 1960's flower pattern but still not the lovely shade of grey that we tried so hard to use to paint the wall.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: GD colorAllocate not changing colour
by afoken (Chancellor) on May 16, 2021 at 16:53 UTC | |
by Discipulus (Canon) on May 16, 2021 at 17:10 UTC | |
by Your Mother (Archbishop) on May 16, 2021 at 18:38 UTC | |
by Bod (Parson) on May 16, 2021 at 17:06 UTC | |
by Discipulus (Canon) on May 16, 2021 at 17:37 UTC | |
by Bod (Parson) on May 16, 2021 at 17:47 UTC | |
Re^3: GD colorAllocate not changing colour -- working example
by Discipulus (Canon) on May 16, 2021 at 19:05 UTC | |
by Bod (Parson) on May 21, 2021 at 21:13 UTC | |
Re^3: GD colorAllocate not changing colour
by huck (Prior) on May 16, 2021 at 21:34 UTC | |
by Bod (Parson) on May 21, 2021 at 21:20 UTC |