PHP : การโชว์ thumbnail

ไฟล์ภาพขนาดเล็ก (Thumbnail) เก็บอยู่ที่ “./thumbnail/”
ส่วนภาพขนาดใหญ่เก็บอยู่ที่ “./uploads/”
ดังนั้นเวลาแสดงภาพให้นำภาพที่ “./thumbnail/” มาแสดง แต่เวลาสร้างลิงค์ไปที่ไฟล์รูปภาพขนาดใหญ่ให้ชี้ไปที่ “./uploads/”

[code lang=”php”]
<?
$imgPathUpload = "./uploads/";
$imgPathThumbnail = "./thumbnail/";
$dirHandle = opendir($imgPathThumbnail);
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && strpos($file, ‘.jpg’)>0 || strpos($file, ‘.gif’)>0 || strpos($file, ‘.png’)>0) {
if (strpos($file,"thumb_")===FALSE)
echo ("<a href=$imgPathUpload$file><img src=$imgPathThumbnail$file></a>");
}
}
closedir($dirHandle);
?>
[/code]

PHP: Upload และปรับเปลี่ยนขนาดรูปภาพ

!! ถ้า upload ไม่ผ่าน ให้เซ็ต permission เป็น 777
แต่ถ้ายัง upload ไม่ผ่านอีก อาจเป็นเพราะไม่สามารถเซ็ต permission จาก ftp client ได้
ให้ไปเซ็ต permission ที่ panel ให้เป็น 777

$_FILES[‘var’][‘tmp_name’] แสดงเท็มสำหรับการอัพโหลด
$_FILES[‘var’][‘name’] แสดงชื่อไฟล์
$_FILES[‘var’][‘size’] แสดงขนาดของไฟล์มีหน่วยเป็นไบต์
$_FILES[‘var’][‘type’] แสดงประเภทของไฟล์
$_FILES[‘var’][‘error’] แสดงรายละเอียดข้อผิดพลาด

ไฟล์ .html

[code lang=”html”]</pre>
<form action="upload_resize.php" enctype="multipart/form-data" method="post">
<h2>Form upload and resize image.</h2>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="fileupload" />

<input type="submit" value="submit" />
<input type="reset" value="Reset" />
</form>
<pre>

[/code]

ไฟล์ .php

[code lang=”php”]
<?php
function imageResize( $ext, $ori_file, $new_file )
{
$max_imageSize = 400;
$ori_size = getimagesize($ori_file);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];

if($ori_w > $ori_h) {
$new_w = $max_imageSize;
$new_h = round(($new_w/$ori_w) * $ori_h);
}
else
{
$new_h = $max_imageSize;
$new_w = round(($new_h/$ori_h) * $ori_w);
}

if ($ext == "jpg" or $ext == "jpeg") {
$ori_img = imagecreatefromjpeg($ori_file);
} else
if ($ext == "png") {
$ori_img = imagecreatefrompng($ori_file);
} else
if ($ext == "gif") {
$ori_img = imagecreatefromgif($ori_file);
}

$new_img = imagecreatetruecolor($new_w, $new_h);
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, $new_file);
} else
if ($ext == "png") {
imagepng($new_img, $new_file);
} else
if ($ext == "gif") {
imagegif($new_img, $new_file);
}

imagedestroy($ori_img);
imagedestroy($new_img);
}

$imagePath = "./uploads/";
$new_file = $imagePath . "test_resize.jpg";
//echo $new_file;
echo "<br>";

$fileupload = $_FILES[‘fileupload’][‘tmp_name’];
$fileupload_name = $_FILES[‘fileupload’][‘name’];
$fileupload_size = $_FILES[‘fileupload’][‘size’];
$fileupload_type = $_FILES[‘fileupload’][‘type’];

echo $fileupload;
echo "<br>";
echo $fileupload_name;
echo "<br>";

$ext = strtolower(end(explode(‘.’, $fileupload_name)));

if ($ext == "jpg" or $ext == "jpeg" or $ext == "png" or $ext=="gif") {
copy($fileupload, $imagePath . $fileupload_name);

$ori_file = $imagePath . $fileupload_name;
imageResize( $ext, $ori_file, $new_file );
unlink($fileupload);

echo "<H1>Upload and resize complete</h1>";
} else {
echo "<h1>Error</h1>";
}
?>
[/code]

ที่มา: thaicreate.com/

PHP: เปลี่ยนขนาดรูปภาพ JPEG

[code lang=”php”]
<?php
function imageResize( $ori_file, $new_file )
{
$max_imageSize = 100;
$ori_size = getimagesize($ori_file);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];

if($ori_w > $ori_h) {
$new_w = $max_imageSize;
$new_h = round(($new_w/$ori_w) * $ori_h);
}
else
{
$new_h = $max_imageSize;
$new_w = round(($new_h/$ori_h) * $ori_w);
}

$ori_img = imagecreatefromjpeg($ori_file);
$new_img = imagecreatetruecolor($new_w, $new_h);
imagecopyresized($new_img, $ori_img, 0, 0, 0, 0, $new_w, $new_h, $ori_w, $ori_h);

imagejpeg($new_img, $new_file);
imagedestroy($ori_img);
imagedestroy($new_img);
}

$imagePath = "./images/";
$ori_file = $imagePath . "test.jpg";
$new_file = $imagePath . "test_resize.jpg";

echo "<H1>Start resizing image</h1>";
imageResize( $ori_file, $new_file );

echo "<H1>Resize complete</h1>";
echo "<img src=’$ori_file’>";
echo "<img src=’$new_file’>";
?>
[/code]

Image RGB 1.0.0.0

โปรแกรม Image GRB เป็นโปรแกรมที่ช่วยในการเรียนรู้เรื่องการผสมสี จากแม่สีทั้ง 3 สีคือ สีแดง (R) สีเขียว (G) และสีน้ำเงิน (B)

RGB

โปรแกรมนี้เหมาะสำหรับภาพที่มีขนาดไม่เกิน 1500×1500 พิกเซล เนื่องจาก เมื่อไฟล์ภาพมีขนาดใหญ่ การประมวลผลสามารถทำได้ แต่จะใช้เวลาในการประมวลผลนานมาก

Download

Image RGB 1.0.0.0

Installation

ดับเบิ้ลคลิกที่ไฟล์ Image RGB จากนั้นกด Next

กด Next

กด Next

ถึงขั้นตอนนี้ ถ้าเป็น Windows 7 จะถาม ว่า “Do you want to allow the following program from an unknown publisher ……….” ให้ตอบว่า Yes

จากนั้นกด Next

กด Close เป็นอันเสร็จสิ้นการติดตั้ง

Document

ค่าสีของเมนูต่างๆ แสดงในตารางดังต่อไปนี้

Menu Submenu R G B
RGB
R+G+B (สีปกติ)
R
G
B
R (สีแดง)
R
0
0
G (สีเขียว)
0
G
0
B (สีน้ำเงิน)
0
0
B
R+G (สีเหลือง)
R
G
0
R+B (สีม่วง)
R
0
B
G+B (สีฟ้า)
0
G
B
0.299R+0.587G+0.114B (Gray)
0.299R
0.587G
0.114B
0.333R+0.333G+0.333B (Gray)
0.333R
0.333G
0.333B
RGB (x2)
R (R) B
R
R
B
R G (R)
R
G
R
(G) G B
G
G
B
R G (G)
R
G
G
(B) G B
B
G
B
R (B) B
R
B
B
R (R) (G)
R
R
G
R (B) (R)
R
B
R
(G) G (R)
G
G
R
(B) G (G)
B
G
G
(B) (R) B
B
R
B
(G) (B) B
G
B
B
RGB (x3)
R (R) (R)
R
R
R
(G) G (G)
G
G
G
(B) (B) B
B
B
B
Shift
R (B) (G)
R
B
G
(G) (B) (R)
G
B
R
(B) G (R)
B
G
R
(B) (R) (G)
B
R
G
(G) (R) B
G
R
B
Invert
Invert (-R -G -B)
255-R
255-G
255-B
-R G B
255-R
G
B
R -G B
R
255-G
B
R G -B
R
G
255-B
-R -G B
255-R
255-G
B
-R G -B
255-R
G
255-B
R -G -B
R
255-G
255-B
Invert (ค่าสีน้อยกว่า 128)
(0.299R+0.587G+0.114B) < 128
255-R
255-G
255-B
(0.299R+0.587G+0.114B) >= 128
R
G
B
Invert (ค่าสีมากกว่า 128)
(0.299R+0.587G+0.114B) > 128
255-R
255-G
255-B
(0.299R+0.587G+0.114B) <= 128
R
G
B

ศึกษาเพิ่มเติม: RGB color modelChannel (digital image)

Image Icon 1.0.0.0

โปรแกรม Image Icon ใช้แปลงไฟล์รูป ให้เป็นไฟล์ไอคอน โดยสามารถเลือกขนาดของไอคอนได้ หลายขนาด

โปรแกรมนี้เหมาะสำหรับภาพที่มีขนาดไม่เกิน 1500×1500 พิกเซล เนื่องจาก เมื่อไฟล์ภาพมีขนาดใหญ่ การประมวลผลสามารถทำได้ แต่จะใช้เวลาในการประมวลผลนานมาก

Download

Image Icon 1.0.0.0

Installation

ดับเบิ้ลคลิกที่ไฟล์ Image Icon จากนั้นกด Next

กด Next

กด Next

ถึงขั้นตอนนี้ ถ้าเป็น Windows 7 จะถาม ว่า “Do you want to allow the following program from an unknown publisher ……….” ให้ตอบว่า Yes

จากนั้นกด Next

กด Close เป็นอันเสร็จสิ้นการติดตั้ง