简体   繁体   English

使用Imagick和PHP删除空格,然后另存为透明PNG

[英]Remove whitespace using Imagick and PHP, then save as transparent PNG

I've got an image and want to remove all whitespace around it, and then save it as a transparent PNG. 我有一个图像,想要删除其周围的所有空白,然后将其另存为透明PNG。 I'm using Imagick in PHP to do so, but my script doesn't seem to function properly. 我在PHP中使用了Imagick,但是我的脚本似乎无法正常运行。

<?php
$im = new Imagick("http://images.icecat.biz/img/norm/high/14688888-2862.jpg");

$im->borderImage("#ffffff", 20, 20);
$im->trimImage(0.3);

$im->setImagePage($im->getImageWidth(), $im->getImageHeight(), 0, 0);
$im->setImageFormat("png");

header("Content-Type: image/" . $im->getImageFormat());
echo $im->getImageBlob();
?>

What do I need to do to remove all white (and close to white) areas at the borders? 我需要怎么做才能去除边界处的所有白色(和接近白色)区域? And when that is done, can I easily resize the image to crop all of the transparency? 完成后,是否可以轻松调整图像大小以裁剪所有透明度?

The fuzz factor needs to be a quantum scaled value, not just for this function but almost all functions that take 'fuzz' as a parameter. 模糊因子必须是一个量子标度值,不仅对于此函数,而且几乎所有以“模糊”为参数的函数都应如此。

ie you need to scale it up to the quantum range. 也就是说,您需要将其放大到量子范围。

$im->trimImage(0.3 * \Imagick::getQuantum());

Or if you are using an earlier version of Imagick that doesn't have that method, then instead do: 或者,如果您使用的Imagick早期版本没有该方法,请执行以下操作:

$range = $image->getQuantumRange();
$image->trimImage(0.3 * $range['quantumRangeLong']);

The reason for this is to allow precise control over the pixel matching. 这样做的原因是允许精确控制像素匹配。 If the value was passed in as a float value in the range 0-1 it would not possible to have exact control over the value that was used for matching. 如果将值作为0-1范围内的浮点值传递,则不可能完全控制用于匹配的值。

By instead using an integer value (for versions of Imagick that do not have HDRI enabled) it allows you to precisely control the values that are compared for the operation. 通过使用整数值(对于未启用HDRI的Imagick版本),它可以精确控制为操作进行比较的值。

You need something like an autocrop based on pixel values, I think this will help: http://www.imagemagick.org/script/command-line-options.php#trim 您需要类似基于像素值的自动裁剪功能,我认为这会有所帮助: http : //www.imagemagick.org/script/command-line-options.php#trim

you might also like: http://fmwconcepts.com/imagemagick/autotrim/index.php 您可能还会喜欢: http : //fmwconcepts.com/imagemagick/autotrim/index.php

source: http://www.imagemagick.org/discourse-server/viewtopic.php?t=10843 来源: http : //www.imagemagick.org/discourse-server/viewtopic.php?t=10843

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM