
January 26th, 2006, 07:25 PM
| | | passing a filename & content type as image
I'm trying to pass a filename (which is a jpeg image) to a php
function / file so that it will display. I know that its simple
to get PHP to display an image hardcoding in the filename. For
example, an href to this:
<?php
header("content-type:image/jpeg");
$filename = "image_file.jpg";
$im=ImageCreateFromJPEG($filename);
ImageJPEG($im);
?>
But when I have a filename I create dynamically in PHP and try to
call a php function with the filename, for example calling:
<?php
display_jpeg_file($image_path);
?>
<?php
function display_jpeg_file($filename) {
header("content-type:image/jpeg");
$im=ImageCreateFromJPEG($filename);
ImageJPEG($im);
}
?>
I get the "cannot modify header information" error. The error
message always refers to a php function early in the phmtl page.
So somehow it sees this function as a continuation of the existing
html document. How do I arrange the function call to my display
function so that it works? Or, if my approach is all wrong, what
is the correct approach? The important part is I have a filename
generated in PHP that I want to display.
Thanks in advance for any help.
B Squared
================================================== =============
Self esteem, n. An erroneous appraisement
-- Ambrose Bierce, The Devils Dictionary | 
January 26th, 2006, 07:45 PM
| | | Re: passing a filename & content type as image
B Squared wrote:[color=blue]
> I'm trying to pass a filename (which is a jpeg image) to a php
> function / file so that it will display.[/color]
I think you can simply use readfile() to read the file and pass it
through to the browser. | 
January 26th, 2006, 07:45 PM
| | | Re: passing a filename & content type as image
"B Squared" <null@null.com> wrote in message
news:BbadneSetuTRgkTenZ2dnUVZ_s-dnZ2d@scnresearch.com...[color=blue]
>
> I'm trying to pass a filename (which is a jpeg image) to a php
> function / file so that it will display. I know that its simple
> to get PHP to display an image hardcoding in the filename. For
> example, an href to this:
>
> <?php
> header("content-type:image/jpeg");
> $filename = "image_file.jpg";
> $im=ImageCreateFromJPEG($filename);
> ImageJPEG($im);
> ?>[/color]
Why don't you just use readfile() instead of imagecreatefromjpeg? You only
really need to use the image functions if you're manipulating images, not if
you're just displaying them.
[color=blue]
> But when I have a filename I create dynamically in PHP and try to
> call a php function with the filename, for example calling:
>
> <?php
> display_jpeg_file($image_path);
> ?>
>
> <?php
> function display_jpeg_file($filename) {
> header("content-type:image/jpeg");
> $im=ImageCreateFromJPEG($filename);
> ImageJPEG($im);
> }
> ?>
>
> I get the "cannot modify header information" error. The error
> message always refers to a php function early in the phmtl page.
> So somehow it sees this function as a continuation of the existing
> html document. How do I arrange the function call to my display
> function so that it works? Or, if my approach is all wrong, what
> is the correct approach? The important part is I have a filename
> generated in PHP that I want to display.[/color]
You can't send headers if you've already outputted (unless you use output
buffering). Why are you outputting something before the image? The image
won't work if you have any output before or after it in the same document.
If you want to get a PHP page to show an image, include a standard HTML
<img> tag with the src pointing to your PHP script. If you stil need to
pass it another filename, you can put that in the query string
(base64_encode it so it's url-safe, and base64_decode it the other end to
get the filename out).
does that make sense?
[color=blue]
> Thanks in advance for any help.
>
> B Squared[/color]
dave
[color=blue]
> ================================================== =============
> Self esteem, n. An erroneous appraisement
> -- Ambrose Bierce, The Devils Dictionary
>
>
>
>[/color] | 
January 27th, 2006, 09:15 PM
| | | Re: passing a filename & content type as image
d wrote:[color=blue]
> "B Squared" <null@null.com> wrote in message
> news:BbadneSetuTRgkTenZ2dnUVZ_s-dnZ2d@scnresearch.com...
>[color=green]
>>I'm trying to pass a filename (which is a jpeg image) to a php
>>function / file so that it will display. I know that its simple
>>to get PHP to display an image hardcoding in the filename. For
>>example, an href to this:
>>
>><?php
>> header("content-type:image/jpeg");
>> $filename = "image_file.jpg";
>> $im=ImageCreateFromJPEG($filename);
>> ImageJPEG($im);
>>?>[/color]
>
>
> Why don't you just use readfile() instead of imagecreatefromjpeg? You only
> really need to use the image functions if you're manipulating images, not if
> you're just displaying them.[/color]
Excellent suggestion. I tried this, and it does just what I need.
[color=blue]
>[color=green]
>>But when I have a filename I create dynamically in PHP and try to
>>call a php function with the filename, for example calling:
>>
>><?php
>> display_jpeg_file($image_path);
>>?>
>>
>><?php
>> function display_jpeg_file($filename) {
>> header("content-type:image/jpeg");
>> $im=ImageCreateFromJPEG($filename);
>> ImageJPEG($im);
>> }
>>?>
>>
>>I get the "cannot modify header information" error. The error
>>message always refers to a php function early in the phmtl page.
>>So somehow it sees this function as a continuation of the existing
>>html document. How do I arrange the function call to my display
>>function so that it works? Or, if my approach is all wrong, what
>>is the correct approach? The important part is I have a filename
>>generated in PHP that I want to display.[/color]
>
>
> You can't send headers if you've already outputted (unless you use output
> buffering).Why are you outputting something before the image?[/color]
I want to place some text above the image, and control the font and
background color. Typical HTML to pretty up the appearance of the page.
[color=blue]
> The image
> won't work if you have any output before or after it in the same document.
> If you want to get a PHP page to show an image, include a standard HTML
> <img> tag with the src pointing to your PHP script. If you stil need to
> pass it another filename, you can put that in the query string
> (base64_encode it so it's url-safe, and base64_decode it the other end to
> get the filename out).[/color]
I understand in part. Here's the deal. The filepathname for the image
to be displayed comes from a database query that's inside a PHP script.
So it really is dynamically generated. So, referring to your method of
encoding the filename, it looks like (if I understand you) that I need
to pass the data I retrieve inside PHP back out to html, so that html
can format the strings, and, using the <img> tag, call another PHP
file to display the image. It seems a bit complicated, but I can do
that. How do I pass the strings from PHP to html. A simple example
of how to do this would be *greatly* appreciated. If I understand
you, this is the only part of the solution I don't know how to do.
Thanks in advance.
B Squared | 
January 28th, 2006, 02:25 AM
| | | Re: passing a filename & content type as image
Never mind my last comment,
I got it figured out.
B Squared.
[color=blue]
> "B Squared" <null@null.com> wrote in message
> news:BbadneSetuTRgkTenZ2dnUVZ_s-dnZ2d@scnresearch.com...
>[color=green]
>>I'm trying to pass a filename (which is a jpeg image) to a php
>>function / file so that it will display. I know that its simple
>>to get PHP to display an image hardcoding in the filename. For
>>example, an href to this:
>>
>><?php
>> header("content-type:image/jpeg");
>> $filename = "image_file.jpg";
>> $im=ImageCreateFromJPEG($filename);
>> ImageJPEG($im);
>>?>[/color]
>
>
> Why don't you just use readfile() instead of imagecreatefromjpeg? You only
> really need to use the image functions if you're manipulating images, not if
> you're just displaying them.
>
>[color=green]
>>But when I have a filename I create dynamically in PHP and try to
>>call a php function with the filename, for example calling:
>>
>><?php
>> display_jpeg_file($image_path);
>>?>
>>
>><?php
>> function display_jpeg_file($filename) {
>> header("content-type:image/jpeg");
>> $im=ImageCreateFromJPEG($filename);
>> ImageJPEG($im);
>> }
>>?>
>>
>>I get the "cannot modify header information" error. The error
>>message always refers to a php function early in the phmtl page.
>>So somehow it sees this function as a continuation of the existing
>>html document. How do I arrange the function call to my display
>>function so that it works? Or, if my approach is all wrong, what
>>is the correct approach? The important part is I have a filename
>>generated in PHP that I want to display.[/color]
>
>
> You can't send headers if you've already outputted (unless you use output
> buffering). Why are you outputting something before the image? The image
> won't work if you have any output before or after it in the same document.
> If you want to get a PHP page to show an image, include a standard HTML
> <img> tag with the src pointing to your PHP script. If you stil need to
> pass it another filename, you can put that in the query string
> (base64_encode it so it's url-safe, and base64_decode it the other end to
> get the filename out).
>
> does that make sense?
>
>[color=green]
>>Thanks in advance for any help.
>>
>>B Squared[/color]
>
>
> dave
>
>[color=green]
>>================================================ ===============
>> Self esteem, n. An erroneous appraisement
>> -- Ambrose Bierce, The Devils Dictionary
>>
>>
>>
>>[/color]
>
>
>[/color] |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|