Log in

View Full Version : [PHP] GD Image Help



Luno1599
20-10-2009, 08:09 PM
Hey,


I made this code, but it only echos one image and there is 7 images in that directory... but it only echos one image resized, how would I fix the code to echo all 7 one after the other??

CODE:

<?php
// Generates thumnail graphic
function doGD($path,$jpg) {
$jpgFile = "$path/$jpg";
$width = 120;
list($width_orig, $height_orig) = getimagesize($jpgFile);
$height = (int) (($width / $width_orig) * $height_orig);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($jpgFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 60);
imagedestroy($image_p);
}

//define the path as relative
$path = "test/";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

//running the while loop
while ($file = readdir($dir_handle))
{

if($file == "." || $file == ".."){
$file = "";
}else{
header('Content-type: image/jpeg');
doGD("$path","$file");
}

}

//closing the directory
closedir($dir_handle);
?>


Dan

Delatory
20-10-2009, 08:48 PM
ImageJPG outputs the picture.
What you'll have to do is have 2 files.

doGD.php


if(!$_REQUEST['path'] || !$_REQUEST['jpg']) exit();
$jpgFile = $_REQUEST['path']."/".$_REQUEST['jpg'];
$width = 120;
list($width_orig, $height_orig) = getimagesize($jpgFile);
$height = (int) (($width / $width_orig) * $height_orig);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($jpgFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
header('Content-type: image/jpeg');
imagejpeg($image_p, null, 60);
imagedestroy($image_p);
Main Code


//define the path as relative
$path = "test/";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

//running the while loop
while ($file = readdir($dir_handle))
{
if($file != "." && $file != ".."){
echo "<img src=\"doGD.php?path=".$path."&jpg=".$file." />";
}
}
//closing the directory
closedir($dir_handle);

Luno1599
20-10-2009, 08:53 PM
EDIT:

Sorry the ticko was looking at the wrong file!!!

I have tryed your script but its echoing 4 boxes with no images in them just like a page in a little box

Dan

Luno1599
20-10-2009, 09:10 PM
FIXED Thanks for your help!

Luno1599
20-10-2009, 10:26 PM
Need some more, help!!

How would I add this? I want to add a watermark to at a 45 degree angle


$black = imagecolorallocate($imgrdg, 0, 0, 0);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add the text
imagettftext($imgrdg, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($imgrdg);
imagedestroy($imgrdg);




Dan

Want to hide these adverts? Register an account for free!