 |
ช่วยตรวจสอบ source code ให้ด้วยค่ะ ....... พอดีไป download code มาจาก web ค่ะ พอนำ script มารันพบว่า script มันไม่สร้างไฟล์ thumbnails |
|
 |
|
|
 |
 |
|
พอดีไป download code มาจาก web ค่ะ พอนำ script มารันพบว่า script มันไม่สร้างไฟล์ thumbnails
ขึ้นมาค่ะ แต่สามารถ upload รูปได้ปกติค่ะ ไม่รู้ว่าผิดที่ไหนเพราะเป็นมือใหม่ด้วยค่ะ ที่อยากได้ script นี้เพราะว่า อยากได้ script
ที่สามารถจะ upload รูปภาพสร้างไฟล์ thumbnails และก็เปลี่ยนชื่อ ไฟล์ที่ upload ไปด้วยค่ะ ฝากผู้รู้ตรวจสอบให้ด้วยนะค่ะ ขอบคุณล่วงหน้าเลยค่ะ
Code (PHP)
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="imageUpload" size="40"/><br />
<input type="submit" value="Upload" id="uploadButton" />
</form>
<?php
// =======================================================
// THE MAIN SCRIPT - UPLOADS AND CREATES A THUMBNAIL
// ========================================================
// Path To Images Directory
$idir = "images/";
// Path To Thumbnails Directory
$tdir = "images/thumbnails/";
// Maximum Width For Thumbnail Images
$twidth = "170";
// Maximum Height For Thumbnail Images
$theight = "140";
// Check to see if proper file format (currently, jpg, jpeg, pjpeg, png, gif)
if (
$_FILES['image']['type'] == "image/jpg" ||
$_FILES['image']['type'] == "image/jpeg" ||
$_FILES['image']['type'] == "image/pjpeg" ||
$_FILES['image']['type'] == "image/png" ||
$_FILES['image']['type'] == "image/gif")
{
// Check to see if image is over 5mb
if ($_FILES['image']['size'] > 5242880) {
echo "Your Image Is Over the 5MB limit.";
exit();
}
$fileEXT = $_FILES['image']['type'];
// Get the file extention, eg .jpg
$file_ext = strrchr($_FILES['image']['name'], '.');
// Get the current unix time, this will be used in the file name, better to use this method than rand(), less CPU and there is a chance that the rand function may return the same var
$time = time();
// Converge it all together, to place into the dir, file name and extention
$url = $idir . $time . $file_ext;
// Move the image to its perm location
$copy = copy($_FILES['image']['tmp_name'], $url);
// If the move was successful
if ($copy) {
switch ($fileEXT)
{
case 'image/jpeg':
$simg = imagecreatefromjpeg($url);
break;
case 'image/gif':
$simg = imagecreatefromgif($url);
break;
case 'image/png':
$simg = imagecreatefrompng($url);
break;
default:
return false;
}
// Image Width
$currwidth = imagesx($simg);
// Image Height
$currheight = imagesy($simg);
// If Height Is Greater Than Width
if ($currheight > $currwidth) {
$zoom = $twidth / $currheight;
$newheight = $theight;
$newwidth = $currwidth * $zoom;
} else {
$zoom = $twidth / $currwidth;
$newwidth = $twidth;
$newheight = $currheight * $zoom;
}
// Make New Image For Thumbnail
$dimg = imagecreate($newwidth, $newheight);
// Create New Color Pallete
imagetruecolortopalette($simg, false, 256);
$palsize = ImageColorsTotal($simg);
// Counting Colors In The Image
for ($i = 0; $i < $palsize; $i++) {
$colors = ImageColorsForIndex($simg, $i);
// Tell The Server What Colors This Image Will Use
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
}
// Copy Resized Image To The New Image
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
// This is the end Thumbnail location
$thuURL = $tdir . $time . $file_ext;
switch ($fileEXT)
{
case 'image/jpeg':
$src = imagejpeg($dimg, $thuURL);
break;
case 'image/gif':
$src = imagegif($dimg, $thuURL);
break;
case 'image/png':
$src = imagepng($dimg, $thuURL);
break;
default:
$src = imagejpeg($dimg, $thuURL);
}
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
echo "Upload went well";
} else {
$uploadError = "There was an error uploading your image";
exit();
}
} else {
echo "Wrong File Type";
exit();
}
?>
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2009-01-10 22:00:34 |
By :
waree |
View :
1231 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
น่าจะมีข้อผิดพลาดแจ้งมาบ้างน่ะครับ ยังไงรบกวนตรวจสอบว่า GD ทำงานปกติหรือเปล่าน่ะครับ
PHP & GD
|
 |
 |
 |
 |
Date :
2009-01-11 17:18:20 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอน upload ไม่มี error เลยค่ะ และได้ตรวจสอบที่ gd แล้วพบว่า enable อยู่นะค่ะ
|
 |
 |
 |
 |
Date :
2009-01-11 18:23:02 |
By :
waree |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|