Imagick::trimImage

(PECL imagick 2, PECL imagick 3)

Imagick::trimImageElimina los extremos de la imagen

Descripción

public Imagick::trimImage(float $fuzz): bool

Elimina los extremos que son el color de fondo de la imagen. Este método está disponible si Imagick ha sido compilado con la versión 6.2.9 o superior de ImageMagick.

Parámetros

fuzz

Por defecto, el objetivo debe coincidir exactamente con un color de píxel en particular. Sin embargo, en la mayoría de los casos dos colores pueden diferir por una cantidad pequeña. El miembro enfoque de la imagen define cuánta tolerancia es aceptable para considerar que dos colores son el mismo. Este parámetro represenata la variación del rango de cuantía.

Valores devueltos

Devuelve true en caso de éxito.

Errores/Excepciones

Lanza ImagickException en caso de error.

Ejemplos

Ejemplo #1 Usar Imagick::trimImage():

Recortar una imagen, después mostrarla en el navegador.

<?php
/* Crear el objeto y leer la imagen */
$im = new Imagick("imagen.jpg");

/* Recortar la imagen. */
$im->trimImage(0);

/* Imprimir la imagen */
header("Content-Type: image/" . $im->getImageFormat());
echo
$im;
?>

Ver también

add a note add a note

User Contributed Notes 3 notes

up
11
dhp
14 years ago
After operations that change the crop of the image, like trimImage does, IM preserves the old canvas and positioning info. If you need to do additional operations on the image based on the new size, you'll need to reset this info with setImagePage. This is the equivalent of the +repage command line argument.

<?php
$im
->trimImage(0);
$im->setImagePage(0, 0, 0, 0);
?>
up
6
Kirsty Forrester
11 years ago
The command for trim can return info which tells you the coordinates of where the image was cropped and the new dimensions of the trimmed image. I couldn't find an example for how to do that with this class, so here's how I did it:

<?php

// Create and trim image
$image = new Imagick('image.png');
$image->trimImage(0.3);

/**
* Imagick:: getImagePage() gives us an array with the width, height, x and y coordinates of the trimmed image
* However, the width and height are of the original image
*/
$imagePage = $image->getImagePage();
list(
$x, $y) = array($imagePage['x'], $imagePage['y']);

// So we need to reset the image page to get the right height and width of the trimmed image
$image->setImagePage(0, 0, 0, 0);
list(
$width, $height) = array($image->width, $image->height);

?>

This will leave you with two variables for the width and height of the trimmed image, plus two variables for the x-coordinate and y-coordinate of the trimmed area relative to the original image.

This information is useful when the image you're trimming is the difference between two images (e.g. from Imagick::compareImages(), and you want to crop the original image to the same size and position as the difference.
up
0
Mark van der Velden
16 years ago
This method requires ImageMagick version >= 6.2.8
To Top