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:
-
-
- $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] < 1) || ($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"> |
- "58%" border="0" align="center">
-
-
-
-
-
-
-
-
-
-
-
-
-
Kalo yang ini untuk menampilkan gambarnya:-
-
-
- $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;
Cuman begitu saja. Semoga bermanfaat.Contoh yang sudah berjalan:
0 komentar:
Posting Komentar