|
 |
|
ขอรบกวนช่วยผมด้วยครับ ต้องการลดขนาดรูปภาพด้วย php แล้วให้คงคุณภาพความชัดไว้เช่นเดิม |
|
 |
|
|
 |
 |
|
ภาพที่ถ่ายจากกล้องก่อนทำการ Resize

หลังจากการทำ Resize ด้วยโปรแกรม และ ภาษา php

รายละเอียด ของรูปภาพ

จะสังเกตุได้ว่า ภาพทางซ้าย ผม resize ด้วยโปรแกรม Photo scape ส่วนทางภาพ ขวาผม resize ด้วย ภาษา php
ทำไม ภาพที่ resize ด้วย ภาษา php ถึงไม่ชัด จะมีวิธีแก้ไหมครับ ถ้าผมต้องการ ให้ลดขนาดแต่คงคุณภาพ ไว้เช่นเดิมอ่ะครับ
ขอบคุณครับ
นี่คือในส่วนของโค้ดครับ
Code (PHP)
<meta charset="utf-8">
<?
ini_set('memory_limit', '120M'); //ปรับขนาดที่ต้องการจนกว่ามันจะไม่ error
ini_set('max_execution_time', '60'); //อันนี้แถมเผื่อเวลาโปรเซสไม่พอ
$folderpic = "images/DD" ; //ชื่อโฟลเดอร์ที่ไว้ใช้เก็บไฟล์
$max_size = 500; // จำกัดขนาดไฟล์ ค่าเป็น kb
$width=800; //ขนาดความกว้างที่ต้องการ Resize
$height=533; // ขนาดความสูงที่ต้องการ Resize
echo $file_size=$_FILES['filUpload']['size']; // ตัวแปรขนาดของไฟล์
$limit_size=1000*$max_size;
if($file_size >= $limit_size){
echo "<script language='javascript'>alert('ไฟล์ของคุณใหญ่เกินไปครับต้องมีขนาดไม่เกิน $max_size kb');</script>";
} else {
if($_FILES["filUpload"]["tmp_name"]){
$fileUpload=$_FILES["filUpload"]["tmp_name"]; // ตัวแปร temp
$fileUpload_name=$_FILES["filUpload"]["name"]; // ตัวแปรชื่อไฟล์
if (isset($_FILES["filUpload"]["name"])) { // ถ้ามีการเซทชื่อไฟล์มาให้ประมวลผลส่วนนี้
//*** ส่วนนี้คือการ Rename รูปให้เป็นวันที่ที่บันทึก ***//
$ext = strtolower(end(explode('.', $fileUpload_name)));
$dateUpFile=date("YmdHis");
$newname=$dateUpFile.".".$ext; //ชื่อไฟล์ที่กำหนดขึ้นมาใหม่ โดยใช้วันและเวลาเป็นตัวกำหนดตั้งชื่อ
//*** สร้างภาพขึ้นมาใหม่ตามนามสกุลของไฟล์เดิม ***//
$images = $fileUpload;
$size = getimagesize($images);
$img_type=strtolower(end(explode('.',$images)));
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images); // GIF
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images); // JPEG
}
else if($size[2] == 3) {
$images_orig = imagecreatefrompng($images); // png
}
//*** ส่วนนี้คือการ Resize รูป ***//
$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); // ทำการ Resize ภาพ
imagegif($images_fin, $folderpic. "/" . $newname);
imagedestroy($images_orig);
imagedestroy($images_fin);
}
// ส่วนการ insert ชื่อไฟล์ลง db
$img = mysql_query("INSERT INTO images VALUES ('id', '$newname')") or die(mysql_error());
if ($img) {
echo "<center><font size=4 color=green>อัพโหลดรูปเรียบร้อยแล้วครับ...</font></center>";
echo "<br>";
}
}
}
?>
Tag : PHP, MySQL, HTML/CSS, JavaScript, jQuery, Windows

|
|
 |
 |
 |
 |
Date :
2014-06-18 10:49:54 |
By :
bamossza |
View :
2029 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); // ทำการ Resize ภาพ
เปลี่ยนเป็น imagecopyresize();
resampled ชื่อมันก็บอกไว้แล้วครับเป็น ตัวอย่าง เอาความละเอียดเท่าเดิมต้องใช้ตัวที่บอกครับ
|
 |
 |
 |
 |
Date :
2014-06-18 18:03:23 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : Chaidhanan เมื่อวันที่ 2014-06-18 18:03:23
รายละเอียดของการตอบ ::
แก้ยังไงนะคับบบ พอดี ไม่เคยทำเลยคับ ขอบคุณครับ
//*** ส่วนนี้คือการ Resize รูป ***//
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imagecreatetruecolor($width, $height);
//setResolution($photoX,$photoY);
//imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); // ทำการ Resize ภาพ
//imagegif($images_fin,"$folderpic/".$newname);
imagecopyresize($images_fin, $images_orig);
imagegif($images_fin, $folderpic. "/" . $newname);
imagedestroy($images_orig);
imagedestroy($images_fin);
|
 |
 |
 |
 |
Date :
2014-06-18 22:40:36 |
By :
bamossza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ imagecopyresampled() น่ะถูกต้องแล้วครับ ถ้าใช้ imagecopyresized() ภาพจะแตกและไม่สวยเมื่อถูกย่อหรือขยายครับ
ซึ่งจริงๆ แล้วปัญหาอยู่ที่บรรทัดนี้ครับ
บรรทัดที่ 51
imagegif($images_fin, $folderpic. "/" . $newname);
คุณใช้ฟังก์ชั่น imagegif() มันจึง export ออกไปเป็นไฟล์ gif ซึ่งมีสีสูงสุดได้แค่ 256 สี (8 bit)
ให้เปลี่ยนเป็น imagejpeg() หรือ imagepng() ครับ (แล้วแต่ว่าจะให้เป็นไฟล์ jpg หรือ png)
Code (PHP)
imagejpeg($images_fin, $folderpic. "/" . $newname);
imagejpeg($images_fin, $folderpic. "/" . $newname, 90); // หรือเพิ่มตัวเลขที่ระบุคุณภาพของภาพด้วย ถ้าไม่เพิ่ม default คือ 75
|
 |
 |
 |
 |
Date :
2014-06-19 01:59:12 |
By :
phpinfo() |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|