note 40238 added to function.imagecopyresized
| From: | WebDragon at rn2 dot php dot net | Date: | Thu, 26 Feb 2004 08:25:10 +0000 |
| Subject: | note 40238 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65781@lists.php.net to get a copy of this message | ||
One thing I'd really like to be able to accomplish is similar to that done by the short Perl
script below, where I am able to specify ONLY the largest single side I wish to have (whether
it's x or y is irrelevant), and have the thumbnail thus created. For example, scaling an image
down to a minithumbnail with no side larger than say 95 pixels, I could do this:
<img
src="cgi-bin/thumb.cgi?size=<?php
echo $size;
?>&image=<?php
echo "$image_fullpath$image_file";
?>">
Script follows:
-------------------
#!/usr/bin/perl -T
# mini_thumb.cgi
use warnings;
use strict;
#local user's installed Perl modules go here
use lib '/www2/home/domain.com/perl';
# These three may need to be installed from CPAN
use CGI qw/:standard -no_xhtml/;
use GD;
use Image::GD::Thumbnail;
if ( param('image') && param('size') ) {
# you can't call the script without em
my $imagefile = param('image');
my $largest_size = param('size');
# Security untaint
# must be absolute path, no wildcards, for a clean non-abusable untaint
# must be /fullpath/to/image/(filename).jpg or boom!
die "Bad Imagefile"
unless $imagefile =~
m#^( # start capture
/www2/home/domain\.com/html/ # full path
_(?:images|thumbnails)/ # don't capture either-or dir choice
[-\w\d._]+jpg # jpeg filename
)$#x; # end capture, whitespace is irrelevant
$imagefile = $1;
# Security untaint
die "Bad Size"
unless $largest_size =~ m#^(\d{1,3})$#;
$largest_size = $1;# must be 1-3 digits or boom!
# sanity check
die "Insane size value: $largest_size"
unless (20 < $largest_size and $largest_size < 200);
die "Bad Filename: '$imagefile' does not exist"
unless -e $imagefile;
die "File not readable: $imagefile has bad permissions"
unless -r _;
# three - argument open, again security related
open IN, '<', "$imagefile"
or die "Could not open $imagefile: $!";
my $src = GD::Image->newFromJpeg(*IN);
close IN or die "Could not close: $!";
# x and y are the new dimensions, but we basically throw those away here.
my ($thumb, $x, $y) =
Image::GD::Thumbnail::create($src, $largest_size);
#ensure stream is treated as binary data
binmode STDOUT;
#ensure we have a proper header for the browser to understand
print header(-type=>'image/jpeg');
# and here's the resized image.
print $thumb->jpeg;
}
__END__
If someone could replace this with a note on how this may be accomplished in PHP I'd be
eternally grateful.
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40238
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40238&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40238&report=yes
Search -- http://master.php.net/manage/user-notes.php