PHP 8.3.4 Released!

Imagick::distortImage

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

Imagick::distortImageさまざまな方式で画像を歪める

説明

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

さまざまな方式で画像を歪めます。 これは、元画像の色を新たな画像の色に対応させることで行います。 新たな画像は、'bestfit' を true に設定しない限りは元画像と同じ大きさになります。

'bestfit' を有効にし、使用する歪め方式がサイズ変更を許可していた場合は、 変換後の画像に合わせて画像のサイズやオフセットが調整されます。 多くの場合、元画像の仮想オフセットも考慮したマッピングが行われます。

このメソッドは、ImageMagick バージョン 6.3.6 以降で Imagick をコンパイルした場合に使用可能です。

パラメータ

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() の使用法

参考

add a note

User Contributed Notes 4 notes

up
4
peter dot e dot lind at gmail dot com
9 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
9
DJ Mike
12 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
1
Abe
3 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
13 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;

?>
To Top