简体   繁体   English

PHP GD库-在将一张GD半透明图像覆盖到另一张GD半透明图像上时发现意外的白点

[英]PHP GD library - Unexpected white spots found on overlaying one gd semi-transparent image over the other

Ive been working on this project for days now and for some reason just not able to get rid of an unexpected 1px by 1px (approx) white spot that apears in each and every tile that is processed. 我已经在这个项目上工作了几天,由于某种原因,我无法摆脱每个处理过的图块中出现的意外的1px x 1px(大约)白点。

Summary: 摘要:

I am using an original image ( say original.jpeg ) as referance to create a mosaic image ( say mosaic.jpeg which is approx 1000px by 1000px ) by merging totegher much much smaller jpeg images ( aprox 10px by 10px ). 我正在使用原始图像(例如original.jpeg)作为参考,通过合并更大得多的jpeg图像(aprox 10px x 10px)来创建马赛克图像(例如mosaic.jpeg大约为1000px x 1000px)。

I have a data set of approx 20,000 tile images to work with. 我有大约20,000个平铺图像的数据集可以使用。

Process so far 到目前为止的过程

  1. I have mapped the original.jpeg image to cut it up into 5px by 5px tiles, then found the average color of each tile and saved it for further use. 我已经映射了original.jpeg图像,将其切成5px x 5px的图块,然后找到了每个图块的平均颜色并将其保存以备将来使用。
  2. I have scanned all the (10x10) tile images and stored their individual average colors as well. 我已经扫描了所有(10x10)平铺图像,并存储了它们各自的平均颜色。
  3. i have calculated which tile image would fit closest to which tile of the original image by using Weighted ref : euclidean distance from the site mentioned. 我已经通过使用Weighted ref: 所述站点的欧几里得距离来计算出哪个瓷砖图像最适合原始图像的哪个瓷砖。
  4. I have managed to use PHP gd library to create a new truecolor image with all the matching tiles placed in the right position ( thus effectively creating a mosaic of the original.jpeg image ) 我设法使用PHP gd库创建了一个新的truecolor图像,并将所有匹配的图块放置在正确的位置(从而有效地创建了original.jpeg图像的马赛克)

The Problem 问题

I am not getting a clear mosaic i had expected, for some reason the tiles do not match properly. 我没有得到我所期望的清晰马赛克,由于某种原因,瓷砖无法正确匹配。

The Workaround 解决方法

Now due to lack of time i an using a quick fix where in i take the original image, give it some 50% opacity and overlay it on top of each tile while it is being placed into the final mosaic. 现在由于时间不足,我正在使用一种快速修复方法,在该方法中,我拍摄原始图像,使其具有50%的不透明度,然后将其放置在最终马赛克中时将其覆盖在每个图块的顶部。

NOTE: Although i am effectively overlaying the original image over the mosaic image, im NOT doing it in one go. 注意:尽管我有效地将原始图像覆盖在了马赛克图像上,但我暂时没有做到这一点。 The overlaying HAS to happen at each tile level. 叠加HAS发生在每个图块级别。

So in short : Before placing each tile at the correct position of the final mosaic, i do the following 1. get that particular section of the original image ( 5x5px ) 2. expand it to match the final tile size ( 10x10px ) 3. set transparency for the section 4. place it over the tile that will be placed 5. merge this new tile over the final mosaic at the corresponding position. 简而言之:在将每个图块放置在最终镶嵌图的正确位置之前,我需要执行以下操作:1.获取原始图像的特定部分(5x5px)2.展开以匹配最终图块大小(10x10px)3.设置第4部分的透明度。将其放置在将要放置的图块上。5.将此新图块在相应位置的最终镶嵌图上合并。

Here is the function i have created to overlay part of the image along with transparency set to it. 这是我创建的用于覆盖部分图像以及为其设置透明度的功能。

public function overlay($dImg, $sImg, $opacity = null) {

    // set default Opacity if not specified
    $opacity = (is_null($opacity)) ? $this->opacity : $opacity;

    // get width, height of sourceImage
    $sWidth = imagesx($sImg);
    $sHeight = imagesy($sImg);

    // get width height of final image
    $dWidth = imagesx($dImg);
    $dHeight = imagesy($dImg);

    $image = imagecreatetruecolor($dWidth, $dHeight);
    imagecopyresampled($image, $sImg, 0, 0, 0, 0, $dWidth, $dHeight, $sWidth, $sHeight);

    $background = imagecolorallocatealpha($image, 255, 255, 255, 127);        
    imagefill($image, 0, 0, $background);

    imagealphablending($image, true);

    imagecopymerge($dImg, $image, 0, 0, 0, 0, $dWidth, $dHeight, $opacity);

    imagedestroy($image);

    return $dImg;
}

THE REAL PROBLEM 真正的问题

in theory all of this seems perfectly fine. 从理论上讲,所有这些似乎都很好。 But the results have their own say in the matter. 但是结果在这件事上有自己的发言权。

I noticed an unusual almost 1x1px white patch at the start of every tile of the final mosaic. 我在最终马赛克的每个图块的开头都注意到了一个不寻常的近1x1px白色补丁。

This white patch only appears when the transparency technique above is applied. 仅当应用上述透明度技术时,才会出现此白色补丁。 It does not happen otherwise. 否则不会发生。

I am clueless as to why this is happening, Due to this white patch the entire image looks like it has white noise all over it and degrades the overall quality immensely. 对于为什么会这样,我一无所知。由于有白色斑点,整个图像看起来像整个表面都有白噪声,极大地降低了整体质量。

Please guide me in any direction as to why something like this could be happening. 请就任何可能发生这种情况的方向指导我。

your problem lies on these two lines: 您的问题在于以下两行:

$background = imagecolorallocatealpha($image, 255, 255, 255, 127);        
imagefill($image, 0, 0, $background);

you don't need those, because imagefill is used to fill an area that have same/similar color with the color located on the supplied coordinate, in your case 0, 0 (top left), when there is no adjacent similar color, then it just change the color at the given coordinate. 您不需要这些,因为imagefill用于使用提供的坐标上的颜色填充具有相同/相似颜色的区域,在您的情况下为0、0(左上),如果没有相邻的相似颜色,则它只是改变给定坐标的颜色。

you can use imagefilledrectangle instead, but i still think you don't need that, just comment out those 2 lines and see the result if it match your requirement, if not, then go ahead use imagefilledrectangle 您可以改用imagefilledrectangle ,但我仍然认为您不需要,只需注释掉这两行,然后查看结果是否符合您的要求,否则请继续使用imagefilledrectangle

imagefilledrectangle 图像填充矩形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM