Giorgio
Giorgio

Reputation: 1613

Draw a black line at the bottom of an image, PHP

I wrote this code that was supposed to draw a black line of 1px at the bottom of an image, but it isn't working as expected and it generates a totally black image:

    $img = imagecreatefrompng("image.png");    
    $dest = imagecreatetruecolor(50, 25);
    imagecopy($dest, $img, 0, 0, $px + $position[$pos][0], $py + $position[$pos][1] , 50, 25);
    $w = imagesx($dest);
    $h = imagesy($dest);
    $out = ImageCreateTrueColor(imagesx($dest),imagesy($dest)) or die('Problem In Creating image');

    for( $x=0; $x<$w; $x++) {
        imagesetpixel($out, $x, $h, imagecolorallocate($out, 0, 0, 0));
    }

Where's the problem?

Upvotes: 1

Views: 382

Answers (2)

Marc B
Marc B

Reputation: 360592

The problem is that you're drawing your line on the $out image, which has nothing in it.

  1. You load up $img (the original).
  2. you create $dest
  3. you resize $img into $dest
  4. you create $out
  5. you draw the line on $out.

$out never gets a copy of either the original or resized image, so it stays at the default all-black. You then draw a black line on a black image... ending up with a black image.

Upvotes: 1

Avanche
Avanche

Reputation: 1810

Your $y isn't set, I think it's just a typo, you have set $h, and used $y instead.

Upvotes: 1

Related Questions