i am definately a newbie on the subject of php, but nonetheless im trying to screw together a script for uploading pictures and then presenting thumbs of these pictures in a row below the upload dialog box.
This is a script that consists of several parts, and they are tied together by using the include - statement.
the one i'm working on now is the one that organises the display and listing of the thumbnails made from the uploaded pictures. only problem is that the thumbs that are listed cannot point ahead to the real image, so it's not all that usefull yet. I have tried to put the ahref-command from HTML into the php code, but then i can't use the string$-command to actually fetch the picture called for by the thumb.
l'll submit the code that i'm working on here, somebody please help! What I want is to have this row of thumbnails actually point to the pictures, so that the full size picture appear when the thumb is clicked. The uploading part and the listing part seem to work for now.
here is the upload script (these are not my own - i have combined them from several available tutorial scripts)
<?
include("functions.php");
//print_r($_POST);
thumbimage($imagename, 75, 75);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($FILES) && isset($HTTP_POST_FILES))
$FILES = $HTTP_POST_FILES;
if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";
$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
thumbimage($imagename, 75, 75);
$orig["dir"] = "images/";
$orig["dirimage"] = $orig["dir"] . $imagename;
$orig["res"] = @imagecreatefromjpeg($orig["dirimage"]);
$orig["x"] = imagesx($orig["res"]);
$orig["y"] = imagesy($orig["res"]);
if($orig["x"] > $orig["y"])
{
$new["x"] = 75;
$new["y"] = (75 / $orig["x"]) $orig["y"];
}
else
{
$new["y"] = 75;
$new["x"] = (75 / $orig["y"] ) $orig["x"];
}
//switch out imagecreatetruecolor with imagecreate if using gd version 1
//$new["res"] = imagecreate($new["x"],$new["y"]);
$new["res"] = imagecreatetruecolor($new["x"],$new["y"]);
//set background to white
$fill = imagecolorallocate($new["res"], 255, 255, 255);
imagefill($new["res"], 0, 0, $fill);
imagecopyresized($new["res"], $orig["res"], 0, 0, 0, 0, $new["x"], $new["y"], $orig["x"], $orig["y"]);
$new["dir"] = "images/";
$new["dirimage"] = $new["dir"] . $imagename . ".thumb.jpg";
imagejpeg($new["res"], $new["dirimage"]);
imagedestroy($orig["res"]);
imagedestroy($new["res"]);if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}
include("upload_form.php");
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($FILES) && isset($HTTP_POST_FILES))
$FILES = $HTTP_POST_FILES;
if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";
$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "success.";
}
}
include("upload_form.php");
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br> \n";
}
}
?><hr size="3"><?
include("list_images.php");
?>
save this as upload.php
here is the thumb-generator/listing script
<?
$handle = @opendir("images");
if(!empty($handle))
{
while(false !== ($file = readdir($handle)))
{
if(is_file("images/" . $file) && (substr($file, -9) == "thumb.jpg"))
echo '<a href="images/"$file"><img src="images/' . $file . '" border="0"></a> ';
}
}
closedir($handle);
?>
(save this script as list_images.php)
here is a function-script, also included through the main upload script.
<?
function thumbimage($imagename, $maxwidth, $maxheight)
{
$orig["dir"] = "images/";
$orig["dirimage"] = $orig["dir"] . $imagename;
$orig["res"] = @imagecreatefromjpeg($orig["dirimage"]);
$orig["x"] = imagesx($orig["res"]);
$orig["y"] = imagesy($orig["res"]);
if($orig["x"] > $orig["y"])
{
$new["x"] = $maxwidth;
$new["y"] = ($maxheight / $orig["x"]) $orig["y"];
}
else
{
$new["y"] = $maxheight;
$new["x"] = ($maxwidth / $orig["y"] ) $orig["x"];
}
//switch out imagecreatetruecolor with imagecreate if using gd version 1
//$new["res"] = imagecreate($new["x"],$new["y"]);
$new["res"] = imagecreatetruecolor($new["x"],$new["y"]);
//set background to white
$fill = imagecolorallocate($new["res"], 255, 255, 255);
imagefill($new["res"], 0, 0, $fill);
imagecopyresized($new["res"], $orig["res"], 0, 0, 0, 0, $new["x"], $new["y"], $orig["x"], $orig["y"]);
$new["dir"] = "images/";
$new["dirimage"] = $new["dir"] . $imagename . ".thumb.jpg";
imagejpeg($new["res"], $new["dirimage"]);
imagedestroy($orig["res"]);
imagedestroy($new["res"]);
return true;
}
?>
save this as "functions.php"
to test this upload these three scripts to a server, and make a dir called images in the same folder