我正在使用PHP GD制作头像。在化身的脚和图像的底部之间存在令人讨厌的空间。我希望通过将头像“推”到底部来消除那个空间(见下文)。
这是我不喜欢的原始图像,以及我想要的图像:
有这方法吗?谢谢。下面是用于生成图像的代码的主要部分。
$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']) {
部分是允许我在现场生成不同大小的头像。它工作正常。
答案 0 :(得分:3)
以下是裁剪底部并将裁剪后的图像移动到底部的代码。
<?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;
}
参考文献:
答案 1 :(得分:2)
我认为解决方案是以自下而上的方式构建头像。即鞋 - &gt;裤子 - &gt;衬衫 - &gt;脸 - &gt;发
(伪代码)
position = (x,y) // where y is the height of the canvas initially
if(need(shoe)){
position = position - shoe.height
add shoe at position
}
if(need(pant)) {
position = position - pant.height
add pant at position
}
... and so on
如果你看一下imagecopy方法,它有以下方法签名
bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
通过改变$dst_x
和$dst_y
,您可以实现我所描述的目标。