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);
?>Imagick::trimImage
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Imagick::trimImage
Референца за `imagick.trimimage.php` со подобрена типографија и навигација.
Imagick::trimImage
(PECL imagick 2, PECL imagick 3)
Imagick::trimImage — Отстрани рабови од сликата
= NULL
Remove edges that are the background color from the image. This method is available if Imagick has been compiled against ImageMagick version 6.2.9 or newer.
Параметри
fuzz-
Стандардно, целта мора точно да одговара на одредена боја на пиксел. Сепак, во многу случаи две бои може да се разликуваат за мала количина. Членот fuzz на сликата дефинира колку толеранција е прифатлива за да се сметаат две бои за исти. Овој параметар го претставува варијацијата на опсегот на квант.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true на успешен исход.
Errors/Exceptions
Фрла ImagickException при грешка.
Примери
Пример #1 Користење Отстранете ги рабовите што се во бојата на позадината од сликата. Овој метод е достапен ако Imagick е компајлиран со верзијата на ImageMagick 6.2.9 или понова.:
Imagick::trimImage()
<?php
/* Create the object and read the image in */
$im = new Imagick("image.jpg");
/* Trim the image. */
$im->trimImage(0);
/* Ouput the image */
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
?>Види Исто така
- Исечете слика, а потоа прикажете ја во прелистувачот. - Добива длабочина на кванти
- Imagick::getQuantumDepth() - Враќа опсег на кванти на Imagick
- imagecropauto() - Исечете слика автоматски користејќи еден од достапните режими
Белешки од корисници 4 белешки
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.fuzz needs getQuantum
/**
* @param string $imagePath
* @param float $fuzz 0.1=10%
* @return bool
* @throws ImagickException
*/
public static function trimImage($imagePath, $fuzz = 0.1)
{
if (empty($imagePath)) {
return false;
}
$imagePath = realpath($imagePath);
if (!is_file($imagePath)) {
return false;
}
if (!class_exists('Imagick')) {
return false;
}
$im = new Imagick($imagePath);
$result = $im->trimImage($fuzz * $im::getQuantum());
if (!$result) {
return false;
}
return $im->writeImage($imagePath);
}