Hi there,
I get a fatal error when I try and run the following script. What I am trying to do is take a pre-existing image on the server (the path to which is stored in a db, and which I output to a variable), regenerate it with GD, and then output it to the browser window. I wrote a function to do this, but it just fails at the point where I call the function. Any ideas what I am doing wrong?
Thanks in advance.
<?php
function generateImage( $source ) {
$source_path = dirname( $source ) ; # What is the path to the image
$source_file = basename( $source ) ; # What is the image filename
chdir( $source_path ) ; # change directory to where image is stored due to note on <!-- w --><a class="postlink" href="http://www.php.net">www.php.net</a><!-- w -->
$im = @imagecreatefromjpeg( $source_file ) ;
if ( !$im ) { /* See if it failed */
$im_ = imagecreate(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc_ = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc); }
return $im;
imagedestroy( $im ) ;
}
?>
// Load of 'connect to db and return the value I want' code
<fieldset id="imagewindow">
<legend>Latest Image</legend>
<?php
$filename = "/path/to/image/on/server.jpg" ;
generateImage( $filename ) ;
echo "<img src=http://www.htmlforums.com/archive/index.php/\"$im\" />\n" ;
?>
</fieldset>as i explained in this (<!-- m --><a class="postlink" href="http://htmlforums.com/showthread.php?s=&threadid=42699">http://htmlforums.com/showthread.php?s=&threadid=42699</a><!-- m -->) thread, what you are trying to do is impossible. you are trying to get gd to get the image data, and then echo that data as the html src. you cannot do that! you have to have a seperate file, or a seperate section on a page or something that sends an image header, and then outputs only the image data. you cant do echo "<img src='http://www.htmlforums.com/archive/index.php/". imagepng($whatever)."' />"; no matter how much you want to
the only thing that can be in the src of an html img element is a url to an image.
also, you cant do a return statement, and then call another function. after a return statement is set, anything after that is not executed. furthermore, when you return something, you have to assign it to a variable, you cant use a variable used inside of a function outside of the function, unless its declared to be a global variable.n8thegreat is 100% correct. You cannot do this the way you are trying to asyou are trying to embed the image's binary as the content of the src=http://www.htmlforums.com/archive/index.php/"" attribute.
However, you are not 100 miles from what it is you want to achieve. Simply put the image function you have created in a file (e.g. genImage.php) and invoke it through the img src tag.
Example genImage.php file:
<?php
header(content-type: image/jpg);
function generateImage($GET['source'])
{
...
return ($im);
}
echo $im;
and call it from within your html like this:
<img src=http://www.htmlforums.com/archive/index.php/"genImage.php?source=whatever" />
You might want to check out the header() bit as it's a bloody long time since I used a content-type header and can't quite be sure the syntax is correct. Anyway I think that should do it *scratches head and realises how quickly unused information leaves the brain*Thanks to both of you.
I have got the script (and function) working now. I essentially did a combination of what you both suggested.
I should have known about what n8thegreat said - I have used GD and PHP many times in the past with success - I just wish GD threw some errors instead of just collapsing without so much as a wheeze to the error_log.
I get a fatal error when I try and run the following script. What I am trying to do is take a pre-existing image on the server (the path to which is stored in a db, and which I output to a variable), regenerate it with GD, and then output it to the browser window. I wrote a function to do this, but it just fails at the point where I call the function. Any ideas what I am doing wrong?
Thanks in advance.
<?php
function generateImage( $source ) {
$source_path = dirname( $source ) ; # What is the path to the image
$source_file = basename( $source ) ; # What is the image filename
chdir( $source_path ) ; # change directory to where image is stored due to note on <!-- w --><a class="postlink" href="http://www.php.net">www.php.net</a><!-- w -->
$im = @imagecreatefromjpeg( $source_file ) ;
if ( !$im ) { /* See if it failed */
$im_ = imagecreate(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc_ = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc); }
return $im;
imagedestroy( $im ) ;
}
?>
// Load of 'connect to db and return the value I want' code
<fieldset id="imagewindow">
<legend>Latest Image</legend>
<?php
$filename = "/path/to/image/on/server.jpg" ;
generateImage( $filename ) ;
echo "<img src=http://www.htmlforums.com/archive/index.php/\"$im\" />\n" ;
?>
</fieldset>as i explained in this (<!-- m --><a class="postlink" href="http://htmlforums.com/showthread.php?s=&threadid=42699">http://htmlforums.com/showthread.php?s=&threadid=42699</a><!-- m -->) thread, what you are trying to do is impossible. you are trying to get gd to get the image data, and then echo that data as the html src. you cannot do that! you have to have a seperate file, or a seperate section on a page or something that sends an image header, and then outputs only the image data. you cant do echo "<img src='http://www.htmlforums.com/archive/index.php/". imagepng($whatever)."' />"; no matter how much you want to
the only thing that can be in the src of an html img element is a url to an image.
also, you cant do a return statement, and then call another function. after a return statement is set, anything after that is not executed. furthermore, when you return something, you have to assign it to a variable, you cant use a variable used inside of a function outside of the function, unless its declared to be a global variable.n8thegreat is 100% correct. You cannot do this the way you are trying to asyou are trying to embed the image's binary as the content of the src=http://www.htmlforums.com/archive/index.php/"" attribute.
However, you are not 100 miles from what it is you want to achieve. Simply put the image function you have created in a file (e.g. genImage.php) and invoke it through the img src tag.
Example genImage.php file:
<?php
header(content-type: image/jpg);
function generateImage($GET['source'])
{
...
return ($im);
}
echo $im;
and call it from within your html like this:
<img src=http://www.htmlforums.com/archive/index.php/"genImage.php?source=whatever" />
You might want to check out the header() bit as it's a bloody long time since I used a content-type header and can't quite be sure the syntax is correct. Anyway I think that should do it *scratches head and realises how quickly unused information leaves the brain*Thanks to both of you.
I have got the script (and function) working now. I essentially did a combination of what you both suggested.
I should have known about what n8thegreat said - I have used GD and PHP many times in the past with success - I just wish GD threw some errors instead of just collapsing without so much as a wheeze to the error_log.