Author Topic: Upload ảnh và tạo thumbnail  (Read 16336 times)

Knigherrant

  • PHP Starter
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #15 on: January 23, 2009, 12:51:15 AM »
Tết rùi rảnh quá nên post bài MultiFile Upload và tạo thumbnail (resize image theo tỉ lệ). Mình viết trên 1 file MultiFile_Upload.php.Tổng hợp từ các bài viết của 4rom  ;):
Phần html
Code: [Select]
<html>
<head>
<h2>MultiFile Upload</h2>
</head>
<form action="" method="POST" enctype="multipart/form-data">
<?php for($i 0$i <5$i ++):?>
<p><input type="file" name="image[]"></p>
<?php endfor;?>
<input type="submit" name="upload" value="Upload">
</form>
</html>
Phần php
Code: [Select]
function upload_image(){
for($i=0; $i<5; $i++){
$fileupload = $_FILES['image']['name'][$i];
$path_thub = './thub_image/';
$path_image = './images/'.$fileupload;
$max_width = 200;
$max_height = 160;
if(trim($fileupload) != '' ){
$ext = getExt($fileupload);
$ext_upload = array('jpg','jpeg','png','bmp','gif');
if(in_array($ext,$ext_upload)){
move_uploaded_file($_FILES['image']['tmp_name'][$i],$path_image);
list($width,$height) = getimagesize($path_image);
                                //kiểm tra nếu file upload nhỏ hơn kích thước quy định thì lấy kích thước cũ.
if($width < $max_width && $height < $max_height){
$new_width = $width;
$new_height = $height;
}else{
$sx = $width/$max_width;
$sy = $height/$max_height;
                                        //resize theo tỷ lệ.
if ($width > $height) {
$new_height = round($height/$sx, 0);
$new_width = $max_width;
} else {
$new_width = round($width/$sy, 0);
$new_height = $max_height;
}
}
$image_thub = imagecreatetruecolor($new_width,$new_height);
switch($ext){
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($path_image);
break;
case 'png':
$image = imagecreatefrompng($path_image);
break;
case 'gif':
$image = imagecreatefromgif($path_image);
break;
default:
$image = imagecreatefromjpeg($path_image);
break;
}
imagecopyresampled($image_thub,$image,0,0,0,0,$new_width,$new_height,$width,$height);
$thumb_file = 'thumb_'.$_FILES['image']['name'][$i];
$tmp_content = imagejpeg($image_thub, $path_thub.$thumb_file, 100);
}
else{
echo 'File khong hop le';
}
}
}
}
function getExt($filename){
return $ext = strtolower(substr(strrchr($filename,'.'),1));
}
Các bạn có thể down file hiàn chỉnh :D

testmotlan

  • PHP Starter
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #16 on: October 13, 2009, 12:03:47 PM »
Link chết rồi bạn ơi

bungbu84

  • PHP Intermediate
  • *
  • Posts: 34
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #17 on: January 21, 2010, 02:53:37 PM »
Link attach mà down không được. Pó tay luôn.

Socloseves

  • PHP Starter
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #18 on: January 02, 2011, 07:54:50 PM »
Làm thế nào để trả về 1 đoạn get link images sau khi đã upload vậy  :'(

KenyLieou

  • Global Moderator
  • PHP Intermediate
  • *****
  • Posts: 908
  • Nothing's Impossible
    • MSN Messenger - kiensuy2000@hotmail.com
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #19 on: January 03, 2011, 10:02:32 AM »
sau khi upload nó sẽ nằm trog tmp, bạn phải move nó qua folder sau www thì lúc đó sẽ link bình thường
VD bạn move nó qua www/upload/
thì hình là _http://yourdomain/upload/xxx.gif

p/s dùng hàm move_uploaded_file http://vn2.php.net/manual/en/function.move-uploaded-file.php
+ PHPVN Supporter or Spamer :D 
+ Email: kenylieou@gmail.com. Please feel free to contact me

lekhoa

  • PHP Starter
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #20 on: November 27, 2011, 02:47:06 AM »
co thay link dow dau

bangqua_thoigian

  • PHP Starter
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Upload ảnh và tạo thumbnail
« Reply #21 on: March 01, 2012, 11:02:04 AM »
sao tìm mãi chã thầy link down nhỉ.