Imagick::trimImage

(PECL imagick 2, PECL imagick 3)

Imagick::trimImage β€” УдаляСт края с изобраТСния

ОписаниС

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

УдаляСт края, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΡΠ²Π»ΡΡŽΡ‚ΡΡ Ρ†Π²Π΅Ρ‚ΠΎΠΌ Ρ„ΠΎΠ½Π° с изобраТСния. ΠœΠ΅Ρ‚ΠΎΠ΄ доступСн, Ссли ΠΌΠΎΠ΄ΡƒΠ»ΡŒ Imagick скомпилировали с вСрсиСй ImageMagick 6.2.9 ΠΈΠ»ΠΈ ΡΡ‚Π°Ρ€ΡˆΠ΅.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

fuzz

По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Ρ†Π΅Π»ΡŒ Π΄ΠΎΠ»ΠΆΠ½Π° Ρ‚ΠΎΡ‡Π½ΠΎ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½ΠΎΠΌΡƒ Ρ†Π²Π΅Ρ‚Ρƒ пиксСля. Однако Π²ΠΎ ΠΌΠ½ΠΎΠ³ΠΈΡ… случаях Π΄Π²Π° Ρ†Π²Π΅Ρ‚Π° ΠΌΠΎΠ³ΡƒΡ‚ Π½Π΅Π·Π½Π°Ρ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ ΠΎΡ‚Π»ΠΈΡ‡Π°Ρ‚ΡŒΡΡ. Π Π°Π·ΠΌΡ‹Ρ‚Ρ‹ΠΉ элСмСнт изобраТСния опрСдСляСт, насколько допустимо, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π΄Π²Π° Ρ†Π²Π΅Ρ‚Π° ΡΡ‡ΠΈΡ‚Π°Π»ΠΈΡΡŒ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Ρ‹ΠΌΠΈ. Π­Ρ‚ΠΎΡ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ прСдставляСт собой ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΊΠ²Π°Π½Ρ‚ΠΎΠ²ΠΎΠ³ΠΎ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π°.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π² случаС ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Ρ‹ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true.

Ошибки

Ѐункция выбрасываСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ImagickException, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Imagick::trimImage():

ΠžΠ±Ρ€Π΅ΠΆΡŒΡ‚Π΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, Π·Π°Ρ‚Π΅ΠΌ ΠΎΡ‚ΠΎΠ±Ρ€Π°Π·ΠΈΡ‚Π΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π΅.

<?php
/* Π‘ΠΎΠ·Π΄Π°ΠΉΡ‚Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΈ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² */
$im = new Imagick("image.jpg");

/* ΠžΠ±Ρ€Π΅ΠΆΡŒΡ‚Π΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. */
$im->trimImage(0);

/* Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ */
header("Content-Type: image/" . $im->getImageFormat());
echo
$im;
?>

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • Imagick::getQuantumDepth() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρƒ Π³Π»ΡƒΠ±ΠΈΠ½Ρ‹
  • Imagick::getQuantumRange() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρƒ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° спСктра Imagick
  • imagecropauto() - ΠžΠ±Ρ€Π΅Π·Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ автоматичСски Π½Π° основС Π·Π°Π΄Π°Π½Π½ΠΎΠ³ΠΎ Ρ€Π΅ΠΆΠΈΠΌΠ°
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
16
dhp ΒΆ
17 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 ΒΆ
13 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
2
tamucola ΒΆ
4 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 ΒΆ
18 years ago
This method requires ImageMagick version >= 6.2.8