Jump to content


This topic is now archived and is closed to further replies.


Displaying Images on an HTML page

Recommended Posts

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;


Share this post

Link to post
Share on other sites
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;


Share this post

Link to post
Share on other sites
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! ;-)

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.