Eclipse
11-10-2008, 03:01 PM
Silahkan copas script di bawah ini...dan jalankan di localhost kamu :malu
<?php
function createthumb($name, $filename, $new_w, $new_h, &$ori_width, &$ori_height, &$thumb_width, &$thumb_height) {
$system = explode(".", $name); // prepare the source file name
if (preg_match("/jpg|jpeg/", $system[1])) { // image is jpg
$src_img = imagecreatefromjpeg($name);
}
if (preg_match("/png/",$system[1])) { // image is png
$src_img = imagecreatefrompng($name);
}
if (preg_match("/gif/", $system[1])) { // image is gif
$src_img = imagecreatefromgif($name);
}
$old_x = imagesx($src_img); // define width
$old_y = imagesy($src_img); // define height
if ($old_x > $old_y) {
$thumb_w = $new_w; // define new width
$thumb_h = (int)floor($old_y * ($new_h / $old_x)); // define new height
}
if ($old_x < $old_y) {
$thumb_w = (int)floor($old_x * ($new_w / $old_y)); // define new width
$thumb_h = $new_h; // define new height
}
if ($old_x == $old_y) {
$thumb_w = $new_w; // define new width
$thumb_h = $new_h; // define new height
}
$ori_width = $old_x; // set original width
$ori_height = $old_y; // set original height
$thumb_width = $thumb_w; // set thumb width
$thumb_height = $thumb_h; // set thumb height
$dst_img = imagecreatetruecolor($thumb_w,$thumb_h); // create new true color image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); // copy and resize image
if (preg_match("/png/", $system[1])) {
imagepng($dst_img, $filename); // output png image to file
} elseif (preg_match("/jpg|jpeg/", $system[1])) {
imagejpeg($dst_img, $filename); // output jpg image to file
} elseif (preg_match("/gif/", $system[1])) {
imagegif($dst_img, $filename); // output gif file to file
}
imagedestroy($dst_img); // destroy source image handle
imagedestroy($src_img); // destroy destination image handle
}
?>
Fungsi di atas memiliki beberapa parameter.
createthumb($name, $filename, $new_w, $new_h, &$ori_width, &$ori_height, &$thumb_width, &$thumb_height)
Parameter:
$name, adalah nama file yang hendak dibuat thumbnail-nya
$filename, adalah nama file target/output dari thumbnail nantinya
$new_w, dimensi width maksimal thumbnail
$new_h, dimensi height maksimal thumbnail
$ori_width, untuk menampung original image width
$ori_height, untuk menampung original image height
$thumb_width, untuk menampung thumbnail image width
$thumb_height, untuk menampung thumbnail image height
Siapkan suatu gambar, misalnya gambar_asli.gif yang dimensinya 320×240.
Kl teman ingin membuat thumbnail untuk gambar tersebut dengan maksimal width 128 dan maksimal height 128 juga. Dan hasil thumbnailnya ingin disimpan pada file gambar_thumbnail.gif, maka contoh penggunaan fungsinya adalah seperti berikut ini.
<?php
$original_width = 0;
$original_height = 0;
$thumbnail_width = 0;
$thumbnail_height = 0;
createthumb(getcwd() . "/gambar_asli.gif", getcwd() . "/gambar_thumbnail.gif", 128, 128, $original_width, $original_height, $thumbnail_width, $thumbnail_height);
?>
Ukurannya thumbnailnya 128×96 (resize secara proposional)
<?php
function createthumb($name, $filename, $new_w, $new_h, &$ori_width, &$ori_height, &$thumb_width, &$thumb_height) {
$system = explode(".", $name); // prepare the source file name
if (preg_match("/jpg|jpeg/", $system[1])) { // image is jpg
$src_img = imagecreatefromjpeg($name);
}
if (preg_match("/png/",$system[1])) { // image is png
$src_img = imagecreatefrompng($name);
}
if (preg_match("/gif/", $system[1])) { // image is gif
$src_img = imagecreatefromgif($name);
}
$old_x = imagesx($src_img); // define width
$old_y = imagesy($src_img); // define height
if ($old_x > $old_y) {
$thumb_w = $new_w; // define new width
$thumb_h = (int)floor($old_y * ($new_h / $old_x)); // define new height
}
if ($old_x < $old_y) {
$thumb_w = (int)floor($old_x * ($new_w / $old_y)); // define new width
$thumb_h = $new_h; // define new height
}
if ($old_x == $old_y) {
$thumb_w = $new_w; // define new width
$thumb_h = $new_h; // define new height
}
$ori_width = $old_x; // set original width
$ori_height = $old_y; // set original height
$thumb_width = $thumb_w; // set thumb width
$thumb_height = $thumb_h; // set thumb height
$dst_img = imagecreatetruecolor($thumb_w,$thumb_h); // create new true color image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); // copy and resize image
if (preg_match("/png/", $system[1])) {
imagepng($dst_img, $filename); // output png image to file
} elseif (preg_match("/jpg|jpeg/", $system[1])) {
imagejpeg($dst_img, $filename); // output jpg image to file
} elseif (preg_match("/gif/", $system[1])) {
imagegif($dst_img, $filename); // output gif file to file
}
imagedestroy($dst_img); // destroy source image handle
imagedestroy($src_img); // destroy destination image handle
}
?>
Fungsi di atas memiliki beberapa parameter.
createthumb($name, $filename, $new_w, $new_h, &$ori_width, &$ori_height, &$thumb_width, &$thumb_height)
Parameter:
$name, adalah nama file yang hendak dibuat thumbnail-nya
$filename, adalah nama file target/output dari thumbnail nantinya
$new_w, dimensi width maksimal thumbnail
$new_h, dimensi height maksimal thumbnail
$ori_width, untuk menampung original image width
$ori_height, untuk menampung original image height
$thumb_width, untuk menampung thumbnail image width
$thumb_height, untuk menampung thumbnail image height
Siapkan suatu gambar, misalnya gambar_asli.gif yang dimensinya 320×240.
Kl teman ingin membuat thumbnail untuk gambar tersebut dengan maksimal width 128 dan maksimal height 128 juga. Dan hasil thumbnailnya ingin disimpan pada file gambar_thumbnail.gif, maka contoh penggunaan fungsinya adalah seperti berikut ini.
<?php
$original_width = 0;
$original_height = 0;
$thumbnail_width = 0;
$thumbnail_height = 0;
createthumb(getcwd() . "/gambar_asli.gif", getcwd() . "/gambar_thumbnail.gif", 128, 128, $original_width, $original_height, $thumbnail_width, $thumbnail_height);
?>
Ukurannya thumbnailnya 128×96 (resize secara proposional)