abiku
abiku

Reputation: 2246

Create colorr image in php

Don't know what i'm doing wrong. Want to create a simple solid color png image.

$im = @imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00'   );
$pdir = '/Applications/AMPPS/www/images/test.png';
imagepng($im,$pDir,9);
imagedestroy($im);

It doesn't work. have no image in the dir even it it is ok for sure and has proper rights. I only see some strange digits begins with PNG displayd on the page

Upvotes: 0

Views: 30

Answers (1)

Ryan Smith
Ryan Smith

Reputation: 694

To display your image, add the following to the very top of your script.

header("Content-type: image/png");

EDIT: Also, your variables for the output directory don't match. One is $pDir and $pdir.

EDIT 2: Here's your final code. imagepng() seems to either save the image to a file or display it, but not at the same time. So you will need two of them. Just simply don't pass a file path to the one that you want to display the image.

header("Content-type: image/png");

$im = @imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00' );

$pdir = '/path/test.png';

imagepng($im);
imagepng($im,$pdir,9);

imagedestroy($im);

Upvotes: 2

Related Questions