Jump to content

Problems with image manipulation


Tanis

Recommended Posts

So far i've got:

<?$width = 20;$height = 20;$text=$_GET["text"];for ($i = 0; $i < strlen($text); ++$i){$letter = $text{$i};$filename = "/goodies/letters/$letter.gif";$x = $width-20;$im = imagecreatetruecolor($width, 20);$image = imagecreatefromGIF($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, $width, 20, 20, 20);$width = $width+20;}header('Content-type: image/gif');imageGIF($im);imageDestroy($im);?>

But to no avail...All I get is a big black box (good thing is that it changes by 20 pixels for every letter you have)PS- Each source image is 20x20.View- clickyI tried removing

$im = imagecreatetruecolor($width, 20);

but that just returned http://www.habbground.com/txtst.php?text=ABCDE

Link to comment
Share on other sites

The destination width is the width of the image when you put it in the new image. If you move a 20x20 image and say the width is 40, the width of the copied image will be 40x20, not 20x20, it will stretch the image. The imagecopyresampled function is usually used to resize an image, and that is how you do it. You don't need to resize, you just want to copy. So width and height stay the same, y doesn't need to change, you just need to keep sliding x down.

Link to comment
Share on other sites

Alright, changed it to

<?0 = $x;$text=$_GET["text"];for ($i = 0; $i < strlen($text); ++$i){$letter = $text{$i};$filename = "/goodies/letters/$letter.gif";$im = imagecreatetruecolor(20, 20);$image = imagecreatefromGIF($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20);$x = $x+20;}header('Content-type: image/gif');imageGIF($im);imageDestroy($im);?>

and it doesn't output anything.What am I missing (most likely something really obvious)?

Link to comment
Share on other sites

Firtst: You are creating the image(s) in every loop (a new image in every loop), I would put his above the loop (and remove it from the loop):

$im = imagecreatetruecolor( 20*strlen($text), 20 );

Don't forget to destroy $image in the end of each loop (as you create a new every time)Than you might have a problem with GIF-transparancy, nut sure on how to solve that though...Last: I think this line is wrong:

0 = $x;

It's backwardsHope that helped..Good Luck and Don't Panic!

Link to comment
Share on other sites

  • 2 weeks later...

I know WHY its black, i just need to know how to change it.Although it would be ok if the user could simply input a background color (in hex), it would be much better if there was a way to make the image transparent.

Link to comment
Share on other sites

That comment that Mr Chisol links to explains how to make an image where the background is initially transparent, but that will only work if you are creating a PNG. JPEG images don't support transparency at all, and GIF images need paletted transparency, not alpha transparency. It will probably be better to output a PNG anyway instead of a GIF or JPG.

Link to comment
Share on other sites

<?$x = 0;$text=$_GET["text"];$im = imagecreatetruecolor( 20*strlen($text), 20 );for ($i = 0; $i < strlen($text); ++$i){$letter = $text[$i];$filename = "http://www.habbground.com/goodies/letters/$letter.png";$image = imagecreatefromPNG($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20);$x = $x+20;}header('Content-type: image/png');imagePNG($im);imageDestroy($im);?>

Returns a totally black image (varying in size depending on the # of letters inputted).

Link to comment
Share on other sites

I'm not sure if you can use an HTTP path for the imagecreate functions. Try using the local filesystem path instead of a web path. Also, while you're debugging this, remove the image output so you can see HTML and see if you are getting any error messages. Add some debug text as well.

<?$x = 0;$text=$_GET["text"];echo "text: {$text}<br>";$im = imagecreatetruecolor( 20*strlen($text), 20 );for ($i = 0; $i < strlen($text); ++$i){$letter = $text[$i];echo "letter: {$letter}<br>";$filename = "http://www.habbground.com/goodies/letters/$letter.png";echo "filename: {$filename}<br>";$image = imagecreatefromPNG($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20);imageDestroy($image);$x = $x+20;}imageDestroy($im);?>

Link to comment
Share on other sites

It should be working... outputting the variables returnedtext: ABCDEletter: Afilename: http://www.habbground.com/goodies/letters/A.pngletter: Bfilename: http://www.habbground.com/goodies/letters/B.pngletter: Cfilename: http://www.habbground.com/goodies/letters/C.pngletter: Dfilename: http://www.habbground.com/goodies/letters/D.pngletter: Efilename: http://www.habbground.com/goodies/letters/E.pngCopy/pasting the links returns an image.

Link to comment
Share on other sites

I added in the transparency code, ending up with

<?$x = 0;$text=$_GET["text"];$im = imagecreatetruecolor( 20*strlen($text), 20 );imagealphablending( $im, false );$col = imagecolorallocate( $im, 0, 0, 0, 127 );imagefilledrectangle( $im, 0, 0, 20*strlen($text), 20, $col );imagealphablending( $im, true );for ($i = 0; $i < strlen($text); ++$i){$letter = $text[$i];$filename = "http://www.habbground.com/goodies/letters/$letter.png";$image = imagecreatefromPNG($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20);$x = $x+20;}header('Content-type: image/png');imagealphablending( $im, false );imagesavealpha( $im, true );imagePNG($im);imageDestroy($im);?>

And it still has a black background.

Link to comment
Share on other sites

If you're still getting black, then there is something wrong with the copy. You didn't add the code I posted above, add this to help debug:

for ($i = 0; $i < strlen($text); ++$i){  $letter = $text[$i];  $filename = "http://www.habbground.com/goodies/letters/$letter.png";  $image = imagecreatefromPNG($filename);  if (!$image)	die("could not open {$filename}");  if (!imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20))	die("could not copy");  $x = $x+20;}

Link to comment
Share on other sites

OK, that means it is not copying the transparency information from the letters and is using black for transparent. That might mean that the letters do not have transparency saved with them, or maybe PHP is just not saving the alpha mask when you copy. Here are some notes from the reference for imagecreatefrompng:

If you're trying to load a translucent png-24 image but are finding an absence of transparency (like it's black), you need to enable alpha channel AND save the setting. I'm new to GD and it took me almost two hours to figure this out.<?php$imgPng = imageCreateFromPng($strImagePath);imageAlphaBlending($imgPng, true);imageSaveAlpha($imgPng, true);/* Output image to browser */header("Content-type: image/png");imagePng($imgPng); ?>
***CORRECTION*** to 07-Jun-2004 01:14 imageAlphaBlending must be false, NOT TRUE$imgPng = imageCreateFromPng($strImagePath);imageAlphaBlending($imgPng, false); <<<FALSEimageSaveAlpha($imgPng, true);
A correction... If you are to set it to FALSE, then not only does it add the transparency to wherever you put it, but it takes the transparency out of the TTF files. Setting it to TRUE however, keeps your transparency both in your PNG file, and in any TTF text you make apply to the image.
I also saw a note that most imagecopy functions do not copy transparency, but imagecopymerge does:http://www.php.net/manual/en/function.imagecopymerge.phpAs a workaround, if you don't even need transparency for this, just set the background color to white or whatever it is if you have a solid background.
Link to comment
Share on other sites

  • 2 weeks later...
What did you do to make it work? What does the final code look like?
<?$x = 0;$text=$_GET["text"];$im = imagecreatetruecolor( 20*strlen($text), 20 );imagealphablending( $im, false );imagesavealpha( $im, true );$col = imagecolorallocate( $im, 0, 0, 0, 127 );imagefilledrectangle( $im, 0, 0, 20*strlen($text), 20, $col );for ($i = 0; $i < strlen($text); ++$i){$letter = $text[$i];$filename = "http://www.habbground.com/goodies/letters/$letter.png";$image = imagecreatefromPNG($filename);imagecopyresampled($im, $image, $x, 0, 0, 0, 20, 20, 20, 20);$x = $x+20;}header('Content-type: image/png');imagealphablending( $im, false );imagesavealpha( $im, true );imagePNG($im);imageDestroy($im);?>

There were some issues with images I had too.PS- sorry about the really long delay, I lost my link to the thread :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...