# imageaffine

(PHP 5 >= 5.5.0, PHP 7)

imageaffine返回经过仿射变换后的图像，剪切区域可选

### 说明

imageaffine ( resource `\$image` , array `\$affine` , array `\$clip` = ? ) : resource

`image`

`affine`

`clip`

### User Contributed Notes 4 notes

21
abc at ed48 dot com
7 years ago
``` AFFINE is a geometric transformation operation involving MATRICES, covering both, 2D and 3D environment.Transformations are often used in linear algebra and computer graphics.In geometric transformations of images, the pixel coordinate is mapped.This means that, each pixel is localized by two coordinates, in the rectangular domain of the image.Without going into more details about pixel mapping, let's get to what really matters: the AFFINE transformations.There are several classes of pixel mapping, one is called "affine".Affine transformations include: scaling, rotation, shearing and translation.PHP 5.5.0+, like "libart", uses ARRAYS of six floating-point elements to do the job.The AFFINE ARRAY, is defined like,          \$affine = [ a0, a1, b0, b1, a2, b2 ];                  where,          a0, a1, b0, b1, a2, b2 are floating-point values.                              represented by equations,                  x' = a0x + a1y + a2                              y' = b0x + b1y + b2         a ) IDENTITY, no change made in the path of points,         \$affine = [ 1, 0, 0, 1, 0, 0 ];                  equations remapping,                  x' = 1x + 0y + 0 = x                  y' = 0x + 1y = 0 = y         b ) TRANSLATION,         \$affine = [ 1, 0, 0, 1, H, V ];         equations remapping,                  x' = 1x + 0y + H = x + H                  y' = 0x + 1y = V = y + V                  Each point is moved H units horizontaly and V units verticaly.         c ) SCALE,         \$affine = [ i, 0, j, 1, 0, 0 ];         equations remapping,                  x' = Mx + 0y + 0 = Mx                  y' = 0x + Ny = 0 = Ny                  Each point will stretch or compress its path, horizontally or vertically, according M and N; negative or positive values.         d ) SHEARING, parallel to x axis,         \$affine = [ 1, K, 0, 1, 0, 0 ];         equations remapping,                  x' = 1x + Ky + 0 = x + Ky                  y' = 0x + 1y = 0 = y             SHEARING, parallel to y axis,         \$affine = [ K, 0, 0, 1, 0, 0 ];         equations remapping,                  x' = 1x + 0y + 0 = x                  y' = Kx + 1y = 0 = y + Kx         e ) ROTATION, clockwise,         \$affine = [ cos Ø, sin Ø, -sin Ø, cos Ø, 0, 0 ];         equations remapping,                  x' =  x cos Ø + y sin Ø + 0 = x cos Ø + y sin Ø                  y' = -x sin Ø + y cos Ø = 0 = y cos Ø - x sin Ø             ROTATION, conterclockwise,         \$affine = [ cos Ø, -sin Ø, sin Ø, cos Ø, 0, 0 ];         equations remapping,                  x' =  x cos Ø - y sin Ø + 0 = x cos Ø - y sin Ø                  y' = x sin Ø + y cos Ø = 0 = y sin Ø - x cos Ø ```
``` CORRECTION:The AFFINE ARRAY, is defined like,         \$affine = [ a0, b0, a1, b1, a2, b2 ]; ```
``` Here is an example, which performs a specific processing, every time it is executed:<?php if (!function_exists('imageaffine')) { echo 'FUNCTION NOT DEFINED IN THIS VERSION OF PHP'; exit; } \$base_img = 'affine.png'; \$tgt_img1 = 'triangle1.png'; \$tgt_img2 = 'triangle2.png'; \$arr_affine = [ [ 1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, 1, 150, 0 ], [ 1.2, 0, 0, 0.6, 0, 0 ], [ -1.2, 0, 0, -0.6, 0, 0 ], [ 1, 2, 0, 1, 0, 0 ], [ 2, 1, 0, 1, 0, 0 ], [ cos(15), sin(15), -sin(15), cos(15), 0, 0 ], [ cos(15), -sin(15), sin(15), cos(15), 0, 0 ] ];\$RSR_base = imagecreatetruecolor(400, 300); \$w = imagesx(\$RSR_base); \$h = imagesy(\$RSR_base); \$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => \$w, 'height' => \$h ]; \$fillcolor = imagecolorallocate(\$RSR_base, 0, 0, 0); imagefill(\$RSR_base, 10,10, \$fillcolor); imagepng(\$RSR_base, \$base_img); \$drawcolor = imagecolorallocate(\$RSR_base, 255, 0, 0);  \$triangle = [ 50, 50, 50, 150, 200, 150 ]; \$points = 3; imageantialias(\$RSR_base, 1); \$drawtriangle = imagefilledpolygon(\$RSR_base, \$triangle, \$points, \$drawcolor);imagepng(\$RSR_base, \$tgt_img1);\$select = mt_rand(0, 7); \$RSRaff2 = imageaffine(\$RSR_base, \$arr_affine[\$select], \$arr_clip); imagepng(\$RSRaff2, \$tgt_img2, 9); ?> SUPPORT IMAGE<br><br> <img src="<?php echo \$base_img; ?>" alt="*" /><br><br> BASE IMAGE<br><br> <img src="<?php echo \$tgt_img1; ?>" alt="*" /><br><br> RESULT IMAGE<br><br> <img src="<?php echo \$tgt_img2; ?>" alt="*" /> ```
``` correcting:c ) SCALE,         \$affine = [ M, 0, N, 1, 0, 0 ];         equations remapping,                  x' = Mx + 0y + 0 = Mx                  y' = 0x + Ny = 0 = Ny                  Each point will stretch or compress its path, horizontally or vertically, according M and N; negative or positive values. ```