【问题标题】:GD Image Library merge 2 images with "Collision Detection"GD 图像库将 2 张图像与“碰撞检测”合并
【发布时间】:2013-01-09 05:21:01
【问题描述】:

我有两张图片,白色背景上的大文字。长度各不相同,但文本始终向左对齐,因此每个图像的右侧基本上都有可用空间。我现在想将这两个图像合并为一个,并将它们尽可能靠近地移动,而不会使文本“碰撞”。

我想以某种方式检查每像素列的基数是否有除白色以外的其他颜色(从右侧开始),所以我知道文本从多少像素开始。

【问题讨论】:

  • 到目前为止,您尝试了什么?那么,您的具体编程问题是什么?到目前为止,您只留下了一些要求,所以这听起来更像是一个谷歌请求而不是一个真正的问题。
  • 我尝试获取文本长度(以字符为单位, strlen() )并将其应用于我的合并函数,但由于文本被扭曲(用于验证码)这不起作用,有时它们重叠
  • 显然是的。你真的没想到它会解决这个问题,对吧?一个更好的变体是 imagettfbox 但它无法击败验证码。您可能希望删除可以在此站点上搜索的页面的白色部分。

标签: php image image-processing gd


【解决方案1】:

找到解决方案,从图像中去除所有空白的简洁功能:

function stripWhitespace($img) {
        //find the size of the borders
$b_top = 0;
$b_btm = 0;
$b_lft = 0;
$b_rt = 0;

//top
for(; $b_top < imagesy($img); ++$b_top) {
  for($x = 0; $x < imagesx($img); ++$x) {
    if(imagecolorat($img, $x, $b_top) != 0xFFFFFF) {
       break 2; //out of the 'top' loop
    }
  }
}

//bottom
for(; $b_btm < imagesy($img); ++$b_btm) {
  for($x = 0; $x < imagesx($img); ++$x) {
    if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0xFFFFFF) {
       break 2; //out of the 'bottom' loop
    }
  }
}

//left
for(; $b_lft < imagesx($img); ++$b_lft) {
  for($y = 0; $y < imagesy($img); ++$y) {
    if(imagecolorat($img, $b_lft, $y) != 0xFFFFFF) {
       break 2; //out of the 'left' loop
    }
  }
}

//right
for(; $b_rt < imagesx($img); ++$b_rt) {
  for($y = 0; $y < imagesy($img); ++$y) {
    if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0xFFFFFF) {
       break 2; //out of the 'right' loop
    }
  }
}

//copy the contents, excluding the border
$newimg = imagecreatetruecolor(
    imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));

imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));
return $newimg;
    }

从这里:Crop whitespace from image in PHP

【讨论】:

    猜你喜欢
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    相关资源
    最近更新 更多