Script untuk menyimpan gambar: -
-
- $db_host = "localhost";
- $db_user = "root";
- $db_pass = "p455w0rdku";
- $db_name = "simpol";
-
-
- $link = mysql_pconnect ($db_host, $db_user, $db_pass) or die ("Ga bisa connect");
- mysql_select_db ($db_name) or die ("Ga bisa select database");
-
- if (isset($act) && $act == "up_foto") {
- $id = trim($nip);
- $editFormAction = $_SERVER['PHP_SELF'];
- $img_thumb_width = 120;
- $path_thumbs = "items/icons";
-
- $file_type = $_FILES['imgfile']['type'];
- $file_name = $_FILES['imgfile']['name'];
- $file_size = $_FILES['imgfile']['size'];
- $file_tmp = $_FILES['imgfile']['tmp_name'];
-
-
- if(!is_uploaded_file($file_tmp)){
- echo "
Anda belum memilih file gambar....."; - print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
- exit();
- }
-
- $size =GetImageSize("$file_tmp");
- if (($size[2] <>$size[2] > 4)) {
- echo '
Maaf yang Anda pilih bukan gambar'; - print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
- exit();
- }
-
-
-
- $getExt = explode ('.', $file_name);
- $file_ext = $getExt[count($getExt)-1];
-
-
-
- $rand_name= "tmp";
-
- $ThumbWidth = $img_thumb_width;
-
-
- if($file_size){
- if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
- $new_img = imagecreatefromjpeg($file_tmp);
- }elseif($file_type == "image/x-png" || $file_type == "image/png"){
- $new_img = imagecreatefrompng($file_tmp);
- }elseif($file_type == "image/gif"){
- $new_img = imagecreatefromgif($file_tmp);
- }
-
- list($width, $height) = getimagesize($file_tmp);
- $imgratio=$width/$height;
- if ($imgratio>1){
- $newwidth = $ThumbWidth;
- $newheight = $ThumbWidth/$imgratio;
- }else{
- $newheight = $ThumbWidth;
- $newwidth = $ThumbWidth*$imgratio;
- }
-
-
- if (function_exists(imagecreatetruecolor)){
- $resized_img = imagecreatetruecolor($newwidth,$newheight);
- }else{
- die("Error: Please make sure you have GD library ver 2+");
- }
- imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
-
- ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
- ImageDestroy ($resized_img);
- ImageDestroy ($new_img);
-
- $gb_baru = "$path_thumbs/$rand_name.$file_ext";
-
-
-
- $fp = fopen($gb_baru, 'r') or die('ga bisa buka gambar');
-
- $data=fread($fp,filesize("$gb_baru"));
- $data = mysql_escape_string($data);
-
- $updatesql=mysql_query("UPDATE foto SET FOTO='$data' where NIP='$id'") or die(mysql_error());
- }
- }
- ?>
-
-
"100%" border="0" cellpadding="0" cellspacing="1" background="items/bg_gradasi.jpg"> -
- "73%">"items/icons/duk.gif" hspace="5" align="absmiddle" /> EDIT FOTO
- "27%" align="center">
-
-
- enctype="multipart/form-data" action="?mod=up_foto&nip=&act=up_foto">
-
"58%" border="0" align="center"> -
- "176" rowspan="3" align="center" valign="middle">
-
- "100%" bgcolor="#999999">
"0" cellpadding="3" width="100%" border="0"> -
-
- "#ededed">"?mod=up_foto&nip=">"modul/foto.php?nip=" alt="Foto" name="foto" border="3" id="foto" />
-
- "23" colspan="2" valign="bottom">Silahkan klik "Browse" dan pilih foto yang baru, kemudian klik "Submit".
-
-
- "23" colspan="2" valign="bottom">"imgfile" type="file" id="imgfile" size="35" />
-
-
- "310" valign="middle">
-
- "66" align="center" valign="middle">"submit" name="Submit" value="Submit" />
-
-
-
Tampil Gambar
- $db_host = "localhost";
- $db_user = "root";
- $db_pass = "p455w0rdku";
- $db_name = "simpol";
-
-
- $link = mysql_pconnect ($db_host, $db_user, $db_pass) or die ("Ga bisa connect");
- mysql_select_db ($db_name) or die ("Ga bisa select database");
-
-
- require_once ('../inc/db.php');
- $sql = mysql_query("SELECT NIP,FOTO FROM foto where foto.NIP = "$nip" ") or die(mysql_error());
-
- while ($res=mysql_fetch_array($sql)) {
- echo $res['FOTO'];
- }
- mysql_free_result($sql);
-
- ?>
Kemudian struktur databasenya sebagai berikut:
- CREATE TABLE `foto` (
- `NIP` varchar(27) NOT NULL default '',
- `FOTO` longblob,
- PRIMARY KEY (`NIP`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;