|
 |
|
ขอแนะนำเรื่องการใช้ function resize image ก่อน upload image ครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
if (isset($_FILES['upload']['name']) && isset($_FILES['upload']['tmp_name'])){
$tmp_name = $_FILES['upload']['tmp_name'];
$name = $_FILES['upload']['name'];
if (!empty($name) && !empty($tmp_name)) {
resize_image($tmp_name, 100, 100);
$path = 'gallery/';
if (move_uploaded_file(resize_image($tmp_name, 200, 200, $crop=TRUE), $path.$name)) {
echo 'ok.';
}
} else {
echo 'empty image.';
}
}
?>
ขอทราบวิธีการใช้ function resize_image ก่อนอัปโหลดไฟล์รูปครับ
ว่าจะใช้ตอนไหน ใช้อย่างไรครับ
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2016-10-08 16:52:08 |
By :
natwipool |
View :
1441 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันต้องอัพโหลดก่อน Resize ครับ 
|
 |
 |
 |
 |
Date :
2016-10-11 11:06:03 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณดูการทำงานนะ
form -> ผู้ใช้เลือกไฟล์ -> ผู้ใช้กด submit -> ข้อมูลในฟอร์มพร้อมทั้งไฟล์ที่เลือกถูกส่งไปยัง server หรือเรียกว่า upload -> web server รับการ submit ของฟอร์ม -> ถ้ามี php ติดตั้งอยู่มันจะสั่ง php ให้ทำงานทันทีเกี่ยวกับการ request นี้จาก client รวมทั้งการ upload ด้วย แต่ php ที่ว่านี้คือตัว .exe ไม่ใช่ .php ที่คุณเขียน -> php ที่เป็น .exe ถูกเรียกให้ทำงานเกี่ยวกับ request นี้เมื่อมีไฟล์อัพโหลดเข้ามาก็ย้ายไปไว้ใน temp folder -> การรับ request ต่างๆเสร็จเรียกรันไฟล์ .php ที่ form action นั้นๆเรียกมา เช่น <form action="form-upload.php"> -> ถึงคราวที่ .php ของคุณทำงานแล้ว.
จากขั้นตอนดังกล่าว คุณจะให้มันย่อภาพก่อนอัพโหลดได้ยังไงครับ?
หรือจะใช้ javascript ย่อเอา?
|
 |
 |
 |
 |
Date :
2016-10-11 22:49:38 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|