note 84516 added to function.imagecopyresampled
| From: | swizec at swizec dot com | Date: | Thu, 17 Jul 2008 09:20:49 +0000 |
| Subject: | note 84516 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141609@lists.php.net to get a copy of this message | ||
Wrote a function for sanitising user uploaded images. It saves the native image in size roughly
800x600 so it still fits on most screens when opened, makes a desired size thumbnail and turns all
images into high quality jpegs for smaller bandwidth use.
Thumbnails are made in a similar way a designer would make them in photoshop, first resize the most
troublesome dimension to desired size, then crop the rest out so the image retains proportions and
most of it ends up in the thumbnail.
<?php
// $image is $_FILES[ <image name> ]
// $imageId is the id used in a database or wherever for this image
// $thumbWidth and $thumbHeight are desired dimensions for the thumbnail
function processImage( $image, $imageId, $thumbWidth, $thumbHeight )
{
$type = $image[ 'type' ];
$galleryPath = 'images/collection/';
if ( strpos( $type, 'image/' ) === FALSE )
{ // not an image
return FALSE;
}
$type = str_replace( 'image/', '', $type );
$createFunc = 'imagecreatefrom' . $type;
$im = $createFunc( $image[ 'tmp_name' ] );
$size = getimagesize( $image[ 'tmp_name' ] );
$w = $size[ 0 ];
$h = $size[ 1 ];
if ( $w > 800 || $h > 600 )
{ // we make sure the image isn't too huge
if ( $w > 800 )
{
$nw = 800;
$nh = ceil( $nw*($h/$w) );
}elseif( $h > 600 )
{
$nh = 600;
$nw = ceil( $nh*($w/$h) );
}
$im2 = imagecreatetruecolor( $nw, $nh );
imagecopyresampled( $im2, $im, 0, 0, 0, 0, $nw, $nh, $w, $h );
imagedestroy( $im );
$im = $im2;
$w = $nw;
$h = $nh;
}
// create thumbnail
$tw = $thumbWidth;
$th = $thumbHeight;
$imT = imagecreatetruecolor( $tw, $th );
if ( $tw/$th > $th/$tw )
{ // wider
$tmph = $h*($tw/$w);
$temp = imagecreatetruecolor( $tw, $tmph );
imagecopyresampled( $temp, $im, 0, 0, 0, 0, $tw, $tmph, $w, $h ); // resize to width
imagecopyresampled( $imT, $temp, 0, 0, 0, $tmph/2-$th/2, $tw, $th, $tw, $th ); // crop
imagedestroy( $temp );
}else
{ // taller
$tmpw = $w*($th/$h );
$imT = imagecreatetruecolor( $tmpw, $th );
imagecopyresampled( $imT, $im, 0, 0, 0, 0, $tmpw, $h, $w, $h ); // resize to height
imagecopyresampled( $imT, $temp, 0, 0, $tmpw/2-$tw/2, 0, $tw, $th, $tw, $th ); // crop
imagedestroy( $temp );
}
// save the image
imagejpeg( $im, $galleryPath . $imgid . '.jpg', 100 );
imagejpeg( $imT, $galleryPath . $imgid . '_thumb.jpg', 100 );
}
?>
----
Server IP: 213.250.2.28
Probable Submitter: 212.18.43.58
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/84516
Del: integrated -- https://master.php.net/note/delete/84516/integrated
Del: useless -- https://master.php.net/note/delete/84516/useless
Del: bad code -- https://master.php.net/note/delete/84516/bad+code
Del: spam -- https://master.php.net/note/delete/84516/spam
Del: non-english -- https://master.php.net/note/delete/84516/non-english
Del: in docs -- https://master.php.net/note/delete/84516/in+docs
Del: other reasons-- https://master.php.net/note/delete/84516
Reject -- https://master.php.net/note/reject/84516
Search -- https://master.php.net/manage/user-notes.php