Jump to content

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(); ?>

<html>
<body>

<br>Start of HTML.<br>

<?php
// 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
imagejpeg($thumb);
//return $thumb;

ob_end_flush();
?>
Link to comment
https://forums.phpfreaks.com/topic/5345-displaying-images-on-an-html-page/
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:
[code]<?php
$percent = 0.5;

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
?>[/code]

Ken
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.........
[/quote]

Any ideas? I am tearing my hair out - and I didn't have much to start with! ;-)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.