PHP Imagick使用黑色背景调整大小

PHP Imagick使用黑色背景调整大小,php,resize,imagick,Php,Resize,Imagick,我正在使用imagick扩展编写一个PHP脚本。我想让脚本做的是拍摄一张用户上传的图像,并从中创建一个200x128的缩略图 这不是唯一的事。显然,并非所有图像都适合200x128的纵横比。所以我想让脚本用黑色背景来填补空白 现在,图像会调整大小,但没有黑色背景,大小也不正确。基本上,图像应始终为200x128。调整大小后的图像将位于中间,其余内容将用黑色填充 有什么想法吗 这是我的密码: function portfolio_image_search_resize($image) {

我正在使用imagick扩展编写一个PHP脚本。我想让脚本做的是拍摄一张用户上传的图像,并从中创建一个200x128的缩略图

这不是唯一的事。显然,并非所有图像都适合200x128的纵横比。所以我想让脚本用黑色背景来填补空白

现在,图像会调整大小,但没有黑色背景,大小也不正确。基本上,图像应始终为200x128。调整大小后的图像将位于中间,其余内容将用黑色填充

有什么想法吗

这是我的密码:

function portfolio_image_search_resize($image) {

    // Check if imagick is loaded. If not, return false.
    if(!extension_loaded('imagick')) { return false; }

    // Set the dimensions of the search result thumbnail
    $search_thumb_width = 200;
    $search_thumb_height = 128;

    // Instantiate class. Then, read the image.
    $IM = new Imagick();
    $IM->readImage($image);

    // Obtain image height and width
    $image_height = $IM->getImageHeight();
    $image_width = $IM->getImageWidth();

    // Determine if the picture is portrait or landscape
    $orientation = ($image_height > $image_width) ? 'portrait' : 'landscape';

    // Set compression and file type
    $IM->setImageCompression(Imagick::COMPRESSION_JPEG);
    $IM->setImageCompressionQuality(100);
    $IM->setResolution(72,72);
    $IM->setImageFormat('jpg');

    switch($orientation) {

        case 'portrait':

            // Since the image must maintain its aspect ratio, the rest of the image must appear as black
            $IM->setImageBackgroundColor("black");

            $IM->scaleImage(0, $search_thumb_height);

            $filename = 'user_search_thumbnail.jpg';

            // Write the image
            if($IM->writeImage($filename) == true) {
                return true;
            }
            else {
                return false;
            }
            break;

        case 'landscape':

            // The aspect ratio of the image might not match the search result thumbnail (1.5625)
            $IM->setImageBackgroundColor("black");

            $calc_image_rsz_height = ($image_height / $image_width) * $search_thumb_width;

            if($calc_image_rsz_height > $search_thumb_height) {
                $IM->scaleImage(0, $search_thumb_height);
            }
            else {
                $IM->scaleImage($search_thumb_width, 0);
            }

            $filename = 'user_search_thumbnail.jpg';

            if($IM->writeImage($filename) == true) {
                return true;
            }
            else {
                return false;
            }

        break;

    }

}
exec('convert-define jpeg:size=400x436 big_image.jpg-auto-oriente-thumb200x218-unsharp 0x.5 thumbnail.gif')

您需要安装imagemagick

sudo-apt-get-install-imagemagick

看看:


它显示了更多的示例以及如何使用您选择的背景颜色填充缩略图。

我知道它很旧,但经过长期尝试,我找到了答案:

您需要使用thumbnailimage ()

将$bestfit和$fill都设置为true,如下所示:

$image->thumbnailImage(200, 128,true,true);

可以画一个较小的图像,比如说128x128px在一个2x128px画布的中间?我对Imagick了解不多,但是从我的图形知识来看,我认为你应该创建一个带有黑色填充的矩形并在上面加上你的缩略图。这对Imagick来说应该不难,对吧?PS:试着把行
$IM->setImageBackgroundColor(“黑色”)
在resizing语句之后,我认为这个问题特定于imagemagick的PHP包装器iMagick