Oct 282012
 

Assalamu’ alaikum wr wb

Yii upload images

Kali ini saya akan berbagi bagaimana cara upload gambar dengan menggunakan framework Yii (Yes It Is). Yii mempunyai banyak sekali extension yang dapat kita gunakan untuk mempermudah program yang akan kita buat. Untuk kali ini kita akan membuat upload image dengan menggunakan extension Upload yang sudah tersedia di Yii.

Yang di butuhkan :

  1. Apache.
  2. Yii Framework.
  3. Upload Extension.
Setelah download extension extrack semuake dalam folder extensions. Untuk penggunakan anda bisa membaca dalam link yang diats di upload extension. contoh :
// import the class
Yii::import('application.extensions.upload.Upload');

// receive file from post
$Upload = new Upload( (isset($_FILES['Filedata']) ? $_FILES['Filedata'] : null) );
$Upload->jpeg_quality  = 100;
$Upload->no_script     = false;
$Upload->image_resize  = true;
$Upload->image_x       = 700;
$Upload->image_y       = 500;
$Upload->image_ratio   = true;

// some vars
$newName  = date('YmdHis');
$destPath = Yii::app()->getBasePath().'/../images/gallery/';
$destName = '';

// verify if was uploaded
if ($Upload->uploaded) {
  $Upload->file_new_name_body = $newName;
  $Upload->process($destPath);

  // if was processed
  if ($Upload->processed) {
    $destName = $Upload->file_dst_name;

    // write image filename on table
    $photo = new Photo();
    $photo->gallery = $_POST['gallery_id'];
    $photo->image_normal = $destName;
    $photo->image_thumb  = 'thumb_' . $destName;
    $photo->save();

    // create the thumb
    unset($Upload);

    $Upload = new Upload($destPath.$destName);
    $Upload->file_new_name_body   = 'thumb_' . $newName;
    $Upload->no_script            = false;
    $Upload->image_resize         = true;
    $Upload->image_x              = 120;
    $Upload->image_y              = 80;
    $Upload->image_ratio          = true;
    $Upload->process($destPath);

  } else {
    echo($Upload->error)
  }
} else {
  echo('Select a file to send');
}
Ok selamat mencoba gitu saja ya untuk pengembangan silakan di buat sendiri ya.
Sumber : http://www.yiiframework.com

Wassalamu’ alaikum wr wb