Embedding GD Images in a php page
Posted: Fri Jul 11, 2008 2:46 am
I'm wondering if someone can point me in the right direction with the GD PHP Image Library. I've only just started using it and finding some of the methods a little restrictive to say the least... and very temperamental!
I've dynamically created a .png image which is based on user input and information obtained from a url. I then store the image in a location on the site so it can then be referred to using BB Code in a phpBB forum in a user's signature.
My question is that I want to allow the user to preview the signature that has been created prior to referring to it using a url. In order to do this in a functional way I need to be able to embed the image inside a webpage.
I can get the .png to display on it's own on a webpage using the imagepng() function. The problem is if I attempt to do an echo '<img src="http://someurl.com/folder/subfolder/blah.png" />'; I just get a message stating that the image contains errors. Clearly it doesn't because I can view it by accessing it directly through a browser.
Here's a snapshot of my GD code:-
The key part of it is referred to at the bottom. As you can see i've commented out the echo <img /> line because it didn't work!
I'm wondering if it's perhaps because i've declared the @header('Content-type: image/png'); which is why it's telling me my image is corrupt when I'm trying to print out any other html code. Is there any way around this??
I've dynamically created a .png image which is based on user input and information obtained from a url. I then store the image in a location on the site so it can then be referred to using BB Code in a phpBB forum in a user's signature.
My question is that I want to allow the user to preview the signature that has been created prior to referring to it using a url. In order to do this in a functional way I need to be able to embed the image inside a webpage.
I can get the .png to display on it's own on a webpage using the imagepng() function. The problem is if I attempt to do an echo '<img src="http://someurl.com/folder/subfolder/blah.png" />'; I just get a message stating that the image contains errors. Clearly it doesn't because I can view it by accessing it directly through a browser.
Here's a snapshot of my GD code:-
Code: Select all
<?php
$gdfonts = array(
'8x13iso', '9x15iso', 'andale12', 'atommicclock', 'bmreceipt',
'chowfun', 'courier8', 'dimurph', 'georgia8', 'proggyclean',
'proggysquare', 'systemex', 'terminal6', 'trisk'
);
//
//
//
if( !isset($image_info) || !isset($image_text) )
{
exit;
}
//
//
//
$sz = @getimagesize(MAINDIR.'sigs/'.$custom_sig->get_sig_theme().'/'.$image_info['image']);
if( !$sz )
{
exit;
}
$image_w = $sz[0];
$image_h = $sz[1];
//
//
//
@header('Content-type: image/png');
$im = @imagecreatefromgif(MAINDIR.'sigs/'.$custom_sig->get_sig_theme().'/'.$image_info['image']);
$rgb = ( isset($image_info['color']) ? $image_info['color'] : array(255, 255, 255) );
$bgColor = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
for( $i = 0; $i < count($image_text); $i++ )
{
if( !is_numeric($image_text[$i]['font']) )
{
$font = 3;
}
else if( $image_text[$i]['font'] < 0 )
{
$font = $image_text[$i]['font'] * -1;
}
else
{
if( !($font = @imageloadfont('./gd_fonts/'.$gdfonts[$image_text[$i]['font']].'.gdf')) )
{
$font = 10;
}
}
$rgb = $image_text[$i]['color'];
$fgColor = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
imagestring($im, $font, $image_text[$i]['x'], $image_text[$i]['y'], $image_text[$i]['text'], $fgColor);
}
// Calculate the path and filename
//echo "maindir " . MAINDIR;
$path = MAINDIR."images/signatures/";
$image_name = $pfuser. ".png";
$fpath = $path.$image_name;
// Get the contents of the png
ob_start();
imagepng($im);
$sImage = ob_get_contents();
ob_end_clean();
// Store on the disk
if(($handle = fopen($fpath,'w+b')) === FALSE){
die('Failed to open file for writing!');
} else {
chmod($fpath, 0777);
fwrite($handle, $sImage);
fclose($handle);
//echo '<img src="'.MAINDIR.'images/signatures/'.$pfuser.'.png" />';
imagepng($im);
imagedestroy($im);
}
?>
I'm wondering if it's perhaps because i've declared the @header('Content-type: image/png'); which is why it's telling me my image is corrupt when I'm trying to print out any other html code. Is there any way around this??