GD color to texture

Discussion in 'PHP' started by dcgamers, May 24, 2010.

  1. #1
    Would it be remotely possible to, using GD library and PHP, parse a given image and find a specified color and replace it with an image texture?

    I know how to replace it with another color, but a texture I don't. I couldn't find the solution anywhere :/

    Please help?
     
    dcgamers, May 24, 2010 IP
  2. lordspace

    lordspace Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1st, remotely ? I'd download the image first and then work with it.
    2nd, if GD can't do it, maybe you can read the docs of ImageMagick
     
    lordspace, May 24, 2010 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    I don't know how well this would work, but it should produce something close.

    I'd start by creating a new image the size of the image to be worked on, and using imagecopy to tile in the texture.
    Then I would use imagecolortransparent and imagecopymerge to set the desired color to transparent in the source image and copy the source image over the newly created texture tiled image.
     
    joebert, May 25, 2010 IP
  4. dcgamers

    dcgamers Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Joebert: I tried what you said but unfortunately i am getting a problem with the transparency.
    
    <?php
    $tile = $_GET['img']; 
    header( "Content-type: image/png" );    
    $src = imagecreatefrompng("tile1.png");    
    $color = imagecolorclosest($src, 0, 148, 255);
    imagecolortransparent($src, $color);
    $new = imagecreatetruecolor(50, 50);
    imagealphablending($src, true);
    imagesavealpha($src, true); 
    imagecopy($new, $src, 0,0,0,0,50,50);
    $texture = imagecreatefrompng("_volanic_real.png");
    imagecopymerge($new, $texture, 0, 0, 0, 0, 50, 50, 0);
    $color = imagecolorclosest($src, 0, 148, 255);
    imagecolortransparent($src, $color);
    imagepng( $new );
    imagedestroy($new);
    ?>
    
    PHP:
    This just prints the image that I am trying to replace with a texture, but instead of the transparent part---I just get black. :confused:

    Help would be appreciated.
     
    dcgamers, May 25, 2010 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    I think you had too much transparency stuff going on. This worked out for me.

    <?php
       
    $src = imagecreatefrompng('pacman.png');    
    $color = imagecolorclosest($src, 27, 26, 55);
    imagecolortransparent($src, $color);
    imagealphablending($src, true);
    imagesavealpha($src, true);
    
    $texture = imagecreatefrompng('pattern.png');
    
    $new = imagecreatetruecolor(460, 460);
    
    imagecopy($new, $texture, 0, 0, 0, 0, 460, 460);
    imagecopymerge($new, $src, 0, 0, 0, 0, 460, 460, 100);
    
    imagepng($new, 'test.png');
    imagedestroy($new);
    imagedestroy($src);
    imagedestroy($texture);
    
    ?>
    PHP:
    Untitled-2.jpg
     
    joebert, May 26, 2010 IP