Page 1 of 1

output of imagejpeg() function

Posted: Mon Jul 31, 2006 12:22 am
by Kestas
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi!
Need help with imagejpeg() function. It should be outputting image to a file or a browser. Outputting to a file works fine, but when trying to output an image to the browser image is shown as a text(bunch of symbols with header in front). Is there anything that I don`t understand about it? Thanks in advance! Will include my code:

Code: Select all

<? session_start(); 
$maindir="C:\web\docs\\\thumbs";
$mydir = opendir($maindir); 
$exclude = array("index.php", "convert.php", "..", ".", "thumbs", "nuotr", "Thumbs.db") ; 
while($fn = readdir($mydir)) { 
  if ($fn == $exclude[0] || $fn == $exclude[1]|| $fn == $exclude[2]||
    $fn == $exclude[3]|| $fn == $exclude[4]|| $fn == $exclude[5]|| $fn == $exclude[6]) continue; 
  $myimage="$fn";
  echo"$fn<br>";
  $photo = ImageCreateFromJPEG("C:\web\docs\\thumbs\\$fn");
  ImageJPEG($photo); 
} 
closedir($mydir); 
?>

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 31, 2006 1:59 am
by feyd
you need to use header() to specify the content type so the browser knows what you are giving it.

Posted: Mon Jul 31, 2006 3:30 am
by onion2k
Specifically..

Code: Select all

header("Content-type: image/jpeg");

Posted: Mon Jul 31, 2006 6:32 am
by Kestas
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Modified my code, but now it outputs nothing.
code:

Code: Select all

<? session_start();
header("Content-type: image/jpeg");
$maindir="C:\web\docs\\thumbs";
$mydir = opendir($maindir); 
$exclude = array("index.php", "convert.php", "..", ".", "thumbs", "nuotr", "Thumbs.db") ; 
while($fn = readdir($mydir)) { 
  if ($fn == $exclude[0] || $fn == $exclude[1]|| $fn == $exclude[2]||
    $fn == $exclude[3]|| $fn == $exclude[4]|| $fn == $exclude[5]|| $fn == $exclude[6]) continue; 
  $myimage="$fn";
  echo"$fn<br>";
  $photo = ImageCreateFromJPEG("C:\web\docs\\thumbs\\$fn");
  //header("Content-type: image/jpeg");
  ImageJPEG($photo); 
} 
closedir($mydir); 
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 31, 2006 7:23 am
by volka
ImageCreateFromJPEG - GD reads a jpeg image to memory (in its own format, more or less jpeg->gd[memory])
ImageJPEG - gd[memory] -> jpeg[output]
Why? A simple readfile() would suffice.

You cannot send 2,3,x images at once as one response. The browser/client asks for one image und you flood it with ...dozens?
Or is there a special client involved that can seperate those images from the stream?


What's the purpose of this script?

Posted: Mon Jul 31, 2006 11:10 am
by Kestas
Thank you for your replies. No, there`s nothing to separate images from the stream. Did I get you right: with a single .php file I will not be able to send all files in a specified folder to a browser?

Posted: Mon Jul 31, 2006 11:15 am
by feyd
correct, that is not possible as the browser has no way of separating them or displaying them correctly.

Posted: Mon Jul 31, 2006 11:47 am
by Kestas
I have followed all your suggestions and came up with the code that still doesn`t work and does the same as previous ones - outputs symbols instead of image(only one image this time). My code:

Code: Select all

<? session_start();
$maindir="E:\web\docs\\thumbs";
$mydir = opendir($maindir); 
$exclude = array("index.php", "convert.php", "..", ".", "thumbs", "nuotr", "Thumbs.db") ; 
while(($fn = readdir($mydir))) { 
  if ($fn == $exclude[0] || $fn == $exclude[1]|| $fn == $exclude[2]||
    $fn == $exclude[3]|| $fn == $exclude[4]|| $fn == $exclude[5]|| $fn == $exclude[6]) continue; 
  $myimage="$fn";
  echo"$fn<br>";
  readfile("E:\web\docs\\thumbs\\$fn");
  break(0);
} 
closedir($mydir); 
?>

Posted: Mon Jul 31, 2006 11:54 am
by volka
feyd wrote:you need to use header() to specify the content type so the browser knows what you are giving it.
onion2k wrote:Specifically..

Code: Select all

header("Content-type: image/jpeg");
hasn't been added.

Try this one.

Code: Select all

<?php
ini_set('display_errors', true); error_reporting(E_ALL);
session_start();
$maindir='E:/web/docs/thumbs/';
$exclude = array("index.php", "convert.php", "..", ".", "thumbs", "nuotr", "Thumbs.db") ;

$mydir = opendir($maindir);
while( $fn=readdir($mydir) ) {
	if ( !in_array($fn, $exclude) ) {
		if (!headers_sent()) {
			header('Content-Type: image/jpeg');
			readfile($maindir.$fn);
		}
		die();
	}
}
?>

Posted: Mon Jul 31, 2006 1:14 pm
by Kestas
It works beautifully! Thanks everyone a lot!