Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Easy Pixel PNG Generator

by bladx (Chaplain)
on Sep 11, 2001 at 07:54 UTC ( [id://111661]=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info bladx - Andy
Description: This is based on a totally random programming idea from Vynce. All this does, is it is a CGI, that lets a user enter in some values for the name of the new generated png that will be created after filling the info. and the hex values for the color of the pixels. It then does the creating of the px png for you, and can be used for generating one color 1x1px png backgrounds as well, for those of you that may need something handy like this, (since I tend to do stuff like this as well.)

Overall, this is a very simple utility, but a fun one to use (or at least resourceful,) if you need to make some quick one color backgrounds ever.
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI;
use GD;

my $query = new CGI;

print $query->header;
print $query->start_html("EasyPX Generator");
print "<H1>EasyPX Generator</H1>\n";
prompt($query);
go_for_it($query);
print $query->end_html;
 
sub prompt {
  my($query) = @_;

  print $query->startform;

  print "Enter Image File Name&nbsp;&nbsp;";
  print $query->textfield('name'),"<br>";

  print "Enter Width&nbsp;&nbsp;";
  print $query->textfield('width'),"<br>";
 
  print "Enter Height&nbsp;&nbsp;";
  print $query->textfield('height'),"<br>";
  
  print "Enter Red Hex Value&nbsp;&nbsp;";
  print $query->textfield('red_value'),"<br>";
  
  print "Enter Green Hex Value&nbsp;&nbsp;";
  print $query->textfield('green_value'),"<br>";

  print "Enter Blue Hex Value&nbsp;&nbsp;";
  print $query->textfield('blue_value'),"<br>";

  print "<p>",$query->reset;
  print $query->submit('Action','Submit');
  print $query->endform;
  print "<hr>\n";
}

sub go_for_it {
  my($query) = @_;
  my(@values,$key);

  print "<H2>Completed with the following values.</H2>";

  foreach $key ($query->param) {
    print "<STRONG>$key</STRONG> -> ";
    @values = $query->param($key);
    print join(", ",@values),"<BR>\n";

    if ($key =~ /name/i) {
      our $name = $query->param($key);
    } elsif ($key =~ /width/i) {
      our $width = $query->param($key);
    } elsif ($key =~ /height/i) {
      our $height = $query->param($key);
    } elsif ($key =~ /red/i) {
      our $red = $query->param($key);
      $red = oct "0x" . $red;
    } elsif ($key =~ /green/i) {
      our $green = $query->param($key);
      $green = oct "0x" . $green;
    } elsif ($key =~ /blue/i) {
      our $blue = $query->param($key);
      $blue = oct "0x" . $blue;
    }
  }

  my $im = new GD::Image(our $width,our $height);

  my $color = $im->colorAllocate(our $red,our $green,our $blue);
  $im->fill(1,1,$color);
  
  open IMAGE, ">" . our $name . ".png";
  print IMAGE $im->png();
  close IMAGE;
}
Replies are listed 'Best First'.
Re: Easy Pixel PNG Generator
by ajt (Prior) on Oct 11, 2001 at 15:44 UTC
    Err, this doesn't actually work! For those of us forced to labour under a Microsoft cloud, you can't just print binary files out without turning binmode on. Thus you need to change the print statement at the end to:
    open IMAGE, ">" . $name . ".png"; binmode IMAGE; print IMAGE $im->png(); close IMAGE;

    You have also loaded CGI but then gone on to print out your own HTML. It seems a bit much having a dog, and barking yourself, if you ask me.

    Also sin of sins, you are writing to the file system, with a user supplied filename and not doing any taint checking. I know you said this was a "simple utility", but you should not allow anyone to write to your filesystem, without being sure that they are writing what and where you want to allow them. See perlsec.

    I think merlyn wrote a pixel factory on his column, which is similar in idea.

    Your script is however, minor defects aside, quite useful.

      Ok, 3 things:

      I originally had binmode in there ... but took it out after forgetting to keeping it in.

      I used CGI.pm for the parsing ... hmm.

      Yes, I did not use taint checking, I admit it.

      This was just a quick utility based on someone's idea, I have no intention of adding or subracting to it as of this moment, but thanks for the delayed response, ajt.

      Andy Summers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://111661]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found