How to Resizing Image with PHPDOWNLOAD SOURCE CODE PHP NA DISINI (FREE)Resizing Image with PHP
ke tutorial script upload image avatar sebelumna, ni rada mirip, cuma kali ini kita belajar bagaimana membuat script untuk me-resize ulang dimensi gambar yang telah di upload, untuk kemudian di create ulang ke lokasi folder yang berbeda.
woke masih sama, untuk menstandarkan dan membiasakan diri dalam merancang script berbasis CMS (dinamic proses), maka gw masih menggunakan penjabaran pada file2 untuk config dan function.
config.php<?
## © Copyright 2008. PHP Tutorial Forum www.dremi.info ##
## http://www.dremi.info/forum/viewtopic.php?p=814#814 ##
## PHP Builder by hairul azami [dr.emi] ##
##=======================LICENSE=======================##
## FREE ##
##=====================================================##
//main directory
define ("_UPLOAD_PATH", "images/");
define ("_RESIZE_PATH", "thumbs/");
define ("_PERCENTSIZE", 0.5); // image asli akan di resize 50% (50/100 = 0.5) dan di create ulang ke folder thumbs
?>
karena resize ukuran gambar na akan di ubah ke setengah bagian dari ukuran semula, maka gw make persen untuk pengalina (didefinisikan sebagai _PERCENTSIZE.
functions.php<?
function getlast($toget)
{
$pos=strrpos($toget,".");
$lastext=substr($toget,$pos+1);
return $lastext;
}
function viewDIR($dir, $rez)
{
//begin dir
if (is_dir($rez))
{
if ($dh = opendir($rez))
{
while (($file = readdir($dh)) !== false)
{
if($file=="Thumbs.db" || $file==".." || $file==".")
{
echo"";
}
else
{
list($width, $height) = getimagesize($rez.$file);
echo "<strong>[ + ]</strong> DIMENSION AFTER RESIZE: ( $width X $height PIXELS )<BR><BR>";
echo "<a href='".$dir.$file."' target='blank'>
<img src='".$rez.$file."' border=0></a><BR><BR>";
}
}
closedir($dh);
}
}
//ending dir
}
//alert
function warning($msg)
{
echo "<SCRIPT>alert(\"WARNING: $msg\");history.go(-1)</SCRIPT>";
exit;
}
//redirect
function redirect($delay,$goto,$msg)
{
echo"<center><h2>$msg</h2><br>Redirect progress..<br>Please Stand By.. <meta http-equiv=\"refresh\" content=\"$delay;URL=$goto\" /><br><br><img src=\"ajax-loader.gif\"></center>";
}
?>
fungsi bwatan viewDIR nantinya akan digunakan dalam memanggil conten direktori untuk ditampilkan dalam bentuk real data, sesuai dalam folder na.
element yang digunakan adlah: viewDIR($dir, $rez)
dimana $dir, merupakan variable inputan untuk direktori image, sebagai target upload image, sedangkan $rez adalah folder kedua, target thumbnails image.
nah ni bwat
index.php na
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Resizing Image With PHP</title>
<style type="text/css">
<!--
body,td,th {
font-family: Trebuchet MS;
font-size: 11px;
color: #333333;
}
body {
background-color: #FFFFFF;
margin-left: 20px;
margin-top: 20px;
margin-right: 0px;
margin-bottom: 0px;
}
a:link {
color: #0099FF;
text-decoration: underline;
}
a:visited {
text-decoration: underline;
color: #0099FF;
}
a:hover {
text-decoration: none;
color: #009900;
}
a:active {
text-decoration: underline;
color: #0099FF;
}
-->
</style></head>
<body>
<?
include_once "config.php";
include_once "functions.php";
if($_POST['submit'])
{
//cek extensi gambar: jpg
$chkFileExt = getlast($HTTP_POST_FILES['ufile']['name'][0]);
if($chkFileExt == "jpg")
{
//directory target
$dir = _UPLOAD_PATH.$HTTP_POST_FILES['ufile']['name'][0];
$rez = _RESIZE_PATH.$HTTP_POST_FILES['ufile']['name'][0];
@copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $dir);
//get size list for original image
list($width, $height) = getimagesize($dir);
$new_width = $width * _PERCENTSIZE;
$new_height = $height * _PERCENTSIZE;
$im=imagecreatefromjpeg($dir);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($new_width,$new_height);
### kalo pake ini hasil kurang smooth
//imagecopyresized($newimage,$im,0,0,0,0,$new_width,$new_height,$width,$height);
//ImageJpeg($newimage,$tsrc,80);
### hasil na lebih smooth
imagecopyresampled($newimage,$im,0,0,0,0,$new_width,$new_height,$width,$height);
if(imagejpeg($newimage,$rez,80))
{
redirect(2, "index.php", "RESIZING IMAGE SUCCESS");
}
else
{
warning("RESIZING IMAGE ERROR, $phperrormsg");
}
}
else
{
warning("File Extention *.$chkFileExt was Invalid");
}
}
else
{
?>
<form style="width:45%; margin:auto;" id="form1" name="form1" method="post" action="" enctype="multipart/form-data">
<fieldset style="padding:0 20px 20px 20px;" ><legend style="font-size:16px; color:#0066CC; font-weight:bold;">Resizing Image With PHP</legend>
<br />
<strong>Image Upload:</strong><br />
<input name="ufile[]" type="file" style="background:#EFEFEF; border:solid 1px #c0c0c0; font-size:14px; color:#666666;" size="50" maxlength="300" />
(*.gif, *.png, *.jpg)<br />
<br />
<br />
<input name="submit" style="padding: 10px; cursor:hand; background:#333333; border:solid 1px #FFFFFF; font-size:12px; font-weight:bold; color:#FFFFFF;" type="submit" value="SUBMIT" />
<input name="reset" style="padding: 10px; cursor:hand; background:#333333; border:solid 1px #FFFFFF; font-size:12px; font-weight:bold; color:#FFFFFF;" type="reset" value="RESET" />
</fieldset><br />
© Copyright 2008 <a title="tutorial photoshop" href="http://www.dremi.info" target="_blank">www.dremi.info</a>. Find this Form at <a title="View Topic: Resizing Image With PHP" href="http://www.dremi.info/forum/viewtopic.php?p=814#814" target="_blank">PHP Tutorial Forum</a> <a title="tutorial photoshop" href="http://www.dremi.info" target="_blank"></a>
<h4 style="color:#999999;">
Data Terinput:</h4>
<? viewDIR(_UPLOAD_PATH, _RESIZE_PATH); ?>
</form>
<?
}
?>
</body>
</html>
setelah memalui validasi extensi file berformat jpg, dilakukan proses copy file terlebih dahulu, ke _UPLOAD_PATH direktory yang telah didefinisikan dalam file config.php
nah baru dah di mulai proses resize ulang pada gambar yang telah diupload, jadi logikana proses resize baru bisa difungsikan jika gambar telah berhasil di upload.. bgono freeennnndddd :idea:
baris dibwah ini akan mengcreate ulang size gambar, dimulai dari create dimensi X dan Y untuk mendefiniskan WIDTH dan HEIGHT.
$im=imagecreatefromjpeg($dir);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($new_width,$new_height);
baru setelah proses pengukuran selesai, gambar kembali di create ulang untuk selanjutnya akan diletakkan di direktori $rez
imagecopyresampled($newimage,$im,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($newimage,$rez,80);
jika proses
imagejpeg($newimage,$rez,80) berhasil, pesan sukses akan ditampilkan
if(imagejpeg($newimage,$rez,80))
{
redirect(2, "index.php", "RESIZING IMAGE SUCCESS");
}
else
{
warning("RESIZING IMAGE ERROR, $phperrormsg");
}
ouke, kalo lom dicoba blum sreg rasana, jadi cobain dlu gih.. hehek :?
semoga bermanfaat.. sekalipun anggota forum jarang ada yang berkoar :motz: :? <img src="{SMILIES_PATH}/icon_razz.gif" alt="

" title="zunge zeigen" />