Jump to content

sending image through header problem


alexb27

Recommended Posts

Hi all,

I am trying to send an image through header so I have two php files:

test0.php

<html>
<head>
</head>
<body>
<?php $imagename='test.php'; ?>
<img src="<?php echo $imagename; ?>"/>

</body>
</html>

 

and test.php:

<html>
<html>
<head>
</hesd>
<body> 
<?php
    $image = imagecreatefromjpeg('images/1.jpg');
    header('Content-Type: image/jpeg');
    imagejpeg($image, '', 100);	
?>
</body>
</html>

 

but the image doesn't show .

What is the correct way?

Thank you in advance.

 

 

Link to comment
Share on other sites

I have tried

 

test0.php

 

<html>
<head>
</head>
<body>

<?php include 'test.php'; ?>
<img src="<?php echo $image; ?>"/>

</body>
</html>

 

and test.php

 

<html>
<head>
</hesd>
<body> 
<?php

    $image = imagecreatefromjpeg('images/1.jpg');

?>
</body>
</html>

 

and also

<html>
<head>
</hesd>
<body> 
<?php
    $dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
    $image = imagecreatefromjpeg($dir . '/1.jpg');

?>
</body>
</html>

 

but it's not working

Link to comment
Share on other sites

The test.php file (that is outputting the image) is not a HTML document. The only thing that should be in it is php code that outputs the content-type header followed by the image data.

 

Also, don't use GD function JUST to output image data. You use GD functions if you need to manipulate the image in some way. For what you are doing (just outputting the image data) you would just use readfile after the content type header has been output.

Link to comment
Share on other sites

for test.php i have used

<?php
    $dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
    $image = imagecreatefromjpeg($dir . '/1.jpg');

?>

 

and still doesn't work

 

The only thing that should be in it is php code that outputs the content-type header followed by the image data.

 

I do not understand. Do you mean

<?php
    $dir='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
    $image = imagecreatefromjpeg($dir . '/1.jpg');
    header('Content-Type: image/jpeg');
    imagejpeg($image, '', 100);
?>

 

If so, how will I write test0.php?

 

I do use GD functions in my original code, this is gust a reduced version of it.

 

 

 

 

Link to comment
Share on other sites

I do have functions that modifies the image.

This is a stripped down version of my  code. In fact test.php is a reduced version of something like

 

 <?php
//change this path to match your images directory
$dir ='C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/images';
// make sure the requested image is valid
if (isset($_GET['id']) && ctype_digit($_GET['id']) &&  file_exists($dir . '/'.
    $_GET['id'] . '.jpg')) {
    $image = imagecreatefromjpeg($dir . '/' . $_GET['id'] . '.jpg');
} else {
    die('invalid image specified');
}
// apply the filter
$effect = (isset($_GET['e'])) ? $_GET['e'] : -1;
switch ($effect) {
case IMG_FILTER_NEGATE:
    imagefilter($image, IMG_FILTER_NEGATE); 
    break;
case IMG_FILTER_GRAYSCALE:
    imagefilter($image, IMG_FILTER_GRAYSCALE); 
    break;
case IMG_FILTER_EMBOSS:
    imagefilter($image, IMG_FILTER_EMBOSS); 
    break;
case IMG_FILTER_GAUSSIAN_BLUR:
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
    break;
}
// show the image
header('Content-Type: image/jpeg');
imagejpeg($image, '', 100);
?> 

 

I am trying to answer this problem by severals days, and i'm ready to give up so any help is really appreciated.

Thank you for your answers so far.

 

Link to comment
Share on other sites

You are likely getting php errors (the html you originally posted in that file would have been producing header() errors.)

 

You should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors your code produces will be reported and displayed. You should set these two values in your master php.ini so that fatal parse errors will be displayed, but if you are sure your code is actually running, you can set them in your script.

 

Temporarily comment out the content-type header() statement, add the following two lines of code immediately after your first opening <?php tag, and browse directly to the test.php file that produces the image -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

Thank you for your useful replies, PFMaBiSmAd.

 

I looked at php.ini and observed that error_reporting = E_ALL | E_STRICT and  display_errors = On so I left them as that; I commented out the content header in test.php and navigated to it in my browser. The image displayed . I de-commented out and the image still displayed . I added anyhow the two lines of code after <?php in test.php and it showed no error(with content header commendted out and not commented out ). I added the two lines also in test0.php , after each <?php and navigated to test0.php and no error was displayed, and no image also.

 

I'm kind of stuck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.