Making thumbnails with GD Library

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
Trajkovski
Forum Newbie
Posts: 2
Joined: Wed Jan 12, 2005 5:56 pm
Location: Denmark

Making thumbnails with GD Library

Post by Trajkovski »

Hi

I'm trying to get GD library to make thumbnails of some pictures I've got.

The script:
[syntax=php]<?
header("Content-type: image/jpeg");
header("Content-type: image/png");
header("Content-type: image/gif");

require("../billeder/mysql/mysql-funktioner.php");

$dir = "../billeder/files/";

$vis_aar = $_GET['vis_aar'];
$dette_aar = date("Y");
$tabel_billed_hoejde = "110";
$tabel_billed_bredde = "110";

//JPEG funktion
function thumb_jpeg($img_navn)
{
global $dir;
global $tabel_billed_bredde;
global $tabel_billed_hoejde;

$thumb = ImageCreateTrueColor($tabel_billed_bredde,$tabel_billed_hoejde) or die("Der opstod et problem da billedet skulle laves");

$srcimg = ImageCreateFromJPEG($dir.$img_navn) or die("Det angivne billed kunne ikke åbnes");

ImageCopyResampled($thumb,$srcimg,0,0,0,0,$tabel_billed_bredde,$tabel_billed_hoejde,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

ImageJPEG($thumb) or die("Billedet kunne ikke sendes til browseren");
}

//PNG funktion
function thumb_png($img_navn)
{
global $dir;
global $tabel_billed_bredde;
global $tabel_billed_hoejde;

$thumb = ImageCreateTrueColor($tabel_billed_bredde,$tabel_billed_hoejde) or die("Der opstod et problem da billedet skulle laves");

$srcimg = ImageCreateFromPNG($dir.$img_navn) or die("Det angivne billed kunne ikke åbnes");

ImageCopyResampled($thumb,$srcimg,0,0,0,0,$tabel_billed_bredde,$tabel_billed_hoejde,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

ImagePNG($thumb) or die("Billedet kunne ikke sendes til browseren");
}

//GIF funktion
function thumb_gif($img_navn)
{
global $dir;
global $tabel_billed_bredde;
global $tabel_billed_hoejde;

$thumb = ImageCreateTrueColor($tabel_billed_bredde,$tabel_billed_hoejde) or die("Der opstod et problem da billedet skulle laves");

$srcimg = ImageCreateFromGIF($dir.$img_navn) or die("Det angivne billed kunne ikke åbnes");

ImageCopyResampled($thumb,$srcimg,0,0,0,0,$tabel_billed_bredde,$tabel_billed_hoejde,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

ImageGIF($thumb) or die("Billedet kunne ikke sendes til browseren");
}

if(!isset($vis_aar)){
$vis_aar = $dette_aar;
}

aabn_forbindelse_og_vaelgdb();
$sql = mysql_query("SELECT * FROM billeder WHERE kategori='$vis_aar' AND godkendt='1' ORDER BY id");

echo "<h3>Billeder fra $vis_aar</h3><br><br><br><br>";
echo "<table height=$tabel_billed_hoejde width=$tabel_billed_bredde>";

while($b = mysql_fetch_array($sql)){
$id = $b["id"];
$billednavn = $b["billednavn"];
$kommentar = $b["kommentar"];
$antal_billeder = count($id);

//Find extension på fil
$len = strlen($billednavn);
$pos =strpos($billednavn,".");
$extension = substr($billednavn,$pos + 1,$len);
$extension = strtolower($extension);

if($extension == "jpeg" || $extension == "jpg"){
$billednavn = thumb_jpeg($billednavn);
}
elseif($extension == "png"){
$billednavn = thumb_png($billednavn);
}
elseif($extension == "gif"){
$billednavn = thumb_gif($billednavn);
} else {
$billednavn = $billednavn;
}


echo "<tr valign=left></td><td><a href=".$dir."/".$billednavn." target=\"_blank\"><img src=".$dir."/".$billednavn." width=$tabel_billed_bredde height=$tabel_billed_hoejde border=0></a></td></tr></tr><tr valign=top><td><br>".$kommentar."</td></tr>";
}
echo "</table>";
?>[/syntax]


This little script results in a very interesting output :wink: I think it returns the pictures in code but have a look yourself:

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀnn"ÿÄ ÿĵ}!1AQa"q2
Trajkovski
Forum Newbie
Posts: 2
Joined: Wed Jan 12, 2005 5:56 pm
Location: Denmark

Post by Trajkovski »

Hmm... How come the [syntax=php]doesn't work?[/syntax]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

has been temporarily disabled due to our update.. it'll return soon..

As for your problem, firstly, you're outputting a jpeg, but telling the browser it's a gif.. I'd move your header() calls to within the individual functions.

secondly, you're echoing stuff. When outputting an image, you cannot echo anything or you will corrupt the image file sent to the browser.

Images are sent in seperate streams from HTML. You need to create an image output script that does this processing. The image script can only output 1 image per call, otherwise, you will corrupt the image stream.
Post Reply