[Trick] 2 Libraries Codeigniter Buat Upload Gambar

Bismillah…

Sebetulnya ini gara gara kerjaan lagi, mau ceritanya gak? *langsungajah… okeh okeh… jadi di sini gw pake libraries untuk melakukan optimasi gambar, karena kalo pake bawaan si ci, rada aneh kalo di resize getuh, rada kurus dari file aselinya. Neh dibawah ini gambar aseli tanpa rekayasa. *lokatapaaan, hee.

aseli

Nah, kalo ini pake libraries dari bawaan… “Image_Lib”.

Image Lib bawaan

Nah, kalo ini baru pas dihati, pake libraries “Image_Moo”

Image Moo

Dengan Catatan Semua gambar yang di ubah menjadi dimensi height and weight 342 semua!

Nah, gimana?

Wey mana librariesnya | Iya ini sabar dong… di google aja cari banyak… atau yang mau todopoint dan berdasarkan kasus, ya tunggu ini baru gw kasih linknya:

1. https://github.com/Mat-Moo/image_moo
2. https://github.com/stvnthomas/CodeIgniter-Multi-Upload

Terus kodingan lo nya mana? | oh, okeh okeh.. gw share juga neh..

function do_upload()
    {
// standard ci
        $upload_conf = array(
            "upload_path" => $this->real_path,
            "allowed_types" => "jpg|JPG|PNG",
            "encrypt_name" => FALSE
        );
        $this->load->library('upload', $upload_conf);
        
// load dulu libraries upload
        if ($this->upload->do_multi_upload("myimage"))
        {
            $upload_data2 = $this->upload->get_multi_upload_data();
            // total image upload
            for($i=0; $i<count($upload_data2); $i++)
            { 
              // langsung ubah
              $this->resize($upload_data2[$i]["file_name"]);
              echo "DONE";
            }
        }
        else
        {    
// terserah mau dilariin kemana
            $this->index();
        }
    }

<!-- ini fungis resize -->

function resize($source)
    {                
        // For Thumbnail
        $config_thumb = array(
            "image_library"=>"gd2",
            "source_image"=>$this->real_path.'/'.$source,
            "new_image"=>'assets/doc/img/thumbnails/'.'/'.$source,
            "maintain_ratio"=>false,
            "width"=>62,
            "height"=>62
        );

        $this->image_lib->initialize($config_thumb);
        $this->image_lib->resize();
        $this->image_lib->clear();
        
        // For Small
        $config_small = array(
            "image_library"=>"gd2",
            "source_image"=>$this->real_path.'/'.$source,
            "new_image"=>'assets/doc/img/small/'.'/'.$source,
            "maintain_ratio"=>false,
            "width"=>152,
            "height"=>152
        );

        $this->image_lib->initialize($config_small);
        $this->image_lib->resize();
        $this->image_lib->clear();
        
        // For Medium
        $config_medium = array(
            "image_library"=>"gd2",
            "source_image"=>$this->real_path.'/'.$source,
            "new_image"=>'assets/doc/img/medium/'.'/'.$source,
            "maintain_ratio"=>false,
            "width"=>342,
            "height"=>342
        );

        $this->image_lib->initialize($config_medium);
        $this->image_lib->resize();
        $this->image_lib->clear();
        
        // For Full
        $config_full = array(
            "image_library"=>"gd2",
            "source_image"=>$this->real_path.'/'.$source,
            "new_image"=>'assets/doc/img/full/'.'/'.$source,
            "maintain_ratio"=>false,
            "width"=>0,
            "height"=>0
        );

        $this->image_lib->initialize($config_full);
        $this->image_lib->resize();
        $this->image_lib->clear();
        
    }

oke, nah kalo ini yang si image moo sama kayak diatas, yaudah ini sekalian.

function do_upload()
    {
        $upload_conf = array(
            "upload_path" => $this->real_path,
            "allowed_types" => "jpg|JPG|PNG",
            "encrypt_name" => FALSE
        );
        $this->load->library('upload', $upload_conf);
        
        if ($this->upload->do_multi_upload("myimage"))
        {
            $upload_data2 = $this->upload->get_multi_upload_data();
            
            // Hitung file yang diupload
            for($i=0; $i<count($upload_data2); $i++)
            { 
                
            // try if we use image_moo
     
            $source = $this->real_path."/".$upload_data2[$i]["file_name"];
           
            $medi_des = "./assets/doc/img/medium/";
            
            $medi_h = 342;
            $medi_w = 342;
          
            $this->image_moo
                    ->load($source)
                    ->resize_crop($medi_w,$medi_h)
                    ->save($medi_des . 'crop_'. $upload_data2[$i]['file_name'])
                    ->set_background_colour("#49F")
                    ->resize($medi_w,$medi_h,TRUE)
                    ->save($medi_des . 'colour_'.$upload_data2[$i]['file_name'])
                    ->resize_crop(150,150)
                    ->border(5, "#ffffff")
                    ->border(1, "#000000")
                    ->save($medi_des . 'border_'.$upload_data2[$i]['file_name'])
                    ->resize_crop($medi_w,$medi_h)
                    ->round(10)
                    ->save($medi_des . 'corner_'.$upload_data2[$i]['file_name'])
                    ->resize_crop($medi_w,$medi_h)
                    ->border_3d(5)
                    ->save($medi_des . '3d_'.$upload_data2[$i]['file_name']) ;
 
            if ($this->image_moo->errors) print $this->image_moo->display_errors();
            else {
                echo "udah selesai" ;
            }
            }
        }
        else
        {   
// terserah mau di bawa kemana 
            $this->index();
        }
    }

simpel kan?

Have A Nice Day!

Leave a comment

9 Comments

  1. Reblogged this on Dinesh Ram Kali..

    Reply
  2. Tommy

     /  12/12/2014

    kang masih bingung saya error di real_path nya malahan

    Reply
    • itu definisiin sendiri variabel atau functionnya, yang mengarahkan ke base path…. seharusnya bisa langsung ganti dengan base_url(‘assets/foto_utama’).

      karena, logicnya… ketika di upload saya simpan di folder real_path, setelah itu ambil data tersebut untuk di convert ke beberapa dimensi gambar.

      errornya apaha?

      Reply
      • Tommy

         /  15/12/2014

        oh jadi gitu ya kang makasih kang .. saya coba deh , ya waktu itu saya coba ya error di real_path nya aja saya udah coba buat folder nya sih tapi tetep error makasih kang

      • Tommy

         /  15/12/2014

        oh ya kang itu simpen nya di Controller / model maaf banyak nanya masih newbie kang

  3. Tommy

     /  16/12/2014

    kang bisa kasih contoh yang lengkap nya ? biar saya mengerti atau source nya

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: