Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^9: image resize error

by poj (Abbot)
on Oct 20, 2018 at 09:58 UTC ( [id://1224380]=note: print w/replies, xml ) Need Help??


in reply to Re^8: image resize error
in thread image resize error

try

#!/usr/bin/perl use strict; use warnings; use CGI; use File::Copy; use CGI::Carp 'fatalsToBrowser'; use Image::Resize; my $q = CGI->new; my $photo = $q->param("pic"); my ($ext) = $photo =~ /([^.]+)$/; my $msg = ''; my $newfile = "100x100.$ext"; # change as req if ($ext =~ /jpe?g|png/i){ my $copydir = '/var/www/html/img/';#/full/path/to/web/img/'; my $image = Image::Resize->new($photo); my $gd = $image->resize(100, 100); open OUTFILE,'>',$copydir.$newfile or die "Could not open $copydir$newfile : $!"; binmode OUTFILE; if ($ext =~ /jpe?g/){ print OUTFILE $gd->jpeg(); } else { print OUTFILE $gd->png; } close OUTFILE; $msg = "OK: File $photo uploaded"; } else { $msg = "ERROR: Image must be jpeg/jpg or png not '$ext'"; } print $q->header; print << "HTM"; <h2>File upload</h2> <form method="post" action="" enctype="multipart/form-data"> Filename: <input type="file" accept="image/*" name="pic"/><br/><br/> <input type="submit"/> </form> <pre>$msg</pre> <img src="/img/$newfile"/> HTM

Note : This works because $q->param('pic') can return both a filename and a filehandle. Although it's not explicit in the documentation Image::Resize->new() will accept a filehandle as it uses GD::Image->new($image) to create the object.

poj

Replies are listed 'Best First'.
Re^10: image resize error
by bigup401 (Pilgrim) on Oct 21, 2018 at 16:48 UTC

    thanks poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found