...

View Full Version : functions use



romalong
06-07-2004, 12:17 PM
i was wonder of creation a function that creates images with different text dependant on its argument:



function image_create($string){
header("Content-type: image/png");
$im = imagecreatefrompng("button.png");
$colour = imagecolorallocate($im, 100, 100, 100);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 4, $string, $colour);
imagepng($im);
imagedestroy($im);
}

echo image_create("home");
echo image_create("info");
echo image_create("media");



it produces only home image.
where is the fault?

firepages
06-07-2004, 01:05 PM
there is no fault , but you can only display 1 image at a time in this manner (setting the content type header) , save your images to file individually , then display them via <img src="" etc

romalong
06-07-2004, 01:18 PM
there is no fault , but you can only display 1 image at a time in this manner (setting the content type header) , save your images to file individually , then display them via <img src="" etc


it's too simple to use common HTML.
i'm learning graphic support in PHP and hope it's possible to create such function, at least i'll try...

romalong
06-07-2004, 05:02 PM
are there any other way to create images via PHP?
i tried to screw it up, but nothing happen - only one image dysplays at a time...

firepages
06-07-2004, 05:42 PM
if you add a filename PHP creates the image file for you..(so in your code this will create all 3 images for you (drop the header() call)

imagepng( $im , $string.'.png' ) ;


but you still can only display 1 image at a time in the browser unless you

echo '<img src="'.$string.'.png" />';

you could create a composite image of the 3 images if you wanted (e.g. 1 image)
another popular library often used with PHP for images is imagemagick

romalong
06-08-2004, 12:07 AM
cheers firepages!

it works:




$menu = array(home,downloads,chat,media,forum,contact);

foreach ($menu as $value){

$im = imagecreatefrompng("button.png");
$colour = imagecolorallocate($im, 100, 100, 100);
$px = (imagesx($im) - 6.5 * strlen($value)) / 2;
imagestring($im, 3, $px, 4, $value, $colour);
imagepng($im,$value.'.png');
imagedestroy($im);
echo '<a href="'.$value.'.php"><img src="'.$value.'.png" border="0" alt="'.$value.'" /></a>'."\n";

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum