PHP.mk документација

imageaffine

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.imageaffine.php PHP.net прокси Преводот е вчитан
Оригинал на PHP.net
Патека function.imageaffine.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + преведен приказ Кодовите, табелите и белешките остануваат читливи во истиот тек.
imageaffine

Референца за `function.imageaffine.php` со подобрена типографија и навигација.

function.imageaffine.php

imageaffine

Распакување на вгнездени низи

imageaffineВрати слика што ја содржи афински трансформираната изворна слика, користејќи опционална област за сечење

= NULL

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

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Оваа функција моментално не е документирана; достапна е само листата со аргументи.

Параметри

image

А GdImage не применува никакво полнење, така што ширината на сликата мора да биде множител на 8. Ова ограничување веќе не важи од PHP 7.0.9. imagecreatetruecolor().

affine

Низа со клучеви од 0 до 5.

clip

Низа со клучеви "x", "y", "width" и "height"; или null.

Вратени вредности

Врати афински објект со слика при успех или false при неуспех.

Дневник на промени

Верзија = NULL
8.0.0 clip сега е null.
8.0.0 При успех, оваа функција враќа GDImage инстанца сега; претходно, а resource .

Белешки од корисници 5 белешки

abc на ed48 точка com
12 години пред
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 Ø
abc на ed48 точка com
12 години пред
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="*" />
adri на sternschanze точка net
пред 8 години
CORRECTION:

The AFFINE ARRAY, is defined like,

         $affine = [ a0, b0, a1, b1, a2, b2 ];
abc на ed48 точка com
12 години пред
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.
der на herrstrietzel точка de
пред 5 години
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.
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.