Jumat, 14 Oktober 2011


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:

  1. // DATABASE
  2. $db_host = "localhost";
  3. $db_user = "root";
  4. $db_pass = "p455w0rdku";
  5. $db_name = "simpol";
  6. // BAGIAN INI KE BAWAH JANGAN DIUBAH
  7. $link = mysql_pconnect ($db_host, $db_user, $db_pass) or die ("Ga bisa connect");
  8. mysql_select_db ($db_name) or die ("Ga bisa select database");
  9. if (isset($act) && $act == "up_foto") {
  10. $id = trim($nip);
  11. $editFormAction = $_SERVER['PHP_SELF'];
  12. $img_thumb_width = 120;
  13. $path_thumbs = "items/icons";
  14. $file_type = $_FILES['imgfile']['type'];
  15. $file_name = $_FILES['imgfile']['name'];
  16. $file_size = $_FILES['imgfile']['size'];
  17. $file_tmp = $_FILES['imgfile']['tmp_name'];
  18. //check if you have selected a file.
  19. if(!is_uploaded_file($file_tmp)){
  20. echo "

    Anda belum memilih file gambar....."
    ;
  21. print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
  22. exit(); //exit the script and don't do anything else.
  23. }
  24. //check file extension jika salah or bukan gambar langsung keluar
  25. $size =GetImageSize("$file_tmp");
  26. if (($size[2] < 1) || ($size[2] > 4)) {
  27. echo '

    Maaf yang Anda pilih bukan gambar'
    ;
  28. print "refresh" content="0; URL=?mod=up_foto&nip=$nip">";
  29. exit();
  30. }
  31. //get the file extension.
  32. $getExt = explode ('.', $file_name);
  33. $file_ext = $getExt[count($getExt)-1];
  34. //create a random file name
  35. $rand_name= "tmp";
  36. //get the new width variable.
  37. $ThumbWidth = $img_thumb_width;
  38. //keep image type
  39. if($file_size){
  40. if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
  41. $new_img = imagecreatefromjpeg($file_tmp);
  42. }elseif($file_type == "image/x-png" || $file_type == "image/png"){
  43. $new_img = imagecreatefrompng($file_tmp);
  44. }elseif($file_type == "image/gif"){
  45. $new_img = imagecreatefromgif($file_tmp);
  46. }
  47. //list width and height and keep height ratio.
  48. list($width, $height) = getimagesize($file_tmp);
  49. $imgratio=$width/$height;
  50. if ($imgratio>1){
  51. $newwidth = $ThumbWidth;
  52. $newheight = $ThumbWidth/$imgratio;
  53. }else{
  54. $newheight = $ThumbWidth;
  55. $newwidth = $ThumbWidth*$imgratio;
  56. }
  57. //function for resize image.
  58. if (function_exists(imagecreatetruecolor)){
  59. $resized_img = imagecreatetruecolor($newwidth,$newheight);
  60. }else{
  61. die("Error: Please make sure you have GD library ver 2+");
  62. }
  63. imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  64. //save image
  65. ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
  66. ImageDestroy ($resized_img);
  67. ImageDestroy ($new_img);
  68. $gb_baru = "$path_thumbs/$rand_name.$file_ext";
  69. ///ini ke bawah buat nyimpen gambar ke database
  70. $fp = fopen($gb_baru, 'r') or die('ga bisa buka gambar');
  71. $data=fread($fp,filesize("$gb_baru"));
  72. $data = mysql_escape_string($data);
  73. $updatesql=mysql_query("UPDATE foto SET FOTO='$data' where NIP='$id'") or die(mysql_error());
  74. }
  75. }
  76. ?>


  77. "100%" border="0" cellpadding="0" cellspacing="1" background="items/bg_gradasi.jpg">
  78. "73%">"items/icons/duk.gif" hspace="5" align="absmiddle" /> EDIT FOTO "27%" align="center">
  79. "POST" enctype="multipart/form-data" action="?mod=up_foto&nip=&act=up_foto">
  80. "58%" border="0" align="center">
  81. "176" rowspan="3" align="center" valign="middle">"3" border="0">
  82. "100%" bgcolor="#999999">"0" cellpadding="3" width="100%" border="0">
  83. "#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">

  84. "66" align="center" valign="middle">"submit" name="Submit" value="Submit" />
Kalo yang ini untuk menampilkan gambarnya:
  1. // DATABASE
  2. $db_host = "localhost";
  3. $db_user = "root";
  4. $db_pass = "p455w0rdku";
  5. $db_name = "simpol";
  6. // BAGIAN INI KE BAWAH JANGAN DIUBAH
  7. $link = mysql_pconnect ($db_host, $db_user, $db_pass) or die ("Ga bisa connect");
  8. mysql_select_db ($db_name) or die ("Ga bisa select database");
  9. require_once ('../inc/db.php');
  10. $sql = mysql_query("SELECT NIP,FOTO FROM foto where foto.NIP = "$nip" ") or die(mysql_error());
  11. while ($res=mysql_fetch_array($sql)) {
  12. echo $res['FOTO'];
  13. }
  14. mysql_free_result($sql);
  15. ?>
Kemudian struktur databasenya sebagai berikut:
  1. CREATE TABLE `foto` (
  2. `NIP` varchar(27) NOT NULL default '',
  3. `FOTO` longblob,
  4. PRIMARY KEY (`NIP`)
  5. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Cuman begitu saja. Semoga bermanfaat.
Contoh yang sudah berjalan: contoh upload gambar ke database

0 komentar:

Posting Komentar