output of imagejpeg() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

output of imagejpeg() function

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need to use header() to specify the content type so the browser knows what you are giving it.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Specifically..

Code: Select all

header("Content-type: image/jpeg");
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

correct, that is not possible as the browser has no way of separating them or displaying them correctly.
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post 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); 
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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();
	}
}
?>
Kestas
Forum Newbie
Posts: 14
Joined: Mon Jul 31, 2006 12:10 am
Location: Lithuania

Post by Kestas »

It works beautifully! Thanks everyone a lot!
Post Reply