alexb27 Posted November 2, 2010 Share Posted November 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/ Share on other sites More sharing options...
doddsey_65 Posted November 2, 2010 Share Posted November 2, 2010 why not is test0.php use: <html> <head> </head> <body> <?php include ('test.php'); ?> <img src="$image; ?>"/> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129369 Share on other sites More sharing options...
alexb27 Posted November 2, 2010 Author Share Posted November 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129390 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2010 Share Posted November 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129409 Share on other sites More sharing options...
alexb27 Posted November 2, 2010 Author Share Posted November 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129444 Share on other sites More sharing options...
litebearer Posted November 2, 2010 Share Posted November 2, 2010 he means, if is ALREADY an image AND you are not changing it, there is NO reason to use GD, simply display the original image Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129481 Share on other sites More sharing options...
alexb27 Posted November 2, 2010 Author Share Posted November 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129550 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2010 Share Posted November 2, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129558 Share on other sites More sharing options...
alexb27 Posted November 3, 2010 Author Share Posted November 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217551-sending-image-through-header-problem/#findComment-1129835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.