2077 Posts in 484 Topics- by 821 Members - Latest Member: liricklagu

Pages: [1]   Go Down
  Print  
Author Topic: How to Resizing Image with PHP  (Read 3069 times)
webmaster
Administrator
phpBB Guru
*****
Offline Offline

Posts: 924


hairulazami
View Profile WWW
« on: May 12, 2008, 02:15:57 PM »

How to Resizing Image with PHP


DOWNLOAD 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


Code:
<?

## © 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

Code:
<?
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


Code:
<!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.

Code:
$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

Code:
imagecopyresampled($newimage,$im,0,0,0,0,$new_width,$new_height,$width,$height);
Code:
imagejpeg($newimage,$rez,80);

jika proses imagejpeg($newimage,$rez,80) berhasil, pesan sukses akan ditampilkan

Code:
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="Tongue" title="zunge zeigen" />
« Last Edit: May 17, 2010, 09:06:23 PM by webmaster » Logged


faris18787
phpBB Guru
*
Offline Offline

Posts: 59


ask me ask me
View Profile WWW
« Reply #1 on: May 12, 2008, 10:08:52 PM »

haha leh jg ni dicoba, gambarnya ke resize jadi se per sekian persennya, bukan ngatur pixel sendiri, jadi ga distort gitu yak,, hehe tar2 nyoba ah  :idea:
« Last Edit: January 01, 1970, 07:00:00 AM by faris18787 » Logged

piis V^_^

webmaster
Administrator
phpBB Guru
*****
Offline Offline

Posts: 924


hairulazami
View Profile WWW
« Reply #2 on: May 12, 2008, 11:25:02 PM »

bwat ngatur perubahan size dengan satuan pixel, tinggal ganti / tambahin baris ini:

config.php


Code:
define ("_PERCENTSIZE", 0.5);
menjadi:
Code:
define ("_HEIGHT", 150);
define ("_WIDTH", 150);

index.php

Code:
list($width, $height) = getimagesize($dir);
$new_width  = $width * _PERCENTSIZE;
$new_height = $height * _PERCENTSIZE;

menjadi:
Code:
//list($width, $height) = getimagesize($dir); //--> Disabled
$new_width  = _WIDTH;
$new_height = _HEIGHT;


woke broder... :D
« Last Edit: January 01, 1970, 07:00:00 AM by webmaster » Logged


Pages: [1]   Go Up
  Print  
 
Jump to: