Page 1 of 1

Outputted Junk From GD - Full Code Provided

Posted: Fri Jul 09, 2004 8:32 am
by malcolmboston
firstly id just like to say sorry for the width of the page :lol:

lately ive been building another CMS, and have been touching on various gallery functionality that would be required, i have came up with a problem whilst trying to dynamicalluy generate some thumbnails, what constantly happens is it just outputs alot of junk, you will see this later in the post.

please note i have tried this will both GD1 AND GD2 neither of which work.
(gd1 uses imagecreate() whereas gd2 uses imagecreatetruecolor();)

ok, first heres the function that generates that layout and file information (this is fine, i am only showing it so you know how the source of the layout is generated)

Code: Select all

// display code that the function is called within
// not full code but this is all that should be needed for spotting any errors
function show_files_upload_area ($picture_upload_table, $default_uploaded_directory)
{
	$query = "SELECT * FROM $picture_upload_table WHERE allowed = 'n'";
	$result = mysql_query($query) or die (mysql_error());
	$num_rows = mysql_num_rows($result);
	print "<strong>$num_rows</strong> picture(s) currently requiring acceptance<br><br>";
	//
	if ($num_rows == 1)
	{
		$array_1 = mysql_fetch_array($result);
		// only one thing to display
		print "
		<form action="picture_admin_uploader_acceptance.php" method="post" enctype="application/x-www-form-urlencoded" name="$array_1[id] target="_self">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
		<td class = "picture-details-table-picture-holder" width="17%" rowspan="6">";
		// simplify the variable for later passing for functions
		$filename = $array_1['url'];
		//print "
		//<a href="$default_uploaded_directory/$filename" target="_BLANK" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('$filename','','img/avatar_anim.gif',1)">";
		thumbnail_generator ($filename, $picture_upload_table, $default_uploaded_directory);
		print "</td>";
		}
now the problematic code, i have absolutely no idea what is wrong with it, ive added alot of error handling and it still shows no errors or nothing

Code: Select all

function thumbnail_generator ($filename, $picture_upload_table, $default_uploaded_directory)
{
	// we should first open the directory
	if (is_dir($default_uploaded_directory))
	{
		// we are entering a valid directory
		// open the directory (DO NOT FORGET TO CLOSE IT)
		$directory_handle = chdir($default_uploaded_directory);
		if (file_exists($filename))
		{
			// the file exists
			// we are now inside the directory
			//define height width of created thumbnail
			define('MAX_WIDTH', 100);
			define('MAX_HEIGHT', 100);
			// lets get extension
			$extension = explode(".", $filename);
			$ext_count = count($extension);
			// we'll need to -1 on this to get true count
			$ext_count = ($ext_count - 1);
			$file_extension = $extension[$ext_count];
			// lower case it
			$file_extension = strtolower($file_extension);
			// we now have the file extension
			// this is can be obtained using $file_extension
			// now we should create an if statement based on what sort
			// of file we're working with and what method to use
			// LATER I WILL BE ADDING SUPPORT FOR OTHER FORMATS
			if ($file_extension == "jpg" || $file_extension == "jpeg")
			{
				// the extension is saying its a jpeg
				// lets set a header
				// we'll add some error handling here concerning
				// the headers already sent error message
				if (headers_sent())
				{
					// headers have already been sent
					// ill use output buffering to stop these errors
					// as they keep bugging me
					// (ob_start(); is called on the actual page and not within the function)
					ob_end_flush();
					header("Content-type: image/jpeg");
				}
				else
				{
					// headers have'nt been sent yet
					header("Content-type: image/jpeg");
				}
				// attempt to open the real full-size file
				$filename = imagecreatefromjpeg($filename);
				// get the height and width of the original image
				$original_height = imagesy($filename);
				$original_width = imagesx($filename);
				// now we need to get the scale and decide what to do from there
				$ratio = min(MAX_WIDTH / $original_width, MAX_HEIGHT / $original_height);
				if ($ratio < 1)
				{
					// the original image needs to be resized
					$new_width = floor($scale * $original_width);
					$new_height = floor($scale * $original_height);
					$temp = imagecreate($new_width, $new_height) or die ("Cannot Create Thumbnail: Possible Problem With Function Coding");
					imagecopyresized($temp, $filename, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
					imagedestroy($filename);
					$thumbnail = $temp;
					if (headers_sent())
					{
						print "<strong><p>Can't, Headers already sent</p></strong>";
					}
					else
					{
						// headers STILL havent been sent
						imagejpeg($thumbnail);
					}
				}
				else
				{
					// the image does'nt need to be resized so we'll just output it
					// to the browser straight away
					if (headers_sent())
					{
						print "<strong><p>Can't, Headers already sent</p></strong>";
						// ive purposefully not outputted the image, just
						// so i can see if its getting to this point in the script
						// (outputting as of now, still doesnt work)
					}
					else
					{
						// headers STILL havent been sent
						$thumbnail = $filename;
						imagejpeg($thumbnail);
					}
				}
			}
			else
			{
				print "Not a valid extension";
			}
		}
		else
		{
			print "file not found in this directory: ";
			$current_directory = getcwd();
			print "<strong>$current_directory</strong>";
			exit;
		}
	}
	else
	{
		print "Not a valid working directory";
	}
	// change directory back
	chdir("../");
	$current_directory = getcwd();
}
heres an image of what it outputs
Image

heres what it should look like (without thumbnail generation, using hardcoding)
Image

also this might prove useful so ill show it, heres the source code after its all generated

Code: Select all

// Outputted HTML (view source)
&lt;td class = "picture-details-table-picture-holder" width="17%" rowspan="6"&gt;ÿØÿà JFIF      ÿþ &gt;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality
ÿÛ C 		

 $.' ",#(7),01444'9=82&lt;.342ÿÛ C			

2!!22222222222222222222222222222222222222222222222222ÿÀ  
 d" ÿÄ           	
ÿÄ µ   &#125; !1AQa"q2

Posted: Fri Jul 09, 2004 8:44 am
by redmonkey
Quickly glancing at your code you seem to send the image/type header early on, then later you check if headers are sent and if they are you are outputting a text error message, unless I'm missing something?

How can you send a text error message when you have instructed the client that it's going to recieve an image?

Posted: Fri Jul 09, 2004 8:46 am
by malcolmboston
that is on a seperate condition:

theres 2; needs resizing and does'nt need resizing

that is located in doesnt, every image ive tried requires resizing so should never get there, also i only added that whilst i was debugging it to see which bit of the code each image was getting upto

Posted: Fri Jul 09, 2004 8:49 am
by malcolmboston
the reason i added 2 is because that was originally that problem i had with the code (headers already been sent) so i went slightly overboard :lol:

either way, it still doesnt work

Posted: Fri Jul 09, 2004 8:55 am
by redmonkey

Code: Select all

if ($file_extension == "jpg" || $file_extension == "jpeg")
         {
            // the extension is saying its a jpeg
            // lets set a header
            // we'll add some error handling here concerning
            // the headers already sent error message
            if (headers_sent())
            {
               // headers have already been sent
               // ill use output buffering to stop these errors
               // as they keep bugging me
               // (ob_start(); is called on the actual page and not within the function)
               ob_end_flush();
               header("Content-type: image/jpeg");
            }
            else
            {
               // headers have'nt been sent yet
               header("Content-type: image/jpeg");
            }........................
So your header will always be sent if the file extension is jpg or jpeg. But later you have...

Code: Select all

if (headers_sent())
               {
                  print "<strong><p>Can't, Headers already sent</p></strong>";
               }
               else
               {
                  // headers STILL havent been sent
                  imagejpeg($thumbnail);
               }
You will have already sent the headers so you are only going to get the text error message output.

Posted: Fri Jul 09, 2004 9:00 am
by malcolmboston
"<strong><p>Can't, Headers already sent</p></strong>";

this never gets printed, so it cannot be going through this condition

Posted: Fri Jul 09, 2004 9:02 am
by malcolmboston
ah, sorry i now see what you mean,

so why is it still trying to output the image? as there is no code on there that should be outputting it? just a print statement that doesnt get printed

Posted: Fri Jul 09, 2004 9:08 am
by redmonkey
You cannot mix headers!

When you send output to the buffer it should be of one type!

It looks like you are trying to send both the HTML and the image(s) in one stream as one request and that is not how it works!

Posted: Fri Jul 09, 2004 9:10 am
by malcolmboston
ok, just want to fully understand what your saying here.

now you talk about mixing output type

does this also mean, i cannot use the function in the way i am trying to (within another function that outputs HTML)???

sorry, i just want to fully understand this.

Posted: Fri Jul 09, 2004 9:19 am
by redmonkey
malcolmboston wrote:does this also mean, i cannot use the function in the way i am trying to (within another function that outputs HTML)???
That's correct. When you request a webpage from a server it is actually multiple requests. There will be a request for the actuall HTML source and then there will be other individual request for each image (or other media type) contained within the page.

Posted: Fri Jul 09, 2004 9:21 am
by malcolmboston
ok, you've been more than helpful.

but one last question:

what is ther best way around this?

Posted: Fri Jul 09, 2004 9:27 am
by redmonkey
The usual way to deal with this is a page that spits out the HTML and where you need the image dynamically generated it is put within a standard HTML img tag e.g.

Code: Select all

<img src="thumbnailer.php?fid=someid" />

Posted: Fri Jul 09, 2004 9:28 am
by malcolmboston
thank you readmonkey

I will try this all out tonight

Mal