Really desperate now .. re - displaying images

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
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

Really desperate now .. re - displaying images

Post by Fari »

a simple script in the include file reduces the size and sends the image to the display - the result is a text output of the image content instead of the image! Plus a Header error message - i.e. you cannot send a header once output is started to HTML .. but then where the f@ck is the header() function supposed to go????

include:

Code: Select all

<?php

function arial_2 ($texi)
    &#123; 
	 echo "<FONT FACE="ARIAL" SIZE="2" color="#1c651c">" . $texi . "</FONT>";
	&#125;
	
function showerror()
  &#123;
      arial_2("Error " . mysql_errno() . " : " . mysql_error());
	  die();
  &#125;

function image_reduce( $to_be_reduced )
  
  &#123;
  
  $temp_image = @ imagecreatefromjpeg ( $to_be_reduced );
  $orig_width = imagesx( $temp_image );	
  $orig_height = imagesy( $temp_image );
  if (($orig_width > 100 ) || ($orig_height > 120 ))
		&#123;
		$ratio = (real)( $orig_height / $orig_width );
		$new_height = 120; $new_width = (int)(120 / $ratio );
		$new_image = imagecreate ( $new_width, $new_height ) ;
		imagecopyresized ( $new_image , $temp_image , 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
		header("Content-type: image/jpeg");
		imagejpeg ( $new_image );
		return true;
		&#125; 
  else return false;		
 &#125;
	
 ?>
the script calling this ( around row 81-82 where the image_reduce is called)

Code: Select all

<?php
session_start();
include 'includes.php';
?>
<HTML>
<HEAD>
     <META NAME="KEYWORDS" CONTENT="lukacsovics">
  <TITLE>Test</TITLE>
</HEAD>
<BODY>
<?php

echo"<TABLE>";     //THIS IS A SPACER TABLE!
echo"<TR HEIGHT="50">"; 
echo"<TD WIDTH="150">";
echo"</TR></TABLE>"; 

echo"<TABLE BORDER="1" BORDERCOLOR="#003366" CELLSPACING="0" CELLPADDING="4">";

echo"<TR VALIGN="CENTER" HEIGHT="20"><TD ALIGN="LEFT" WIDTH="180">";
arial_2("Database selected to view :  ");
echo"</TD><TD ALIGN="LEFT" WIDTH="150">"; arial_2($db_sel); echo"</TD></TR>";

echo"<TR VALIGN="CENTER"><TD VALIGN="CENTER" ALIGN="LEFT">";
arial_2("Table selected to view :  ");
echo"</TD><TD ALIGN="LEFT">"; arial_2($tab_select); echo"</TD></TR>";

echo"<TR><TD ALIGN="LEFT">";
arial_2("Message : ");
echo"</TD>";
if (!($connection =  mysql_connect($server,$login,$pwd)))
 &#123;
   echo "<TD>";arial_2("Could not Connect!"); echo"</TD></TR>";
   showerror();
 &#125;
echo"<TD ALIGN="LEFT">"; 
arial_2("Connection Established");
echo"</TD></TR>";

mysql_select_db($db_sel);

$query = "SELECT * FROM " . $tab_select; 

$result = mysql_query( $query );

$fields = mysql_num_fields( $result );

$table = mysql_field_table ( $result , 0 );

$rows = mysql_num_rows( $result ); // get the number of rows in the table

echo"<TR VALIGN="CENTER"><TD VALIGN="CENTER" ALIGN="LEFT">";
arial_2("Number of columns :  ");
echo"</TD><TD ALIGN="LEFT">"; arial_2($fields); echo"</TD></TR>";

echo"<TR><TD ALIGN="LEFT">"; arial_2("No. of records : "); echo"</TD>";

echo"<TD ALIGN="LEFT">"; arial_2($rows); echo"</TD></TR>";
echo"</TABLE>";

echo"&nbsp";

echo"<TABLE BORDER="1" BORDERCOLOR="#003366" CELLSPACING="1" CELLPADDING="4">";

echo"<TR>";
for( $i=0 ; $i< $fields ; $i++) 
 &#123; echo "<TD>"; 
   arial_2(mysql_field_name( $result , $i )); 
   echo "</TD>";
 &#125;
 
echo"</TR>";

while ( $row = @ mysql_fetch_array ( $result ))
    &#123;
    echo"<TR>";
	for( $i=0 ; $i< $fields ; $i++) 
	   &#123;
	    echo"<TD>";
		 if ((mysql_field_type ( $result , $i ) == 'blob'))  
			if (!image_reduce($row&#1111;$i])) echo"<IMG SRC="" . $row&#1111;$i] . "">"; //no need to reduce image
			else  image_reduce($row&#1111;$i]);
			
 		 elseif (mysql_field_type ( $result , $i ) <> 'blob') arial_2($row&#1111;$i]);
		
	   &#125;	 
  	 echo"</TR>";
	 &#125;
echo"</TABLE>";
 
mysql_close($connection);
  
?>
</BODY>
</HTML>
and the resulting output is:

Code: Select all

Warning: Cannot add header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\test\view_table_3.php:11) in c:\program files\apache group\apache\htdocs\test\includes.php on line 28
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀxZ"ÿÄ ÿĵ&#125;!
please have mercy on my poor soul!!! :oops:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

the problem is that your trying to display the image inline to your code. this is not possible. you need to call it from a seperate acript which does nothing elese put display your image.
i.e.

Code: Select all

&lt;img src="image_resize_script.php?image=image_to_resize.jpg" /&gt;
with that, image_resize_script.php does one thing, resize your image. no html or anything else, just resize yourimage. its contents may look like this

Code: Select all

$temp_image = @ imagecreatefromjpeg ( $_POST&#1111;'image'] );
  $orig_width = imagesx( $temp_image );   
  $orig_height = imagesy( $temp_image );
  if (($orig_width &gt; 100 ) || ($orig_height &gt; 120 ))
      {
      $ratio = (real)( $orig_height / $orig_width );
      $new_height = 120; $new_width = (int)(120 / $ratio );
      $new_image = imagecreate ( $new_width, $new_height ) ;
      imagecopyresized ( $new_image , $temp_image , 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
      header("Content-type: image/jpeg");
      imagejpeg ( $new_image );
      }
this is completely untested but i did pull it from your code and change like two lines. how that will work into your previous code, im not sure but this should get you on the right track.
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

that won't work...

Post by Fari »

...coz the header() function is called in the HTML body - i.e. AFTER the the output has started!

F
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if (!image_reduce($row[$i])) echo"<IMG SRC=\"" . $row[$i] . "\">"; //no need to reduce image
else image_reduce($row[$i]);
in the first case a <img>-tag like <img src="whereever.jpg"> is added to the html-document-stream, in the ladder the binary image data.
The first might work, but have you ever opened the source view of your browser for a page that contains images? Have you ever seen the image data to be within that document (I'm still ignoring netscape's inline image data ;) )?
Replace the above code block completely by

Code: Select all

echo '<IMG SRC="getImage.php?pic=', urlencode($row&#1111;$i]), '" />';
and then write a script getImage.php that roughly does what is in your include-file:
decide wether the image size is feasable or not. If feasable --> readfile and done, if not: reduce and output
Post Reply