将图像移动到底部
问题描述:
我正在用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']) {
部分是让我产生当场化身的大小不同。它工作正常。
答
这里是裁剪底部并将裁剪后的图像移动到底部的代码。
<?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;
}
参考文献:
答
我认为解决方案是以自下而上的方式构建虚拟形象。即鞋 - >裤子 - >衬衫 - >脸 - >发
(伪代码)
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
如果你看它有以下方法签名
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)
的imagecopy的方法通过改变$dst_x
和$dst_y
你可以实现我所描述的。
这是很难理解你的问题,但为什么不通过像素降低游说高度? – 2013-03-23 15:17:27
更新w /图片和更好的解释。我很难解释事情。抱歉。 :( – Anonymous 2013-03-23 15:24:07
@BenD我无法调整图像大小,这是由于用户可能会添加一组鞋或某些东西会占用这个空间。 – Anonymous 2013-03-23 15:28:35