michaelg has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm writing code that should become stand alone exe. I need to put a logo in it , well I did it using

my $image = $nw->Photo(-file => "./tsl_logo1.gif"); my $t_im = $nw->Label(-image => $image, -background => $color )->pack( -side => 'left', -expand => 1, -fill => 'both' );
Well ofcourse that when I execute the binary at a place that does not contain the needed gif the app crash down.

Do you monks have a way to solve that .

thx
michaelg

edited: Sun Nov 16 16:17:55 2003 by jeffa - code tags formatting

janitored by ybiC: Append "TK" to title

Replies are listed 'Best First'.
Re: using an image in binary perl
by zentara (Cardinal) on Nov 16, 2003 at 23:05 UTC
    Well it looks like you might be using Tk. Here is a simple example of encoding a photo to a variable. (It's not porn, it's Tux :-) In Tk , one of the peculiarities is that "data" image files must be Base64 encoded anyways, so there is no need to decode"
    #!/usr/bin/perl -w use strict; use Tk; use Tk::JPEG; use MIME::Base64; #this method works as easy as it does, because #Tk needs it's images to be base64 encoded anyways, #use this to encode #open (FH,"< tux.jpg") or die $!; #my $photo = do {local $/; <FH>}; #my $content = encode_base64($photo) or die $!; #print $content; #don't forget to '' quote my $photo = '/9j/4AAQSkZJRgABAQIASABIAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UH +RofHh0a HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMj +IyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AE +MDASIA AhEBAxEB/8QAHAAAAgIDAQEAAAAAAAAAAAAAAAQFBwIDCAYB/8QAMBAAAQMCBQMCBQMFAA +AAAAAA AQIDBAARBQYSITEHUXETYSIyQUKBFTOhI1KRscH/xAAaAQEBAQADAQAAAAAAAAAAAAAAAQ +IDBQYE /8QAIxEAAgEDAwQDAAAAAAAAAAAAAAECAwQREyExBRJBkVFSof/aAAwDAQACEQMRAD8Ap5 +brjgSF uLUEgB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9kgWAoDbRRRQFc9ZsnR8zZJkzUpAn4W2qSy59SgC60n2IF/IHvXLkVZcjpJ5GxrrzqfN +dgdM8f fZ06zFLXxC+yyEH+FGuQIP7Fve9AW9Iw+JIfU7h+TJaoqrFsuy1sqO290FSrb3+p7+1FQK +HXEoAW UuqAtrW2gqPnaivld3BPGGcmkz//2Q=='; my $mw = MainWindow->new(); my $image = $mw->Photo(-data => $photo); $mw->Label(-image => $image)->pack(-expand => 1, -fill => 'both'); $mw->Button(-text => 'Quit', -command => [destroy => $mw])->pack; MainLoop;
Re: using an image in binary perl
by Roger (Parson) on Nov 16, 2003 at 22:38 UTC
    Just a note that GIF's are in a proprietry format with plenty of copyright issues with its LZW compression. You should perhaps use the png format to store your logo.

    I think bart's solution is one of the cleanest so far -

    Preparation Steps
    Step 1 - use a standalone script to uuencode the logo image into a text string.
    Step 2 - cut & paste the string in your source.

    Execution Steps
    Step 1 - uudecode the string in your Perl app into decoded binary image.
    Step 2 - initialize the image object with the in-memory binary image.

Re: using an image in binary perl
by simonm (Vicar) on Nov 16, 2003 at 15:21 UTC
    I'm not entirely sure I understand the question, but...

    Perhaps you could wrap the above block in an if clause that tested for the existence of that file?

    if ( -e "./tsl_logo1.gif" ) { ... }
      NOP,
      I Need to load it anyway , it must become part of my code.
      I mean that when I'll activate the binary It
      won't have to search for any file , all the data it needs
      should be compiled inside it.
      very simillar to .pm files

      THANKS FOR YOUR TIME
      michaelg
        You could use Perl's built in UU-encoding, built into pack/unpack, using the "u*" template, to convert any binary string to a text string of limited line length.

        You can use the following code to load the file and generate the Perl code to set the image string, which you can then include in your script:

        { open IN, "tsl_logo1.gif"; binmode IN; local $/; print "\$image = unpack 'u*', <<'_END_';\n"; print pack "u*", <IN>; print "_END_\n"; }

        Of course, that requires that you can just set an image in a UI from a binary string, instead of loading it from a file. I don't know what module you're using for the UI, so you'll have to check that yourself.

        The proper way of packing along extra files differs depending on how you're creating the binary.

        Please tell us what tools you are using to produce the executable -- perl2exe? PAR?

Re: using an image in binary perl
by Itatsumaki (Friar) on Nov 16, 2003 at 15:35 UTC

    Another way (besides testing for the existence of the file) is to wrap the call that crashes into an eval block and test for errors in @$ afterwards.

    I think part of what you want is a way to distribute the gif with the binary of Perl itself. You could do that by ROT13 encoding the gif, embedding that into the source code of your application, and then extracting and unencoding it before your program needs it.

      ...by ROT13 encoding the gif...

      What will that bring you? That's not going to help you troublesome byte values in your source code. I assume you mean Base64 (MIME) encoding?

      Liz

        Ahh, you're right; I plead early-morning insanity. :)