Smart PHP Thumbnailer
Dulu sempat bikin code untuk update function thumbnailer supaya hasilnya bisa square (bujur sangkar) dan proporsional di posisi tengah gambar (tidak gepeng secara horizontal/vertical walaupun bentuk image asli tidak square).
Karena kebutuhan lain, akhirnya saya update lagi fungsi tersebut supaya bisa lebih pintar. Intinya thumbnailer akan mengecek rasio image source dan membandingkannya dengan rasio image destination, memutuskan rasio terbaik untuk membuat thumbnail, dan melakukan crop pada image untuk membentuk thumbnail sesuai dengan ukuran destination yang diinginkan. Berikut keunggulan fungsi thumbnailer ini:
- Free style, ukuran thumbnail bebas dan tidak harus sesuai dengan rasio image
- Adjust to Width, ukuran thumbnail bisa disesuaikan dengan rasio image cukup dengan menentukan lebar dari thumbnail
- Adjust to Height, penyesuaian dengan rasio image cukup dengan menentukan tinggi dari thumbnail
- Caching, thumbnail hanya di generate 1 kali, dan setiap permintaan thumbnail akan tetap menggunakan file yang sama, kecuali bila image source lebih baru dari file cache.
- Preview Image, selain berguna untuk membuat thumbnail, fungsi ini juga bisa menampilkan image source.
- Various Image Format, bisa mengenali image dari beragam format (diuji dengan format jpg, gif dan png), tapi tidak mengenal transparansi karena thumbnail yang dihasilkan dalam format jpg.
<?php define('IMAGES_DIR', 'images/'); define('IMAGES_CACHE_DIR', IMAGES_DIR.'cache/'); define('IMAGES_CACHE_QUALITY', '85'); function draw_image($path, $filename, $alt, $width='', $height='', $params='') { $src = $path.$filename; if(file_exists($src)) { $info=getimagesize($src); $w = $info[0]; $h = $info[1]; if($width=='' && $height=='') { $width = $w; $height = $h; } elseif($width=='' && $height==$h) { $width = $w; } elseif($width==$w && $height=='') { $height = $h; } elseif($width!=$w || $height!=$h) { //CONTROL RATIO IF ONLY ONE VALUE IS USED if($width=='') $width = $w/$h*$height; if($height=='') $height = $h/$w*$width; $cachefile = IMAGES_CACHE_DIR.$width.'x'.$height.'_'.$filename; $createcachefile = true; if(file_exists($cachefile)) { if(filemtime($cachefile)>filemtime($src)) { $createcachefile = false; } } if($createcachefile===true) { $wratio = $w/$width; $hratio = $h/$height; if($wratio<$hratio) { $wtoget = $w; $htoget = $height/$width*$w; $hdiff = $h-$htoget; $xpos = 0; $ypos = $hdiff/2; } else { $wtoget = $width/$height*$h; $htoget = $h; $wdiff = $w-$wtoget; $xpos = $wdiff/2; $ypos = 0; } $imagesource = file_get_contents($src); $image = @imagecreatefromstring($imagesource); $image_new = imagecreatetruecolor($width, $height); imagecopyresampled($image_new, $image, 0, 0, $xpos, $ypos, $width, $height, $wtoget, $htoget); imagejpeg($image_new, $cachefile, IMAGES_CACHE_QUALITY); } $src = $cachefile; } } else { $src = ''; $width = ''; $height = ''; } if($width!='') $width = "width='$width'"; if($height!='') $height = "height='$height'"; $image = "<image src='$src' alt='$alt' $width $height $params />"; return(str_replace("'", '"', $image)); } echo draw_image(IMAGES_DIR, 'foto-1.jpg', 'Foto 1', 100, 100); echo '<br /><br />'; echo draw_image(IMAGES_DIR, 'foto-1.jpg', 'Foto 1', 100); echo '<br /><br />'; echo draw_image(IMAGES_DIR, 'foto-1.jpg', 'Foto 1', '', 100); echo '<br /><br />'; echo draw_image(IMAGES_DIR, 'foto-1.jpg', 'Foto 1'); ?>
- Buat file php baru
- kopi seluruh code di atas pada file tersebut
- Pada lokasi yang sama, buatlah folder ‘images/’ dan ‘images/cache/’
- Kopi sebuah file image ke folder images dan rename menjadi ‘foto-1.jpg’
- Jalankan file php tersebut, selamat mencoba (^_^)
Tags: PHP

June 10th, 2008 at 9:49 am
Pak Sahat,…
Ini script untuk munculin kodok nya yach?
hahahahaha …. (^^)v Peace pak.
Makin lama, nih blog, makin technical. =D
June 11th, 2008 at 4:54 pm
Wallah walah… om daphit pakabarnya?
Tumben mampir (^_^) ga lagi stres menjelang hari H kan?
hehehe…
July 28th, 2008 at 4:26 pm
wah.. makasih ya bang Sahat, jadi tambah ilmu nih, he..he..
jangan kapok2 ya…
August 1st, 2008 at 10:12 am
sama-sama om ashto (^_^) kalau ada ide jurus baru kabar2in aja