php - Imagick 调整大小、居中和稀疏填充问题

标签 php resize imagemagick center imagick

我的最终目标是将输入图像调整为 100 像素宽、125 像素高。一些输入图像具有不同的宽高比,因此我希望它们位于 100x125 的容器中,背景稀疏地填充了它们的边缘颜色。

好的,所以这适用于基本的调整大小:

$image = new Imagick($imgFile);
$image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
$image->writeImage("$Dir/$game.png");
header("Content-type: ".$image->getImageFormat());
echo $image;
$image->clear();
$image->destroy();

但是,我已经搜索了几个小时,但找不到适用于 PHP 的 Imagick 库的简单“这就是您在 Canvas 中居中图像的方式”。一切都是为了实际的 ImageMagick 转换应用程序,这并不是我真正想要的。我已经尝试将调整大小的图像合成为具有设置宽度和高度的空 newImage,但它似乎只是覆盖尺寸,无论合成类型如何,将重力设置为中心,然后将范围设置为 100x125 没有效果(它总是位于 0,0,并尝试将 y 偏移量设置为 ((125-imageheight)/2) 导致偏移量超出应有的值)

编辑:

    $imageOutput = new Imagick();
    $image = new Imagick($imgFile);
    $image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
    $imageOutput->newImage(100, 125, new ImagickPixel('black'));
    $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((125 - $image->getImageHeight()))/2 );
    $imageOutput->setImageFormat('png');
    $imageOutput->writeImage("$Dir/$game.png");
    header("Content-type: ".$imageOutput->getImageFormat());
    echo $imageOutput;
    $image->clear();
    $image->destroy();

所以我开始了居中工作,重力显然对实际图像没有影响。

我完全不知道我什至会从哪里开始尝试使用该库在 PHP 中重新创建命令行边缘稀疏填充。

最佳答案

我最终使用 Imagick 和 shell 调用的组合来转换自身,我最终将重写它以完全使用 shell 调用。我还更改了尺寸,这是代码:

    $imageOutput = new Imagick(); // This will hold the resized image
    $image = new Imagick($imgFile); // Open image file
    $image->resizeImage(120,0, Imagick::FILTER_LANCZOS, 1, false); // Resize it width-wise
    $imageOutput->newImage(120, 150, "none"); // Make the container with transparency
    $imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((150 - $image->getImageHeight())/2) ); // Center the resized image inside of the container
    $imageOutput->setImageFormat('png'); // Set the format to maintain transparency
    $imageOutput->writeImage("$Dir/$game.temp.png"); // Write it to disk
    $image->clear(); //cleanup -v
    $image->destroy(); 
    $imageOutput->clear();
    $imageOutput->destroy();
    //Now the real fun
    $edge = shell_exec("convert $Dir/$game.temp.png -channel A -morphology EdgeIn Diamond $Dir/$game.temp.edge.png"); // Get the edges of the box, create an image from just that
    $shepards = shell_exec("convert $Dir/$game.temp.edge.png txt:- | sed '1d; / 0) /d; s/:.* /,/;'"); // get the pixel coordinates
    $final = shell_exec("convert $Dir/$game.temp.edge.png -alpha off -sparse-color shepards '$shepards' png:- | convert png:- $Dir/$game.temp.png -quality 90 -composite $Dir/$game.jpg"); // Sparse fill the entire container using the edge of the other image as shepards , then composite that on top of this new image
    unlink("$Dir/$game.temp.png"); // cleanup temp files
    unlink("$Dir/$game.temp.edge.png");
    set_header_and_serve("$Dir/$game.jpg"); // serve the newly created file

关于php - Imagick 调整大小、居中和稀疏填充问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12327423/

相关文章:

css - 全宽容器中的响应图像

Python、Imagemagick 和子进程调用

python - 以编程方式检测图像是否有边框(返回 bool 值)

php - 使用 php/mysql 在数据集之间显示/创建空白行并打破表数据

php 输入测试/mysql_real_escape_string/pdo

php - 如何使用另一个数组中的字符串作为位置名称在 PHP 中创建数组?

powershell - imagemagick 中 STDIN/STDOUT 的正确语法

php mysql连接困惑

jquery - 将背景图像缩放到浏览器的大小

php - 良好的图像调整框架