note 36234 added to function.imagecopyresampled
| From: | code_couturier at graffiti dot net | Date: | Thu, 02 Oct 2003 00:09:43 +0000 |
| Subject: | note 36234 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-57565@lists.php.net to get a copy of this message | ||
# here is a simple way to calc
# the new size of an image according
# to a specific proportional value
# of an original/source image and a
# specific dec./inc. value.
# $oX,$oY = original x and y image_size
# $nX,$nY = new x and y image_size
# here we go ...
$n=(float)1.7; # must be >1.0
$org_proportion=(float)($oX/$oY);
$nX=(int)($oX/$n);
$nY=(int)($nX/$org_proportion);
# now we can create, resize and copy
$img_new = imagecreatetruecolor($nX,$nY);
imagecopyresampled ($im_new, $im_org,0,0,0,0,
$nX,$nY,$oX,$oY);
# and here the same with a new proportional
# value for the destination image. we use
# (width/height) = proportion to calc the
# new height and width, according to the
# new size. the image must have a landscape
# format otherwise we must swap the x an y
# values inside this calculation (see if ...)!
$n=(float)1.7; # must be >1.0
$new_proportion=(float)1.4; # must be >1.0
if ($oY<=$oX) {
# if org_image is landscape_oriented
$nY=(int)($oY/$n);
$nX=(int)($nY*$new_proportion);
} else {
# if org_image is portrait_oriented
$nX=(int)($oX/$n);
$nY=(int)($nX*$new_proportion);
}
# now we can create,resize and copy
$im_new = imagecreatetruecolor($nX,$nY);
imagecopyresampled($im_new,$im_org,
0,0,0,0,$nX,$nY,$nX,$nY);
# but - this will only create a clip from the
# top and left (0,0) corner from the org_image.
# why we do not take all (usefull) cut-out's
# from the original image ?!
# ok, ther are 13'tn possible/usefull areas, we
# are able to calculate a crop-position (x,y,w,h) from:
1 top,left
(0,0,$nX,$nY)
2 top,center
((int)(($oX-$nX)/2),0,$nX,$nY)
3 top,right
($oX-$nX,0,$nX,$nY)
4 bottom,left
(0,$oY-$nY,$nX,$nY)
5 bottom,center
((int)(($oX-$nX)/2),$oY-$nY,$nX,$nY)
6 bottom,right
($oX-$nX,$oY-$nY,$nX,$nY)
7 center,right
($oX-$nX,(int)(($oY-$nY)/2),$nX,$nY)
8 center,left
(0,(int)(($oY-$nY)/2),$nX,$nY)
9 top
(0,0,$oX,$nY)
10 bottom
(0,$oY-$nY,$oX,$nY)
11 right
($oX-$nX,0,$nX,$oY)
12 left
(0,0,$nX,$oY)
13 center
((int)(($oX-$nX)/2),(int)(($oY-$nY)/2),$nX,$nY)
# you can use these different (x,y,h,w) alignements here:
imagecopyresampled($im_new,$im_org,
0,0,x,y,$nX,$nY,w,h);
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36234
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36234&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36234&report=yes