Hello.
I wrote a script to upload images and then preview a thumbnail.
My upload form is in a seperate file called upload_form.inc
Everything worked until I tried to add support for gif and png in the [I]thumbnail function
Please, what is wrong? is it something to do with the way I nested the if statement?
[code=php]<html>
<head>
<title>Upload</title>
</head>
<body>
<?php
if(!isset($_POST[‘Upload’]))
{
include(“upload_form.inc”);
exit();
}/*endif*/
else
{
if($_FILES[‘pix’][‘tmp_name’] == “none”)
{
echo “<b>File did not successfully upload. Check the file size. File must be less than 1Mb.<br>”;
include(“upload_form.inc”);
exit();
}
if(!ereg(“image”,$_FILES[‘pix’][‘type’]))
{
echo “<b>File is not a picture. Please try another file.</b><br>”;
include(“upload_form.inc”);
exit();
}
else
{
function thumbnail($image_path,$thumb_path,$image_name,$thumb_width)
{
if(ereg(“image/pjpeg”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefromjpeg(“$image_name”);
}
elseif(ereg(“image/gif”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefromgif(“$image_name”);
}
elseif(ereg(“image/png”,$_FILES[‘pix’][‘type’]))
{
$src_img = imagecreatefrompng(“$image_name”);
}
else
{
$src_img = imagecreatefromjpeg(“$image_name”);
}
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$ratio=$thumb_width/$origw;
$new_h=$origh * $ratio;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, “$thumb_path/$image_name”);
return true;
}
}
$ran = rand (1, 100000) ;
$destination = $firstname.””.$ran.”.”.$_FILES[‘pix’][‘name’];
$temp_file = $_FILES[‘pix’][‘tmp_name’];
move_uploaded_file($temp_file,$destination);
$imgpath = “http://www.mywebsite.ch/rwt/junk/UPLOAD%20AND%20SEND/{$ran}.{$_FILES[‘pix’][‘name’]}”;
echo “<p><b>Your image:</b></p>”;
thumbnail(0,”thumbs”,”{$ran}.{$_FILES[‘pix’][‘name’]}”,200);
echo “<a href=’$imgpath’ title=’$firstname portrait’ target=’_blank’ alt=’$firstname portrait><img src=’http://www.mywebsite.ch/rwt/junk/UPLOAD%20AND%20SEND/thumbs/{$ran}.{$_FILES[‘pix’][‘name’]}’ style=’border: 1px solid #C0C0C0′ alt=’$firstname’></a>”;
}
?>
</body>
</html>