|
|
|
Insert และ Update รูป... Rename,Resize แบบมี Thumb เห็นถามกันบ่อยเลยเอามาลงให้ครับ... |
|
|
|
|
|
|
|
Upload...
<?
error_reporting(E_ALL ^ E_NOTICE);
session_start();
//require("../auth.inc.php");
$photo = $_FILES['photo']['tmp_name'];
$photo_name = $_FILES['photo']['name'];
$photo_size = $_FILES['photo']['size'];
//$user = $_SESSION['ses_user'];
date_default_timezone_set('Asia/Bangkok');
$time = date("Y-m-d-H-i-s");
$ext = strtolower(end(explode('.', $photo_name)));
if($ext!="jpg" and $ext!="jpeg" and $ext!="png" and $ext!="gif"){
?>
<html>
<title>Upload Failed...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="2.8; url=my_account.php" >
<style type="text/css">
html { height:100%; width:100%; }
body {
width:100%;
height:100%;
margin:0px;
padding:0px;
border:0px;
font-family: Verdana, Geneva,Segoe UI, sans-serif;
font-size:13px;
line-height:15px;
}
.bg{
height:92%;
width:100%;
margin-bottom:0px;
background:url(../images/sky_bg.jpg);
}
.footerl{
width:100%;
height:8%;
margin-bottom:0px;
border:0px;
background-color:#09F;
}
</style>
<div class="bg">
<?
echo "<br /> ไม่สามารถอัปโหลด ".$photo_name." , ( .".$ext." )";
?>
<center>
<br /><br />
<img src="../images/Security_graphic.png" border="0" /><br /><br /><br />
<b><font color="#000099" size="+1" face="Comic Sans MS, cursive">Can not upload ! รอสักครู่ ...</font></b></center>
<br /><br /><br />
</div>
<div class="footerl">
<center>
<table height="100%" width="100%" border="0" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#00FFFF">
<center><font size="-1">
CopyRight @
</font>
</center>
</td>
</tr>
</table>
</center>
</div>
</html>
<?
exit();
}else{
$file_name = $time."-".session_id().".".$ext;
if($ext == "jpg" or $ext == "jpeg" or $ext == "png" or $ext == "gif"){
//copy($photo,$file_name);
if($ext == "jpg" or $ext == "jpeg"){
$ori_img = imagecreatefromjpeg($photo);
}else if($ext == "png"){
$ori_img = imagecreatefrompng($photo);
}else if($ext == "gif"){
$ori_img = imagecreatefromgif($photo);
}
$ori_size = getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
if($ori_w > 84){
$new_w = 84;
$new_h = round(($new_w/$ori_w) * $ori_h);
$new_img = imagecreatetruecolor($new_w, $new_h);
$thumb = imagecreatetruecolor(30, 30);
imagecopyresized($new_img, $ori_img, 0,0,0,0, $new_w, $new_h, $ori_w, $ori_h);
if($ext == "jpg" or $ext == "jpeg"){
imagejpeg($new_img,"../images/avatar_user/$file_name");
imagecopyresized($thumb, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagejpeg($thumb,"../images/avatar_thumb/$file_name");
}else if($ext == "png"){
imagepng($new_img,"../images/avatar_user/$file_name");
imagecopyresized($thumb, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagepng($thumb,"../images/avatar_thumb/$file_name");
}else if($ext == "gif"){
imagegif($new_img,"../images/avatar_user/$file_name");
imagecopyresized($thumb, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagegif($thumb,"../images/avatar_thumb/$file_name");
}
imagedestroy($new_img);
imagedestroy($thumb);
}else{
$ori_size = getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
$new_img = imagecreatetruecolor(30, 30);
if($ext == "jpg" or $ext == "jpeg"){
imagejpeg($ori_img,"../images/avatar_user/$file_name");
imagecopyresized($new_img, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagejpeg($new_img,"../images/avatar_thumb/$file_name");
}else if($ext == "png"){
imagepng($ori_img,"../images/avatar_user/$file_name");
imagecopyresized($new_img, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagepng($new_img,"../images/avatar_thumb/$file_name");
}else if($ext == "gif"){
imagegif($ori_img,"../images/avatar_user/$file_name");
imagecopyresized($new_img, $ori_img, 0,0,0,0, 30, 30, $ori_w, $ori_h);
imagegif($new_img,"../images/avatar_thumb/$file_name");
}
}
require("../includes/connection.php");
mysql_select_db($db_name,$conn);
mysql_query("SET NAMES UTF8",$conn);
$sql_select = "SELECT photo FROM user WHERE user_name='$_SESSION[ses_user]' ";
$db_query_photo = mysql_query($sql_select,$conn) or die("Error Qeury [".$db_query."]");
$sql_photo = mysql_fetch_array($db_query_photo);
$selected = $sql_photo['photo'];
if($selected != NULL){
$sql_update ="UPDATE user SET photo='$file_name',last_update='$time' WHERE user_name='$_SESSION[ses_user]' ";
mysql_db_query($db_name,$sql_update);
unlink("../images/avatar_user/$selected");
unlink("../images/avatar_thumb/$selected");
}else{
$sql_insert = "INSERT INTO user (photo,last_update) VALUES('$file_name','$time') WHERE user_name='$_SESSION[ses_user]' ";
mysql_db_query($db_name,$sql_insert);
}
imagedestroy($ori_img);
}else{
?>
<html>
<title>Upload Failed...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="2.8; url=my_account.php" >
<style type="text/css">
html { height:100%; width:100%; }
body {
width:100%;
height:100%;
margin:0px;
padding:0px;
border:0px;
font-family: Verdana, Geneva,Segoe UI, sans-serif;
font-size:13px;
line-height:15px;
}
.bg{
height:92%;
width:100%;
margin-bottom:0px;
background:url(../images/sky_bg.jpg);
}
.footerl{
width:100%;
height:8%;
margin-bottom:0px;
border:0px;
background-color:#09F;
}
</style>
<div class="bg"><center>
<br /><br />
<img src="../images/Security_graphic.png" border="0" /><br /><br /><br />
<b><font color="#000099" size="+1" face="Comic Sans MS, cursive">Can not upload ! รอสักครู่ ...</font></b></center>
<br /><br /><br />
</div>
<div class="footerl">
<center>
<table height="100%" width="100%" border="0" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#00FFFF">
<center><font size="-1">
CopyRight @
</font>
</center>
</td>
</tr>
</table>
</center>
</div>
</html>
<?
exit();
}
?>
<html>
<title>Uploading...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="refresh" content="3; url=my_account.php" >
<style type="text/css">
html { height:100%; width:100%; }
body {
width:100%;
height:100%;
margin:0px;
padding:0px;
border:0px;
font-family: Verdana, Geneva,Segoe UI, sans-serif;
font-size:13px;
line-height:15px;
}
.bg{
height:92%;
width:100%;
margin-bottom:0px;
background:url(../images/sky_bg.jpg);
}
.footerl{
width:100%;
height:8%;
margin-bottom:0px;
border:0px;
background-color:#09F;
}
.fonts{
font-size:13px;
}
</style>
<div class="bg">
<?
echo " <table width=\"500\" border=\"0\" cellspacing=\"3\" cellpadding=\"0\">
<tr>
<td width=\"20\" align=\"right\" valign=\"middle\" ><img src=\"../images/animation/loading_small.gif\" border=\"0\" /></td>
<td width=\"480\" align=\"left\" valign=\"middle\"><div class=\"fonts\">กำลังอัฟโหลด : <b>".$photo_name."</b> , ขนาด : <b>".$photo_size."</b> Byte</div></td>
</tr>
</table>";
?>
<center>
<br /><br />
<img src="../images/pictures-reflect.png" border="0" /><br /><br /><br />
<b><font color="#000099" size="+1" face="Comic Sans MS, cursive">Uploading : รอสักครู่ ...</font></b></center>
<br /><br /><br />
</div>
<div class="footerl">
<center>
<table height="100%" width="100%" border="0" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#00FFFF">
<center><font size="-1">
CopyRight @
</font>
</center>
</td>
</tr>
</table>
</center>
</div>
</html>
<?
mysql_close();
}
?>
Insert และ Update รูป... Resize แบบมี thumb
เป็นตัวเก่าที่ผมเคยทำไว้...
Tag : PHP
|
ประวัติการแก้ไข 2010-09-09 01:17:52 2010-09-09 19:38:06
|
|
|
|
|
Date :
2010-09-08 20:27:13 |
By :
t-monroe |
View :
6394 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอตัวอย่างแบบสั้นๆ มีไหมค๊ะ ยาวแบบนี้ไม่รู้จะไปปรับแก้ตรงไหนเลย
ขอบคุณนะค๊ะ
|
|
|
|
|
Date :
2010-09-09 08:06:36 |
By :
มิ้น |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks...
|
|
|
|
|
Date :
2010-09-09 14:09:12 |
By :
Goddd... |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ความจริงถ้าตัด ส่วนที่เป็น HTML ออก มันก็ไม่ยเยอะนะครับ...
|
|
|
|
|
Date :
2010-09-09 19:37:30 |
By :
t-monroe |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แบบว่าเราอัพข้อมูลกับรูปไปแล้ว แต่เราต้องการแก้ไข แต่ตอนแต่ไขนั้นอัพรูปไม่ได้ ต้องทำอย่างไร ??
|
|
|
|
|
Date :
2011-01-25 13:24:28 |
By :
fasaiaya |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้ code ผมดัดแปลงนิดหน่อยจากใน บทเรียนนะครับ
Multible Upload & Resize ( เพิ่มข้อมูลลง Database M.Access )
HTML CODE
<html>
<head>
<title>Multible Upload & Resize</title>
</head>
<script language="javascript">
function fncCreateElement(){
var mySpan = document.getElementById('mySpan');
var myLine = document.getElementById('hdnLine');
myLine.value++;
// Create input file
var myElement2 = document.createElement('input');
myElement2.setAttribute('type',"file");
myElement2.setAttribute('name',"fileUpload"+myLine.value);
myElement2.setAttribute('id',"fil"+myLine.value);
mySpan.appendChild(myElement2);
// Create <br>
var myElement3 = document.createElement('<br>');
myElement3.setAttribute('id',"br"+myLine.value);
mySpan.appendChild(myElement3);
}
function fncDeleteElement(){
var mySpan = document.getElementById('mySpan');
var myLine = document.getElementById('hdnLine');
if(myLine.value > 1 )
{
// Remove input file
var deleteFile = document.getElementById("fil"+myLine.value);
mySpan.removeChild(deleteFile);
// Remove <br>
var deleteBr = document.getElementById("br"+myLine.value);
mySpan.removeChild(deleteBr);
myLine.value--;
}
}
</script>
<body>
<form action="multi_upload1.php" method="post" name="form1" enctype="multipart/form-data">
<input type="file" name="fileUpload1">
<input name="btnCreate" type="button" value="+" onClick="JavaScript:fncCreateElement();">
<input name="btnDelete" type="button" value="-" onClick="JavaScript:fncDeleteElement();"><br>
<span id="mySpan"></span>
<input name="hdnLine" type="hidden" value="1">
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
</html>
PHP CODE
<?php ob_start(); ?>
<html>
<head>
<title>Multible Upload & Resize</title>
</head>
<body>
<?php
$strConn = new COM("ADODB.Connection") or die("Cannot start ADO");
$strConn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("db/db_main.mdb"));
for($j=1;$j<=(int)($_POST["hdnLine"]);$j++)
{
if($_FILES["fileUpload".$j]["name"] != "")
{
$sql = "select * from count";
$objRec = $strConn->Execute($sql);
$i = $objRec->Fields["num"]->Value + 1 ;
$sql = "update count set num=$i";
$strConn->Execute($sql);
$s_pic = "Thumbnails_".$i."_".$_FILES["fileUpload".$j]["name"];
$l_pic = $i."_".$_FILES["fileUpload".$j]["name"];
$sql = "insert into picture (s_picture,l_picture) values ('$s_pic','$l_pic')";
$strConn->Execute($sql);
$images = $_FILES["fileUpload".$j]["tmp_name"];
$new_images = "Thumbnails_".$i."_".$_FILES["fileUpload".$j]["name"];
copy($_FILES["fileUpload".$j]["tmp_name"],"MyResize/".$i."_".$_FILES["fileUpload".$j]["name"]);
$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/thumbnails/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
}
$strConn->Close();
$strConn = null;
header("Location: multi_slide.php");
?>
</body>
</html>
<?php ob_end_flush() ; ?>
|
|
|
|
|
Date :
2011-01-25 16:00:09 |
By :
lightkung |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รบกวนถามหน่อยนะคับ พอดีไม่รู้เรื่องไรเลย
Code (PHP)
for($i=0;$i<count($_FILES["fileUpload"]["name"]);$i++)
{
if(trim($_FILES["fileUpload"]["tmp_name"][$i]) != "")
จากโค้ด2บรรทัดนี้ ที่
["fileUpload"]
["name"]
["tmp_name"]
มันคืออะไรหรอคับ เป็นคำสั่งหรือฟังก์ชัน หรือว่าชื่อ ไฟล์ อ่ะ คือผมอยากรู้อ่ะคับ ก้อเลยสงสัย^^
ขอบคุณครับ
|
|
|
|
|
Date :
2011-06-08 15:20:33 |
By :
^^ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|