PHP Multiple Upload & Multiple Resize to MySQL |
PHP Multiple Upload & Multiple Resize to MySQL เป็นการสร้าง Multiple Upload และ MultipleResize รูปภาพในรูปแบบของ Thumbnails และทำการบันทึกลงในฐานข้อมูล MySQL
PHP & GD
phpUploadResizeToMySQL1.html
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<form name="form1" method="post" action="phpMultiUploadResizeToMySQL2.php" enctype="multipart/form-data">
<input type="file" name="fileUpload[]"><br>
<input type="file" name="fileUpload[]"><br>
<input type="file" name="fileUpload[]"><br>
<input type="file" name="fileUpload[]"><br>
<input type="file" name="fileUpload[]"><br>
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
</html>
phpUploadResizeToMySQL2.php
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<?php
for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++)
{
if(trim($_FILES["fileUpload"]["tmp_name"][$i]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"][$i];
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"][$i];
copy($_FILES["fileUpload"]["tmp_name"][$i],"MyResize/".$_FILES["fileUpload"]["name"][$i]);
$width=100; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"MyResize/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Resize Successful.<br>";
//*** Insert Record ***//
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "INSERT INTO files ";
$strSQL .="(Thumbnails,FilesName) VALUES ('".$new_images."','".$_FILES["fileUpload"]["name"][$i]."')";
$objQuery = mysql_query($strSQL);
}
}
?>
<a href="phpMultiUploadResizeToMySQL3.php">View files</a>
</body>
</html>
phpUploadResizeToMySQL3.php
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<?php
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM files";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="200" border="1">
<tr>
<th width="50"> <div align="center">Files ID </div></th>
<th width="150"> <div align="center">Thumbnails </div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["FilesID"];?></div></td>
<td><center><a href="MyResize/<?php echo $objResult["FilesName"];?>">
<img src="MyResize/<?php echo $objResult["Thumbnails"];?>" border="0"></a></center></td>
</tr>
<?php
}
?>
</table>
<?php
mysql_close($objConnect);
?>
<br>
<a href="phpMultiUploadResizeToMySQL1.html">Upload Images</a>
</body>
</html>
Link แนะนำ ที่ควรศึกษา
|