Thumbnail image JPEG seperti flickr dengan PHP

Thursday, January 17, 2008 10:08
Posted in category PHP

Artikel ini akan menjelaskan cara pembuatan thumbnail image yang proposional seperti

situs flickr. Thumbnail didapat dari hasil resize gambar yang sebenarnya, kemudian

mengambil bentuk tengahnya, dan diambil kembali sekian persen. Langkah-langkahnya

sebagai berikut :

Catatan * : Hanya untuk format gambar (JPG,JPEG)

1. Pastikan pustaka GD sudah diaktifkan agar PHP dapat mendukung

pembuatan dan resize grafis secara on the fly.
Caranya:
- buka file configurasi php(php.ini) dengan text editor.
- Cari ;extension=php_gd.dll
- Hilangkan tanda ; sehingga menjadi extension=php_gd.dll

2. Buat folder “img” pada direktori htdocs(untuk web server Apache) atau

wwwroot(untuk web server IIS)

3. Buat file php seperti di bawah ini dan simpan dengan nama

“create_thumb.php” di direktori yg sama dengan folder “img”:

<?php

$vdir_upload = ‘img/’;
$vfile_upload = $vdir_upload . $_FILES['img']['name'];
if(!move_uploaded_file($_FILES['img']['tmp_name'], $vfile_upload))
exit(“Upload Error”);
else
createThumbnail($vfile_upload);

function createThumbnail($file_name){
header(“Content-type: image/jpeg”);

//identitas file asli
$im_src = imagecreatefromjpeg($file_name);
$src_width = imageSX($im_src);
$src_height = imageSY($im_src);

//set ukuran gambar hasil perubahan
$dst_width = 100;
$dst_height = $dst_width;
//set persentase gambar yg ingin diambil
$prsn = 60;

//proses perubahan ukuran
$im = imagecreatetruecolor($dst_width,$dst_height);
if($src_width > $src_height)
imagecopyresampled($im,$im_src,0,0,round((($src_width-$src_height)/2)+

((((100-$prsn)/2)/100)*$src_height)),round((((100-$prsn)/2)/100)*$src_height),

$dst_width, $dst_height, ($prsn/100)*$src_height, ($prsn/100)*$src_height);
else
imagecopyresampled($im,$im_src,0,0,round((($src_height-$src_width)/2)+

((((100-$prsn)/2)/100)*$src_width)),round((((100-$prsn)/2)/100)*$src_width),

$dst_width, $dst_height, ($prsn/100)*$src_width, ($prsn/100)*$src_width);

//Simpan gambar
imagejpeg($im,$file_name . “_thumb.jpg”);

imagedestroy($im_src);
imagedestroy($im);
}
?>

4. Buat file html seperti di bawah ini dan simpan dengan nama

“upload_img.html” di direktori yg sama dengan folder “img”:

<html>
<head>
<title>Thumbnail</title>
</head>

<body>
<form enctype=”multipart/form-data” action=”create_thumb.php” method=”post”>
Upload gambar :<input name=”img” type=”file”>
<input type=”submit” value=”Ok”>
</form>
</body>
</html>
5. Coba upload sebuah gambar berformat JPEG dengan perintah tersebut.

Lihat pada direktori “img”, jika berhasil maka akan terbentuk 2 buah file

upload(1 gambar sebenarnya, 1 thumbnail).

You can skip to the end and leave a response. Pinging is currently not allowed.

3 Responses to “Thumbnail image JPEG seperti flickr dengan PHP”

  1. 007 says:

    April 28th, 2008 at 9:20 am

    no comment

  2. bima says:

    February 10th, 2009 at 4:25 am

    makasih ya….. skripnya membantu sekali…..

    salam kenal….

  3. bimo says:

    March 13th, 2009 at 6:32 pm

    Thanks , it helped me a lot . .
    Just , it would be nice if there’re some improvitation on the programming article . .

    =’)

Leave a Reply

Comment spam protected by SpamBam