Hi, all,
I'm trying to do something that I would think would be pretty simple
-- I have a web forum and I want to convert uploaded images to an
*appropriate* output type, using some very simple business logic, as
follows:
If the image is of type GIF or MNG and has more than one image/frame
: Define output type as 'gif'
Else
: Flatten it
If the image uses transparency
: Define output type as 'png'
If output type is not defined
: Define output type as 'jpg'
The idea here is that all animated uploads (not MPEGs and stuff --
I'll be using ffmpeg to handle those) should be converted into one of
the three basic web image formats.
- Animated stuff becomes GIF, for animation purposes (I don't trust
browsers to handle MNG)
- Stuff with a used alpha channel becomes PNG, for transparency
purposes (I already have the IE PNG fix JavaScript in place)
- Everything else becomes a JPEG because it's smallest, and doesn't
need animation or opacity
However, I'm running into a problem with the detection of opacity.
I tried this:
my $ani_gif = 0;
if (scalar @{$im} > 1) {
if (grep $imtype eq $_, 'gif', 'mng') {
$ani_gif = 1;
$type = 'gif';
}
}
else {
$im = $im->Flatten;
}
if ($im->Get('alpha') ne 'Off') {
$type = 'png';
}
$type ||= 'jpg';
And everything not animated ends up a PNG. I can upload a JPEG and it
gets turned into a PNG, even though a JPEG should not show up as
having alpha anything but "Off"
So I tried this:
my $ani_gif = 0;
if (scalar @{$im} > 1) {
if (grep $imtype eq $_, 'gif', 'mng') {
$ani_gif = 1;
$type = 'gif';
}
}
else {
$im = $im->Flatten;
}
if ($im->Get('alpha') ne 'Off') {
for my $x (1 .. $im->Get('width')) {
for my $y (1 .. $im->Get('height')) {
my $pixel = $im->GetPixel(geometry => "${x}x$y",
channel=>"Alpha");
$type = 'png' if $pixel;
}
}
}
$type ||= 'jpg';
And with THIS one, everything not animated turns into a JPEG! Even
when I upload PNGs that *specifically* have alpha channels! Some
logging turns out that every GetPixel, even on things I *KNOW* have an
alpha channel with gradients and such, comes out 0
So what's going on here, and how cna I easily and quickly determine if
an image *has and uses transparency* here?
I want users to be able to upload TIFFs and PSDs and such, and make
the ones with transparency into PNGs and the ones with no transparency
into JPGs and this seems like it should be simple.
All I want is a forum that's, you know, intelligent (it bugs me to no
end when I see forums with notes like "Attachments must be no larger
than 500 x 500 pixels, and must be in JPEG format" and then if it
doesn't meet these requirements, you get an error and a note to go
back and fix the errors... because I'm thinking: You people have a
COMPUTER, you know. USE IT and stop trying to make your users open
photoshop every time something's off by a pixel!)
Anyway... any ideas?
--
Dodger
_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users