Udah lama ne ga update blog gue... Ne gue dapet dari om google, cara mudah untuk menyimpan gambar.
Oh iya, script ini sudah dilengkapi dengan auto resize gambar dan sudah dilengkapi fungsi cek apakah yang diupload itu gambar atau bukan.
Ini gw ambil dari simpeg online Pemkab Trenggalek. Silahkan diedit sesuai keperluan Anda.
Script untuk menyimpan gambar:
- // DATABASE
- $db_host = "localhost";
- $db_user = "root";
- $db_pass = "p455w0rdku";
- $db_name = "simpol";
- // BAGIAN INI KE BAWAH JANGAN DIUBAH
- $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'];
- //check if you have selected a file.
- if(!is_uploaded_file($file_tmp)){
- echo "
Anda belum memilih file gambar....."; - print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
- exit(); //exit the script and don't do anything else.
- }
- //check file extension jika salah or bukan gambar langsung keluar
- $size =GetImageSize("$file_tmp");
- if (($size[2] < 1) || ($size[2] > 4)) {
- echo '
Maaf yang Anda pilih bukan gambar'; - print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
- exit();
- }
- //get the file extension.
- $getExt = explode ('.', $file_name);
- $file_ext = $getExt[count($getExt)-1];
- //create a random file name
- $rand_name= "tmp";
- //get the new width variable.
- $ThumbWidth = $img_thumb_width;
- //keep image type
- 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 and height and keep height ratio.
- list($width, $height) = getimagesize($file_tmp);
- $imgratio=$width/$height;
- if ($imgratio>1){
- $newwidth = $ThumbWidth;
- $newheight = $ThumbWidth/$imgratio;
- }else{
- $newheight = $ThumbWidth;
- $newwidth = $ThumbWidth*$imgratio;
- }
- //function for resize image.
- 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);
- //save image
- ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
- ImageDestroy ($resized_img);
- ImageDestroy ($new_img);
- $gb_baru = "$path_thumbs/$rand_name.$file_ext";
- ///ini ke bawah buat nyimpen gambar ke database
- $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">
- "58%" border="0" align="center">
| "73%"> | "27%" align="center"> |
"176" rowspan="3" align="center" valign="middle">"3" border="0">
| "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" /> |
- // DATABASE
- $db_host = "localhost";
- $db_user = "root";
- $db_pass = "p455w0rdku";
- $db_name = "simpol";
- // BAGIAN INI KE BAWAH JANGAN DIUBAH
- $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);
- ?>
- CREATE TABLE `foto` (
- `NIP` varchar(27) NOT NULL default '',
- `FOTO` longblob,
- PRIMARY KEY (`NIP`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Contoh yang sudah berjalan:











