Jump to content


Displaying Images on an HTML page

  • Please log in to reply
2 replies to this topic

#1 sturbitt

  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 20 March 2006 - 02:14 PM

I am trying to teach myself PHP and I have run through a few tutorial and set myself a project. I am now stuck.

I am trying to get PHP to generate thumnails for all my pictures, which are all in directories, and displaythem onto an HTML web page. I am running into a lot of issues with headers. I am sure this must be a fairly simple thing to do.

All my pictures are jpgs and are in the directory the PHP is running in.

Most of my tries have been cut and pasted examples. Any ideas or points in the right direction would be much Apprenticed. Here is one of my attempts:

<?php ob_start(); ?>


<br>Start of HTML.<br>

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

//echo "<br>$image<br>";

//$filename = $image;
$filename = "test.jpg";
$percent = 0.5;

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = 150;
$newheight = 150;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$filename = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $filename, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
//return $thumb;


#2 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 March 2006 - 03:21 PM

You can remove the lines before the "<?php" opening tag. This routine is not sending HTML code, but a binary image. Also, remove the "<?php ob_start(); ?>" and "ob_end_flush();".

A comment on your code:

You specify a "$percent = 0.5;", but then you have fixed the new height and width at 150px. This is going to make you thumbnail images look distorted. You should use:
$percent = 0.5;

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;


#3 sturbitt

  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 20 March 2006 - 03:58 PM

Thanks for the tips - my coding needs a lot of work as yet.

Howerver I still get a lot of gobbledy-gook out and / or header information warnings:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\tutorial\picture\index.php:2) in c:\program files\easyphp1-8\www\tutorial\picture\index.php on line 5
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀ––"ÿÄ ÿĵ}!1.........

Any ideas? I am tearing my hair out - and I didn't have much to start with! ;-)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users