Tạo Thumbnails

Một phần của tài liệu THAO TÁC VÀ TẠO HÌNH ẢNH VỚI PHP (Trang 32 - 38)

Tất nhiên, việc cho thấy những hình ảnh của người dùng thì tốt. Tuy nhiên, nó không tốt cho cuộc triển lãm hay một danh sách hình trong một trang. Bạn cần làm sao để tạo ra thumbnail tự động của mỗi file upload mà bạn sử dụng cho mục đích là một phòng triển lãm tranh.

Ví dụ về việc tạo Thumbnails

Bạn muốn tạo tự động một phiên bản thumbnail tất cả các hình mà người dùng upload, bạn cần sửa đổi check_image.php và bao gồm hàm:

1. Tạo một danh mục hình ảnh con cho hình ảnh. Ví dụ, bạn tạo c:\Program Files\Apache Group\Apache2\images\thumbs, chắc rằng thư mục được cho phép.

2. Thay đổi check_image.php như sau: <?php

//Kết nối cở sở dữ liệu

$link = mysql_connect(“localhost”, “bp5am”, “bp5ampass”) or die(“Could not connect: “ . mysql_error());

mysql_select_db(“moviesite”, $link) or die (mysql_error()); //Tạo những biến có sẵn $image_caption = $_POST[‘image_caption’]; $image_username = $_POST[‘image_username’]; $image_tempname = $_FILES[‘image_filename’][‘name’]; $today = date(“Y-m-d”);

//đưa hình ảnh lên và kiểm tra loại hình ảnh

$ImageDir = “c:/Program Files/Apache Group/Apache2/test/images/”; //**Chèn

$ImageThumb = $ImageDir . “thumbs/”; //**Kết thúc chèn

$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES[‘image_filename’][‘tmp_name’], $ImageName))

{

//get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($ImageName);

if ($type > 3) {

echo “Sorry, but the file you uploaded was not a GIF, JPG, or “ .

echo “Please hit your browser’s ‘back’ button and try again.”; } else

{

//image is acceptable; ok to proceed //Chèn thông tin vào bảng hình ảnh $insert = “INSERT INTO images

(image_caption, image_username, image_date) VALUES

(‘$image_caption’, ‘$image_username’, ‘$today’)”; $insertresults = mysql_query($insert)

or die(mysql_error());

$lastpicid = mysql_insert_id();

$newfilename = $ImageDir . $lastpicid . “.jpg”; if ($type == 2) { rename($ImageName, $newfilename); } else { if ($type == 1) { $image_old = imagecreatefromgif($ImageName); } elseif ($type == 3) { $image_old = imagecreatefrompng($ImageName); //”convert” the image to jpg

$image_jpg = imagecreatetruecolor($width, $height); imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height);

imagejpeg($image_jpg, $newfilename); imagedestroy($image_old);

imagedestroy($image_jpg); }

//**Bắt đầu chèn

$newthumbname = $ImageThumb . $lastpicid . “.jpg”; //thiết lập chiều cho thumbnail

$thumb_width = $width * 0.10; $thumb_height = $height * 0.10; //Tạo thumbnail $largeimage = imagecreatefromjpeg($newfilename); $thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); imagejpeg($thumb, $newthumbname); (adsbygoogle = window.adsbygoogle || []).push({});

imagedestroy($largeimage); imagedestroy($thumb); //**Kết thúc chèn

$url = “location: showimage.php?id=” . $lastpicid; header ($url);

} }

?>

3. Bây giờ chúng ta sẽ tạo gallery.php như phòng triển lãm của bạn, nhập đoạn mã sau:

<?php

//Kết nối cơ sở dữ liệu

$link = mysql_connect(“localhost”, “root”, “”) or die(“Could not connect: “ . msql_error()); mysql_select_db(“moviesite”, $link)

$ImageDir = “images”;

$ImageThumb = $ImageDir . “/thumbs/”; ?>

<html> <head>

<title>Welcome to our Photo Gallery</title> </head>

<body>

<p align=”center”>Click on any image to see it full sized.</p> <table align=”center”> <tr> <td align=”center”>Image</td> <td align=”center”>Caption</td> <td align=”center”>Uploaded By</td> <td align=”center”>Date Uploaded</td> </tr> <?php

//get the thumbs

$getpic = mysql_query(“SELECT * FROM images”) or die(mysql_error());

while ($rows = mysql_fetch_array($getpic)) { extract($rows);

echo “<tr>\n”;

echo “<td><a href=\””.$ImageDir . $image_id . “.jpg\”>”;

echo “<img src=\”” . $ImageThumb . $image_id . “.jpg\” border=\”0\”>”;

echo “</a></td>\n”;

echo “<td>” . $image_caption . “</td>\n”; echo “<td>” . $image_username . “</td>\n”; echo “<td>” . $image_date . “</td>\n”;

echo “</tr>\n”; } ?> </table> </body> </html>

4. Bây giờ bạn đưa một vài hình sử dụng cho trang upload_image.php. Bạn sẽ thấy như hình 7-8, bạn có thể thêm âm thanh tùy ý.

Hình 7.8 Cách thức hoạt động:

Thực sự tự thumbnails tạo ra trong trang check_image.php, bạn thêm những đoạn sau đây để hoàn thành

//**Chèn từ dòng này

$newthumbname = $ImageThumb . $lastpicid . “.jpg”; //Taọ kích thước cho thumbnails

$thumb_width = $width * 0.10; $thumb_height = $height * 0.10; //Tạo thumbnail

$largeimage = imagecreatefromjpeg($newfilename); (adsbygoogle = window.adsbygoogle || []).push({});

$thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0,

$thumb_width, $thumb_height, $width, $height); imagejpeg($thumb, $newthumbname);

imagedestroy($largeimage); imagedestroy($thumb); //**Kết thúc dòng chèn

Bạn tạo một thumbnail quá trình gồm 5 bước như trước:

1. Tạo một GD thân thiện từ hình nguồn

2. Tạo một GD thân thiện là một hình trống, với kích thước nhỏ

3. Sao chép hình nguồn vào hình trống

4. Cất giữ hình mới đó vào một thư mục thích hợp

Một phần của tài liệu THAO TÁC VÀ TẠO HÌNH ẢNH VỚI PHP (Trang 32 - 38)