将图像移动到底部

问题描述:

我正在用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']) {部分是让我产生当场化身的大小不同。它工作正常。

+0

这是很难理解你的问题,但为什么不通过像素降低游说高度? – 2013-03-23 15:17:27

+0

更新w /图片和更好的解释。我很难解释事情。抱歉。 :( – Anonymous 2013-03-23 15:24:07

+0

@BenD我无法调整图像大小,这是由于用户可能会添加一组鞋或某些东西会占用这个空间。 – Anonymous 2013-03-23 15:28:35

这里是裁剪底部并将裁剪后的图像移动到底部的代码。

<?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你可以实现我所描述的。

+0

这很麻烦,因为鞋子可以有不同的高度,对此有什么建议吗? – Anonymous 2013-09-30 06:27:44

+0

使用getimagesize()来动态查找高度或更改资产数组转换为具有所有必要参数的嵌套哈希。 – Josnidhin 2013-09-30 09:44:14