Image::Magick->Write() doesn't want to write 
Author Message
 Image::Magick->Write() doesn't want to write

I can't seem to get a CGI script that's supposed to do quick-and-dirty
conversions of any image into a png.  My problem is that
$magick_image->Write('$upload_dir/$outfile'); never seems to output
anything.  The script is essentially a copy of
http://www.*-*-*.com/
PerlMagick tacked on.  Please help.

Here's the script:

#!/usr/bin/perl -wT  

use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
use Image::Magick;

$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/var/www/tmp";

my $query = new CGI;
my $filename = $query->param("image");
my $email_address = $query->param("email_address");

if ( !$filename ) {
 print $query->header ( );
 print "There was a problem uploading your image (try a smaller file).";
 exit;

Quote:
}

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;

if ( $filename =~ /^([$safe_filename_characters]+)$/ ){
 $filename = $1;

Quote:
}

else{
 die "Filename contains invalid characters";

Quote:
}

my $upload_filehandle = $query->upload("image");

open ( UPLOADFILE, ">$upload_dir/$filename" )
  or die "$! Cannot write to $upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
 print UPLOADFILE;

Quote:
}

close UPLOADFILE;

###################
# Image Conversion
###################

my $magick_image = Image::Magick->new();
my $outfile = $filename;
my $status;

$outfile =~ s/\..*$//g;
$outfile = $outfile . ".png";

open(IMAGE, '$upload_dir/$filename');
$magick_image->Read('$upload_dir/$filename');
$magick_image->Write('$upload_dir/$outfile');
close(IMAGE);

print $query->header ( );
print <<END_HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns=" http://www.*-*-*.com/ ; xml:lang="en" lang="en">
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Your Converted Image</title>
   <style type="text/css">
     img {border: none;}
   </style>
 </head>
 <body>
   <p>Here is your original image</p>
   <p><img src="/tmp/$filename" alt="Original Image" /></p>
   <p>Here is your converted image</p>
   <p><img src="/tmp/$outfile" alt="Converted Image" /></p>
 </body>
</html>
END_HTML

--
David Griffith



Fri, 04 May 2012 22:10:29 GMT  
 Image::Magick->Write() doesn't want to write

Quote:

> I can't seem to get a CGI script that's supposed to do quick-and-dirty
> conversions of any image into a png.  My problem is that

<snip>

Quote:
> open(IMAGE, '$upload_dir/$filename');
> $magick_image->Read('$upload_dir/$filename');
> $magick_image->Write('$upload_dir/$outfile');
> close(IMAGE);

If that script is accurate, you're going to have a problem with (at
least) these lines.  The single quotes prevent variable interpolation,
so you're trying to read and write files literally named
$upload_dir/$filename, $upload_dir/$outfile, etc.  You'll have more luck
if you change these to double quotes, or, as an example,
sprintf('%s/%s', $upload_dir, $filename).

Cheers,
Danny.



Fri, 04 May 2012 22:23:31 GMT  
 Image::Magick->Write() doesn't want to write

Quote:


>> I can't seem to get a CGI script that's supposed to do quick-and-dirty
>> conversions of any image into a png.  My problem is that
> <snip>
>> open(IMAGE, '$upload_dir/$filename');
>> $magick_image->Read('$upload_dir/$filename');
>> $magick_image->Write('$upload_dir/$outfile');
>> close(IMAGE);
> If that script is accurate, you're going to have a problem with (at
> least) these lines.  The single quotes prevent variable interpolation,
> so you're trying to read and write files literally named
> $upload_dir/$filename, $upload_dir/$outfile, etc.  You'll have more luck
> if you change these to double quotes, or, as an example,
> sprintf('%s/%s', $upload_dir, $filename).

Thanks.  I was fiddling around with this for an hour.  Argh!

--
David Griffith



Fri, 04 May 2012 23:07:21 GMT  
 Image::Magick->Write() doesn't want to write
On Mon, 16 Nov 2009 21:10:29 +0000 (UTC) in comp.lang.perl.misc,

Quote:
>                                      My problem is that
>$magick_image->Write('$upload_dir/$outfile'); never seems to output
>anything.

What does the error message returned by $magick_image->Write say?


Sat, 05 May 2012 05:01:37 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Image::Magick to write Windows icons

2. Can't load Magick.so for module Image::Magick

3. Apache, embperl, mod_perl : require doesn't work and can't write to file

4. Help: Perl Script Doesn't Write Out

5. writing to db file fails even though the open doesn't

6. I'm looking for someone who wants to write a complete perl-script (for free)

7. Image Magick finding image size without loading image

8. HELP: 'write' in perl 5.001 doesn't write "\n"

9. Image::Magick JPEG=>TIF Conversion

10. Image::Magick JPEG=>TIF Conversion

11. Need help with Perl::Magick (Image::Magick)

 

 
Powered by phpBB® Forum Software