single pixel manipulation with php GD

前端 未结 3 452
南笙
南笙 2021-01-23 18:26

First I\'m referring to a previous question Change image per pixel and save to db

I found that the html5 canvas isn\'t suitable because it\'s hard to keep the source-ima

3条回答
  •  清歌不尽
    2021-01-23 18:58

    With the help of ogres I got it partially working.

    What I do is manipulating a toplayer and after the manipulation, merge it with function imagecopy with the source-image. The output is a jpg image.

    The manipulation of the toplayer is done with this code

    for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
    $img = imagecreatefrompng("thecover.png");
        imagealphablending($img, false); // Turn off blending
        $white_color_transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
    
                $rgb = imagecolorat($img, $x, $y);
                $pixel_color = imagecolorsforindex($img, $rgb);
                if ($pixel_color['red'] == 255 && $pixel_color['green'] == 255 && $pixel_color['blue'] == 255){
                    for ($i = 0; $i < 200; $i++) {
                        imagesetpixel($img, rand(0,300), rand(0,300), $white_color_transparent);
                        }
                }
    }
    }
    

    The color of the toplayer is white, RGB (0,0,0). The script checks for that color and sets, at this point, 200 random pixels to transparent.

    It's setting a lot of pixels to transparent, but not the given 200.

    Someone who can help me out.

提交回复
热议问题