PHP 8.3.4 Released!

Imagick::trimImage

(PECL imagick 2, PECL imagick 3)

Imagick::trimImageGörüntünün kenarlarını siler

Açıklama

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

Artalan rengi olarak görüntünün kenarlarını siler. Bu yöntem, derleme sırasında ImageMagick kütüphanesinin 6.2.9 veya sonraki bir sürümü kuruluysa kullanılabilir.

Bağımsız Değişkenler

benzerlik

Renkler arasındaki azami benzerlik. Örneğin bu değere 10 atarsanız, renk kırmızı ise ve kırmızının koyulukları 100 ve 102 ise bu iki renk aynı kabul edilir. Öntanımlı olarak hedefin belli bir piksel rengi ile tam olarak eşleşmesi gerekir. Ancak, çoğu durumda iki renk arasında küçük bir fark vardır. benzerlik bağımsız değişkeni iki rengin aynı kabul edilebilmesi için ne kadarlık bir farkın kabul edilebilir olduğunu belirler. Bu bağımsız değişken tanecik aralığında bir değişimi ifade eder.

Dönen Değerler

Başarı durumunda true döner.

Hatalar/İstisnalar

Hata durumunda bir ImagickException istisnası oluşur.

Örnekler

Örnek 1 - Imagick::trimImage() örneği

Görüntünün kenarlarını kırpıp tarayıcıda gösterelim.

<?php
/* Bir görüntüyü okuyarak nesneYi oluştur */
$im = new Imagick("image.jpg");

/* Görüntüyü kırp */
$im->trimImage(0);

/* ve çıktıla */
header("Content-Type: image/" . $im->getImageFormat());
echo
$im;
?>

Ayrıca Bakınız

add a note

User Contributed Notes 4 notes

up
14
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
8
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
1
tamucola
2 years ago
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);
}
up
0
Mark van der Velden
16 years ago
This method requires ImageMagick version >= 6.2.8
To Top