php - 将图像 move 到底部

标签 php image gd move pixels

我正在用 PHP GD 制作头像。化身的脚和图像底部之间有令人讨厌的空间。我想通过将头像“推”到底部来摆脱那个空间(见下文)。

这是我不喜欢的原始图像,以及我想要得到的图像:

enter image description here

有什么方法吗?谢谢。以下是用于图像生成的代码的主要部分。

$assets = array(
    "../assets/shirt/Default.png",
    "../assets/body/Default.png",
    "../assets/hair/Default.png",
    "../assets/eyes/Default.png",
    "../assets/eyebrows/Default.png",
    "../assets/mouth/Default.png",
    "../assets/pants/Default.png"
);

$baseImage = imagecreatefrompng($assets[0]);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);

foreach($assets as $item) {
    $newImage = imagecreatefrompng($item);
    imagecopy($baseImage, $newImage, 0, 0, 0, 0, 350, 550);

    imagealphablending($baseImage, true);
    imagesavealpha($baseImage, true);
}

if($_GET['x']) {

    $sizex = $_GET['x']; if($sizex > 350) $sizex = 350;
    $sizey = $_GET['y']; if($sizey > 550) $sizey = 550;

    $png = imagecreatetruecolor($sizex, $sizey);
    imagesavealpha($png, true);

    $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
    imagefill($png, 0, 0, $trans_colour);

    $blankImage = $png;
    imagealphablending($blankImage, true);
    imagesavealpha($blankImage, true);

    imagecopyresampled($blankImage, $baseImage, 0, 0, 0, 0, $sizex, $sizey, 350, 550);

    header("Content-type: image/png");
    imagepng($blankImage);
}
else {
    header("Content-type: image/png");
    imagepng($baseImage);
}

注意:该代码的 if($_GET['x']) { 部分允许我当场生成不同大小的头像。它工作正常。

最佳答案

下面是裁剪底部并将裁剪后的图像 move 到底部的代码。

<?php
example();
function example(){
    $img = imagecreatefrompng('http://i.stack.imgur.com/UUiMK.png');
    imagealphablending($img, true);
    imagesavealpha($img, true);

    // copy cropped portion
    $img2 = imageCropBottom($img);

    // output cropped image to the browser
    header('Content-Type: image/png');
    imagepng($img2);

    imagedestroy($img2);
}

function imageCropBottom($image) {
    $background1 = imagecolorat($image, 0, 0);
    $background2 = imagecolorat($image, 1, 1);

    $imageWidth = imageSX($image);
    $imageHeight = imageSY($image);
    $bottom = 0;

    for ($y = $imageHeight ; $y > 0 ; $y--) {
        for ($x = 0 ; $x < imagesx($image) ; $x++) {

            $imageColor = imagecolorat($image, $x, $y);
            if (($imageColor != $background1) && ($imageColor != $background2)) {
                $bottom = $y;
                break;
            }
        }
        if ($bottom > 0) break;
    }

    $bottom++;

    // create new image with padding
    $img = imagecreatetruecolor($imageWidth, $imageHeight);
    imagealphablending($img, true);
    imagesavealpha($img, true);

    $trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagefill($img, 0, 0, $trans_colour);

    // copy
    imagecopy($img, $image, 1, $imageHeight-$bottom, 1, 1, $imageWidth-2, $bottom-1);

    // Draw a black rectangle
    $black = imagecolorallocate($img, 0, 0, 0);
    imagerectangle($img, 0, 0, $imageWidth-1, $imageHeight-1, $black);


    // destroy old image cursor
    imagedestroy($image);
    return $img;
} 

引用资料:

关于php - 将图像 move 到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15588386/

相关文章:

php - 仅在一个站点上显示 PHP 错误

php - 获取嵌套关联数组中所有元素的路径和值

从无符号字符缓冲区创建图像

flash - 从图库中批量导出图像?

Drupal 安装问题。 GD 和 PDO 无法识别

PHP GD - 水平居中对齐文本并减小字体大小以将其保留在图像内

php - htmlentities 返回空字符串

php - CodeIgniter如果用户名存在则通过用户名获取用户信息,如果不存在则通过userID获取

html - 无法居中(垂直)链接的图像和文本

image - 在 NodeJS 中使用 GD 库复制和重新采样图像