PHP 8.4.1 Released!

imageaffine

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

imageaffineReturn an image containing the affine transformed src image, using an optional clipping area

Beschreibung

imageaffine(GdImage $image, array $affine, ?array $clip = null): GdImage|false

Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Parameter zur Verfügung.

Parameter-Liste

image

Ein GdImage-Objekt, das von einer der Funktionen zur Bilderzeugung, z. B. imagecreatetruecolor(), zurückgegeben wurde.

affine

Array with keys 0 to 5.

clip

Array with keys "x", "y", "width" and "height"; or null.

Rückgabewerte

Return affined image object on successBei einem Fehler wird false zurückgegeben..

Changelog

Version Beschreibung
8.0.0 clip is now nullable.
8.0.0 On success, this function returns a GDImage instance now; previously, a resource was returned.
add a note

User Contributed Notes 5 notes

up
22
abc at ed48 dot com
10 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 Ø
up
5
abc at ed48 dot com
10 years ago
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="*" />
up
2
adri at sternschanze dot net
6 years ago
CORRECTION:

The AFFINE ARRAY, is defined like,

$affine = [ a0, b0, a1, b1, a2, b2 ];
up
3
abc at ed48 dot com
10 years ago
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.
up
0
der at herrstrietzel dot de
3 years ago
Might be an alternative or workaround for imagecopyresampled as imageaffine supports more sophisticated interpolation such as IMG_MITCHELL.

// create image resource
$img_path = 'test.jpg';
$img = imagecreatefromstring(file_get_contents($img_path));
// scale image to 50% with Mitchell interpolation
$scale = 0.5;
imagesetinterpolation($img, IMG_MITCHELL);
$img_scaled = imageaffine($img, [0.5, 0, 0, 0.5, 0, 0]);
// save image
$output = 'test_scaledAffine.png';
imagepng($img_scaled, $output, 9);
echo '<img src="'.$output.'" width="300">';

Helper function example:

function scaleImgAffine($img, $newWidth, $resized_file='', $interpolation=IMG_MITCHELL)
{
// create img resource if $img is file path
if(!is_resource($img)){
$img = imagecreatefromstring(file_get_contents($img));
}
// get original img dimensions
$w = imagesx($img);
$h = imagesy($img);

/**
* recommended interpolation method is 'IMG_MITCHELL'
* providing the overall best precision and
* compression for most image types
* in particular for pngs with transparency suffering
* from artifacted downsampling result
*/
imagesetinterpolation($img, $interpolation);
/*
* imageaffine will scale dimensions in an anamorphic way –
* even with square based images:
* 1200x1200 will get something like 1199x1200
* – improvements are very welcome
* Workaround: Adjusting both the original width and the clip parameter
* by adding 1px solved the issue for me
*/
$scale = ($newWidth+1) / $w;
$newHeight = floor($h*$scale);

// scale by affine matrix
$resized_affine =
imageaffine(
$img,
[$scale, 0, 0, $scale, 0, 0],
['x' => 0, 'y' => 0, 'width' => $w, 'height' => $h-1]
);
/*
* 'clip' parameter might be misleading as it expects
* the original and not the final/transformed width
*/

// save to file or return resource
if($resized_file){
$file_type = pathinfo($resized_file, PATHINFO_EXTENSION );
switch($file_type){
case 'jpg':
case 'jpeg':
// flatten transparency for jpgs
$tmp_img = imagecreatetruecolor($newWidth, $newHeight );
$bg = imagecolorallocate($tmp_img, 255,255,255);
imagefill($tmp_img, 0, 0, $bg);
imagesavealpha($tmp_img, false);
imagealphablending($tmp_img, true);
imagecopy($tmp_img, $resized_affine, 0, 0, 0, 0, $newWidth, $newHeight );
imagejpeg($tmp_img, $resized_file, 85);
break;
case 'png':
imagepng($resized_affine, $resized_file, 9);
break;
}
}else{
return $resized_affine;
}
}

$img_scaled_affine = scaleImgAffine($img_path, 400, 'test_scaledAffine.jpg', true);

IMG_MITCHELL is in particular advanced when it comes to scaling png24 with transparency in GDlib environments (... actually png31 due to gds alpha 7bit channel?). Most commonly you'll experience bad compression using png due to imperceptible artifacts after resizing.

Hopefully we'll see an implementation of more advanced interpolation methods in basic GD resizing functions like imagecopyresized or imagescale.
To Top