简体   繁体   中英

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.

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 ).

I have a data set of approx 20,000 tile images to work with.

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.
  2. I have scanned all the (10x10) tile images and stored their individual average colors as well.
  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.
  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 )

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.

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.

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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