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;
?>
Imagick::distortImage
(PECL imagick 2.0.1)
Imagick::distortImage — Deforma una imagen utilizando varios métodos de distorsión
Descripción
$method
, array $arguments
, bool $bestfit
)
Deforma una imagen utilizando varios métodos de distorsión, mapeando la paleta
de colores de la imagen de origen a una nueva imagen destino normalmente del mismo
tamaño que la imagen de origen, a menos que 'bestfit' esté establecido a TRUE.
Si 'bestfit' está habilitado, y la distorsión lo permite, la imagen destino se ajusta para asegurarse de que la 'imagen' de origen entera se ajustará dentro de la imagen destino final, la cuál será redimensionada e compensada acordemente. También, en la mayoría de los casos el índice virtual de la imagen de origen será tomado en cuenta en el mapeado.
Este método está disponible si Imagick ha sido compilado con la versión 6.3.6 (o superior) de ImageMagick.
Parámetros
-
method -
El método de distorsión de la imagen. Véase constantes de distorsión
-
arguments -
Los argumentos para este método de distorsión
-
bestfit -
Intenta redimensionar la imagen destino para ajustarse a la imagen de origen deformada
Valores devueltos
Devuelve TRUE en caso de éxito.
Errores/Excepciones
Lanza ImagickException en caso de error.
Ejemplos
Ejemplo #1 Usar Imagick::distortImage():
Deformar una imagen y mostrarla en el navegador.
<?php
/* Crear un nuevo objeto */
$im = new Imagick();
/* Crear un nuevo patrón de tablero de ajedrez */
$im->newPseudoImage(100, 100, "pattern:checkerboard");
/* Esteblecer el formato de la imagen a png */
$im->setImageFormat('png');
/* Rellenar las nuevas áreas visibles con transparente */
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
/* Activar el mate */
$im->setImageMatte(true);
/* Puntos de control para la distorsión */
$puntosControl = 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);
/* Realizar la distorsión */
$im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $puntosControl, true);
/* Imprimir la imagen */
header("Content-Type: image/png");
echo $im;
?>
El resultado del ejemplo sería algo similar a:
Ver también
- Imagick::blurImage() - Añade un filtro de borrosidad a la imagen
- Imagick::motionBlurImage() - Simula borrosidad en movimiento
- Imagick::radialBlurImage() - Hace borrosa de forma radial una imagen
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;
?>
