Imagick::distortImage

(PECL imagick 2 >= 2.0.1, PECL imagick 3)

Imagick::distortImage β€” Π˜ΡΠΊΠ°ΠΆΠ°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ искаТСния

ОписаниС

public function Imagick::distortImage(int $method, array $arguments, bool $bestfit): bool

Π˜ΡΠΊΠ°ΠΆΠ°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ искаТСния, сопоставляя поисковыС запросы Ρ†Π²Π΅Ρ‚Π° исходного изобраТСния с Π½ΠΎΠ²Ρ‹ΠΌ Ρ†Π΅Π»Π΅Π²Ρ‹ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ, ΠΎΠ±Ρ‹Ρ‡Π½ΠΎ Ρ‚ΠΎΠ³ΠΎ ΠΆΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°, Ρ‡Ρ‚ΠΎ ΠΈ исходноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, Ссли для ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° "bestfit" установлСно Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

Если ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ "bestfit" Π²ΠΊΠ»ΡŽΡ‡Ρ‘Π½ ΠΈ это позволяСт искаТСниС, Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ настраиваСтся Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ исходноС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ соотвСтствовало ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠΌΡƒ Ρ†Π΅Π»Π΅Π²ΠΎΠΌΡƒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΠΉ Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΈ смСщСниС. Π’Π°ΠΊΠΆΠ΅ Π²ΠΎ ΠΌΠ½ΠΎΠ³ΠΈΡ… случаях ΠΏΡ€ΠΈ сопоставлСнии Π±ΡƒΠ΄Π΅Ρ‚ ΡƒΡ‡ΠΈΡ‚Ρ‹Π²Π°Ρ‚ΡŒΡΡ Π²ΠΈΡ€Ρ‚ΡƒΠ°Π»ΡŒΠ½ΠΎΠ΅ смСщСниС исходного изобраТСния.

ΠœΠ΅Ρ‚ΠΎΠ΄ доступСн, Ссли ΠΌΠΎΠ΄ΡƒΠ»ΡŒ Imagick скомпилировали с вСрсиСй ImageMagick 6.3.6 ΠΈΠ»ΠΈ ΡΡ‚Π°Ρ€ΡˆΠ΅.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

method

ΠœΠ΅Ρ‚ΠΎΠ΄ искаТСния изобраТСния. Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ константы искаТСния.

arguments

АргумСнты для Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° искаТСния.

bestfit

ΠŸΠΎΠΏΡ‹Ρ‚ΠΊΠ° ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Ρ€Π°Π·ΠΌΠ΅Ρ€ мСста назначСния, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ½ соотвСтствовал искаТённому источнику.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π² случаС ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Ρ‹ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true.

Ошибки

Ѐункция выбрасываСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ImagickException, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Imagick::distortImage():

ИскаТСниС изобраТСния ΠΈ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π΅.

<?php
/* Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° */
$im = new Imagick();

/* Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΡƒΠ·ΠΎΡ€Π° Π² Π²ΠΈΠ΄Π΅ ΡˆΠ°Ρ…ΠΌΠ°Ρ‚Π½ΠΎΠΉ доски */
$im->newPseudoImage(100, 100, "pattern:checkerboard");

/* Установка Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° изобраТСния Π½Π° png */
$im->setImageFormat('png');

/* Π—Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²Ρ‹Ρ… Π²ΠΈΠ΄ΠΈΠΌΡ‹Ρ… областСй ΠΏΡ€ΠΎΠ·Ρ€Π°Ρ‡Π½Ρ‹ΠΌ Ρ†Π²Π΅Ρ‚ΠΎΠΌ */
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);

/* Активация матовости */
$im->setImageMatte(true);

/* ΠšΠΎΠ½Ρ‚Ρ€ΠΎΠ»ΡŒΠ½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ для искаТСния */
$controlPoints = array( 10, 10,
10, 5,

10, $im->getImageHeight() - 20,
10, $im->getImageHeight() - 5,

$im->getImageWidth() - 10, 10,
$im->getImageWidth() - 10, 20,

$im->getImageWidth() - 10, $im->getImageHeight() - 10,
$im->getImageWidth() - 10, $im->getImageHeight() - 30);

/* Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ искаТСния */
$im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true);

/* Π’Ρ‹Π²ΠΎΠ΄ изобраТСния */
header("Content-Type: image/png");
echo
$im;
?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Imagick::distortImage()

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • Imagick::blurImage() - ДобавляСт Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ размытия ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ
  • Imagick::motionBlurImage() - Π˜ΠΌΠΈΡ‚ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ€Π°Π·ΠΌΡ‹Ρ‚ΠΈΠ΅ Π² Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΠΈ
  • Imagick::radialBlurImage() - РадиальноС Ρ€Π°Π·ΠΌΡ‹Ρ‚ΠΈΠ΅ изобраТСния
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
11
DJ Mike ΒΆ
15 years ago
Affine

<?php 
$image = new imagick( "opossum.jpg" ); 
$points = array( 
                0,0, 25,25,   
               100,0, 100,50 
               );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage(  Imagick::DISTORTION_AFFINE, $points, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Affine Projection

<?php 
$image = new imagick( "opossum.jpg" ); 
$points = array( 0.9,0.3,
                -0.2,0.7,
                 20,15 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_AFFINEPROJECTION, $points, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Arc

<?php 
$image = new imagick( "opossum.jpg" ); 
$draw = new imagickdraw(); 
$degrees = array( 180 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_ARC, $degrees, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Rotated Arc

<?php 
$image = new imagick( "opossum.jpg" ); 
$draw = new imagickdraw(); 
$degrees = array( 180, 45, 100, 20 );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_ARC, $degrees, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Bilinear

<?php 
$image = new imagick( "opossum.jpg" ); 
$points = array( 
                0,0, 25,25, # top left  
                176,0, 126,0, # top right
                0,135, 0,105, # bottom right 
                176,135, 176,135 # bottum left
                );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_BILINEAR, $points, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Perspective

<?php 
$image = new imagick( "opossum.jpg" ); 
$points = array( 
                0,0, 25,25, # top left  
                176,0, 126,0, # top right
                0,135, 0,105, # bottom right 
                176,135, 176,135 # bottum left
                );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_PERSPECTIVE, $points, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>

Scale Rotate Translate

<?php 
$image = new imagick( "opossum.jpg" ); 
$points = array( 
                 1.5, # scale 150%
                 150 # rotate
               );
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( imagick::DISTORTION_SCALEROTATETRANSLATE, $points, TRUE ); 
header( "Content-Type: image/jpeg" ); 
echo $image;
?>
up
4
peter dot e dot lind at gmail dot com ΒΆ
11 years ago
It is worth nothing that details on distorts as well as arguments can be found at http://www.imagemagick.org/Usage/distorts/ - the PHP manual doesn't give much, if any, detail on arguments.

I found this very useful when I had to do a translated rotation on an image - i.e. rotating around a point in the image that was not square in the center. This can be achieved by using distortImage with Imagick::DISTORTION_SCALEROTATETRANSLATE like this:

<?php

$imagick = new Imagick('/path/to/image');
$args = array(
    20, # x point to rotate around
    20, # y point to rotate around
    1, # scaling factor - 1 means no scaling
    90, # angle to rotate
);

$imagick->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$imagick->distortImage(Imagick::DISTORTION_SCALEROTATETRANSLATE, $args, true);

?>

Note that this type of distort takes from 1 to 7 arguments, depending on what you want to do exactly - there is a lot of flexibility here.
up
2
Abe ΒΆ
5 years ago
Control Points for DISTORTION_PERSPECTIVE are pairs of x0, y0, x1, y1 coordinates, where x0/y0 is the original point and x1/y1 is the destination point, 

e.g. for a four point distortion:

    <?php 

    $im = new imagick( $fn); 

    $w=$im->getImageWidth();
    $h=$im->getImageHeight();
    $im->setImageFormat('png');

    $controlPoints = array(
                    $tLx, $tLy, 0, 0,  // top left
                    $tRx, $tRy, $w, 0, // top right
                    $bRx, $bRy, $w, $h, // bottom right
                    $bLx, $bLy, 0, $h // bottom left
                );

    /* Perform the distortion */
    $im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, false);

    // Output the image
    $output = $im->getimageblob();
    $outputtype = $im->getFormat();

    header("Content-type: $outputtype");
    echo $output;
     
    ?>
up
1
ashokmca.g at gmail dot com ΒΆ
15 years ago
Slide image with shadow using distortImage

<?php

$slideValue = 150;

// Create new object 
$im = new Imagick("grnhrs.jpg");

// Resize 
$im->thumbnailImage(500,400);

// Set the image format to png 
$im->setImageFormat('png');

//Clone the current object
$shadow = $im->clone();

//Set image background color to black (this is the color of the shadow)
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
 
 //Create the shadow 
$shadow->shadowImage( 80, 10, 5, 5 );

// Fill background area with transparent for image
//VIRTUALPIXELMETHOD_TRANSPARENT
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_ TRANSPARENT);

// Activate matte 
$im->setImageMatte(true);

//Control points for the distortion 
$controlPoints = array( 0, 0,
                        $slideValue, 0,
                        
                        0, $im->getImageHeight(),
                        0, $im->getImageHeight(),
                        
                        $im->getImageWidth(), 0,
                        $im->getImageWidth(), 0,
                        
                        $im->getImageWidth(), $im->getImageHeight(),
                        $im->getImageWidth()-$slideValue, $im->getImageHeight());
                        
// Perform the distortion 
$im->distortImage(Imagick::DISTORTION_PERSPECTIVEPROJECTION, $controlPoints, true);

// Perform the distortion in shadow image
$shadow->distortImage(Imagick::DISTORTION_PERSPECTIVEPROJECTION, $controlPoints, true);

// Imagick::shadowImage only creates the shadow.
// That is why the original image is composited over it 
$shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );

/* Ouput the image */
header("Content-Type: image/png");
echo $shadow;

?>