반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- node
- linux
- ReactNative
- 센토스
- PYTHON
- vsCode
- IOS
- jest
- webpack
- MachineLearning
- xcode
- androidstudio
- 맥
- localserver
- 리눅스
- 네트워크
- Android
- MAC
- centos
- Chrome
- 티스토리챌린지
- VirtualBox
- build
- qunit
- react
- 오블완
- TensorFlow
- 개발
- unittest
Archives
- Today
- Total
로메오의 블로그
[jQuery - PHP] ajax로 form data 파일 업로드 (multipart) 본문
반응형
html
<form method="POST" enctype="multipart/form-data" id="uploadForm">
<input type="file" name="profile" />
<button id='btnUpload'>확인</button>
</form>
javascript
$('#btnUpload').on('click', function(event) {
event.preventDefault();
var form = $('#uploadForm')[0]
var data = new FormData(form);
data.append('attachment', $('[name="profile"]').files[0])
$('#btnUpload').prop('disabled', true);
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: "/upload",
data: data,
processData: false,
contentType: false,
cache: false,
timeout: 600000,
success: function (data) {
$('#btnUpload').prop('disabled', false);
alert('success')
},
error: function (e) {
$('#btnUpload').prop('disabled', false);
alert('fail');
}
});
})
upload.php
<?php
include 'SimpleImage.php';
$image = $_FILES['attachment'];
$image_prefix = 'path';
$thumnail = uploadImage($image, $image_prefix);
$value = array('filename'=>$thumnail);
echo json_encode($value);
/**
* upload image
*/
function uploadImage($image, $image_prefix) {
if (isset($image)) {
$types = split('/', $image['type']);
$image_extend = $types[1];
$image_size = floatval($image['size']);
$image_name = $image_prefix . '_' . time() . '.' . $image_extend;
}
if ($image_size > 3145728) {
//echo "3M를 초과할수 없습니다."
}
if (isset($image)) {
move_uploaded_file($image['tmp_name'], '../upload/' . $image_prefix . '/ori/' . $image_name);
// 사이즈 조절
$resize_image = new SimpleImage();
$resize_image->load('../upload/' . $image_prefix . '/ori/' . $image_name);
$resize_image->resizeToWidth(350);
$resize_image->save('../upload/' . $image_prefix . '/thum/' . $image_name);
}
return $image_name;
}
?>
SimpleImage.php
<?php
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>
input file 이미지 미리보기:
반응형
'Frontend > ETC' 카테고리의 다른 글
[VSCode] ftp 접속하기 ftp-kr (0) | 2020.06.17 |
---|---|
javascript UUID 생성하기 (0) | 2020.06.11 |
[postman] Capture requests로 Ajax 통신 정보 확인 (0) | 2020.05.26 |
[VS Code] vue.js 탭 사이즈 영구적으로 변경하기 (1) | 2020.04.24 |
[엑셀] 새로운 창으로 엑셀 파일 열기 - 엑셀 창 분리 (1) | 2020.03.11 |
Comments