kali ini ane mo share ,gimana cara upload gambar , merubah ukuran dan menandakan gambar, pertama yang harus dilakukan adalah
1. download dulu Codeigniter trebaru, (ane pake versi 2.0.3)
kite g usah buat lagi yang namanye library buat upload karena di Codeigniter sudah di sediakan di system->libraries.
ada 2 file yang di butuhkan 1.image_lib.php dan upload.php. nah kite copy file Image_lib.php ke application->libraries dan kite rename menjadi MY_Image_lib.php dan kite edit pada bari ke 27 :
baca lebih lanjut dah..
class CI_Image_lib
jadi ,class MY_Image_lib extends CI_Image_lib
setelah itu kite bua file di application->controller dengan file upload.php dengan code dibawah inibuat file di controller dengan nama upload.php dengan code :
<?php
class Upload extends CI_Controller {
function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); }
function index() { $this->load->view('upload_form', array('error' => ' ' )); }
function cobaupload(){
// buat folder userfile sejajar dengan folder application dan system $config['upload_path'] = './userfile/'; //pastikan permissionnya udah diset ke 777 $config['allowed_types'] = 'gif|jpg|png|jpeg'; //tipe file yang diperbolehkan $config['max_width'] = 1028; //ukuran maksimal yang diperbolehkan $config['max_height'] = 780; //ukuran maksimal yang diperbolehkan $this->load->library('upload', $config); if($this->upload->do_upload('myfile')){//jika file berhasil diupload $tp=$this->upload->data(); $res = $tp['full_path']; $ori = $tp['file_name']; $rw = $tp['raw_name']; $fr = $tp['file_ext']; $kcl = $tp['raw_name'].'_thumb'.$tp['file_ext']; $tnd = $tp['raw_name'].'_water'.$tp['file_ext']; $mr = $tp['file_path'].$ori; $tn = $tp['file_path'].$tnd;
$ubah = $this->ubah_ukuran_gambar($res); $mark = $this->watermarking($mr); chmod($res,0777); chmod($tn,0777); $data = array('upload_data' => $this->upload->data(),'ori'=>$ori,'tnd'=>$tnd,'kcl'=>$kcl); $this->load->view('upload_success', $data); } else {//jika gagal $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); }
}
// function buat bikin tanda di gambar function watermark($gb) { $config['source_image'] = $gb; $config['wm_text'] = 'D U T A L A'; // tanda yang akan tercetak di gambar $config['wm_type'] = 'text'; $config['wm_font_path'] = './system/fonts/BOWSHADW.TTF'; //pastikan fontnya ada $config['wm_font_size'] = 60; $config['wm_font_color'] = '00000'; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = 5; $config['thumb_marker'] = '_water'; $config['wm_opacity'] = 5; $config['wm_shadow_color'] = 333333;
$this->image_lib->initialize($config);
$this->image_lib->watermark(); }
function watermarking($gb) { $config['source_image'] = $gb; $config['wm_type'] = 'overlay'; $config['wm_overlay_path'] = './userfile/lara.png';
//ganti gambar sesuai yg bro pnya ini untuk watermarking $config['wm_font_color'] = '00000'; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = 5; $config['wm_x_transp'] = 0; $config['wm_y_transp'] = 5; $config['thumb_marker'] = '_water'; $config['wm_opacity'] = 75;
$this->image_lib->initialize($config);
$this->image_lib->watermark(); }
// function merubah ukuran gambar function ubah_ukuran_gambar($tp) { $config ['image_library'] = 'gd2'; $config ['source_image'] = $tp; $config ['create_thumb'] = TRUE; $config ['maintain_ratio'] = TRUE;
// ukuran akan di ubah tidak kedua-duanya bisa jadi width atau height saja $config ['width'] = 300; $config ['height'] = 250; $this->load->library('image_lib',$config); $this->image_lib->resize(); } }setelah itu kita buat file upload_form.php di application->view dengan code dibawah ini :
<html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php //jangan lupa menyertakan $this->load->helper('form'); di controller yang memanggil view ini echo form_open_multipart('upload/cobaupload', '');?> <div style="padding-left:35px;"> <h2>upload, resize & watermarking</h2> <?php echo form_upload('myfile'). form_submit('submit', 'Submit').'</p>'; ?> </form> </div> </body> </html>dan buat satu lagi dengan nama upload_success.php dengan code :
<html> <head> <title>Upload Form</title> </head> <body> <div style="padding-left:5px;"> <h3>Your file was successfully uploaded!</h3> <ul> <?php foreach ($upload_data as $item => $value):?> <li><?php echo $item;?>: <?php echo $value;?></li> <?php endforeach; ?> </ul> <p><?php echo anchor('upload', 'Upload Another File!'); ?></p> </div> <h1>File Asli</h1> <img src="<?=base_url();?>userfile/<?=$ori;?>"><br/><?php echo $ori;?></img></div> <h1>File yang sudah di watermarking</h1> <img src="<?=base_url();?>userfile/<?=$tnd;?>"><br/><?php echo $tnd;?></img> <div style="margin:-730px 0 0 1100px;"> <h1>File yang sudah di resize</h1> <img src="<?=base_urll();?>userfile/<?=$kcl;?>"><br/><?php echo $kcl;?></img> </div> </body> </html>
Sumber




Tidak ada komentar:
Posting Komentar