Selamat siang gan,
Kali ini mau numpang sharing resize dan upload file. Intinya kita punya foto asli (>2Mb), gimana kita buat aplikasi upload foto image yg gak perlu olah dulu manual foto tsb.
Saya kebetulan udah impplementasi ekstensi imagedan udah dicoba di beberapa aplikasi saya contohnya merah-putih.
Proses step-by-stepnya sbb:
-
Download ekstension image di alamat image.
-
Esktrak folder /image dan letakkan di /protected/extentions
-
edit /protected/config/main.php
'components'=>array(
..
//utk resize image
'image'=>array(
'class'=>'application.extensions.image.CImageComponent',
'driver'=>'GD',
) ,
..
- Utk ukuran max file yg bisa diupload aturnya di dua tempat:
a. php.ini file (klo di web server biasany ada di dir /etc) tambahkan:
upload_max_filesize = 5M
b. di model nya protected/model/News.php:
..
public function rules()
{
return array(
..
array('foto', 'file', 'allowEmpty'=>true,'maxSize'=>1024*1024*5, //ukuran file max
'types'=>'jpg,jpeg,png','tooLarge'=>'Ukuran foto tidak lebih dari 5Mb',
'wrongType'=>'Jenis file hanya JPEG atau PNG',
'on'=>'insert'),
array('foto', 'file', 'allowEmpty'=>true,'maxSize'=>1024*1024*5,
'types'=>'jpg,jpeg,png','tooLarge'=>'Ukuran foto tidak lebih dari 5Mb',
'wrongType'=>'Jenis file hanya JPEG atau PNG',
'except'=>'insert'),
...
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, judul, jenis, isi, foto, video, inputer, time', 'safe', 'on'=>'search'),
);
}
..
- Sedangkan untuk controller protected/controllers/NewsController.php:
...
public function actionCreate()
{
$model=new News;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['News']))
{
$rnd = rand(0,9999); // generate random number between 0-9999
$model->attributes=$_POST['News'];
$uploadedFile=CUploadedFile::getInstance($model,'foto');
// if(!empty($model->foto))
if(!empty($uploadedFile))
{
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$var = 'http://merahputih.panjattebing.org';
$model->foto = $var.'/images/data/'.$fileName;
$model->save(); //mengirim data lokasi file foto ke mysql
//$uploadedFile->saveAs(Yii::app()->basePath.'/../images/data/'.$fileName);
//simpan file
$name=Yii::app()->basePath.'/../images/data/'.$fileName;
$uploadedFile->saveAs($name); //
//
//utk resize gambar
$image = Yii::app()->image->load($name);
/* begin- utk memutar jika file asli portrait */
$tinggi=$image->__get('height');
$lebar=$image->__get('width');
if($tinggi>$lebar)
{
$image->resize(600, 400)->rotate(0);
}
else
{
$image->resize(600, 400);
}
/* end- utk memutar jika file ori portrait */
$image->save();
$this->redirect(array('admin'));
}
else
{
$model->save();
$this->redirect(array('admin')); //new
}
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['News']))
{
$_POST['News']['foto'] = $model->foto;
$model->attributes=$_POST['News'];
$uploadedFile=CUploadedFile::getInstance($model,'foto');
//proses simpan file gambar
if(!empty($uploadedFile))
{
$rnd = rand(0,9999); // generate random number between 0-9999
$var = 'http://merahputih.panjattebing.org';
$fileName = "{$rnd}-{$uploadedFile}"; // random number + file name
$model->foto = $var.'/images/data/'.$fileName ;
$model->save(); //mengirim data lokasi file foto ke mysql
//simpan file
$name=Yii::app()->basePath.'/../images/data/'.$fileName;
$uploadedFile->saveAs($name); //
//
//utk resize gambar
$image = Yii::app()->image->load($name);
/* begin- utk memutar jika file ori portrait */
$tinggi=$image->__get('height');
$lebar=$image->__get('width');
if($tinggi>$lebar)
{
$image->resize(600, 400)->rotate(0);
}
else
{
$image->resize(600, 400);
}
/* end- utk memutar jika file ori portrait */
$image->save();
$this->redirect(array('view','id'=>$model->id));
}
else
{
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
..
6.Form uploadnya sbb:
a. Jangan lupa menambahkan ‘enctype’ => ‘multipart/form-data’,), di awal atas form kongkritnya:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'news-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'enctype' => 'multipart/form-data', //tambahan dari _form hasil gii
),
)); ?>
b. lalu ketik bagian ini di form nya:
<div class="row">
<?php echo $form->labelEx($model,'foto'); ?>
<?php echo CHtml::activeFileField($model, 'foto'); ?> <font color='orange'><=.jpg/.png dan pastikan ukuran file max 5Mb</font>
<?php echo $form->error($model,'foto'); ?>
</div>
<?php if($model->isNewRecord!='1'){ ?>
<div class="row">
<?php echo CHtml::image($model->foto,"foto",array("width"=>100)); ?>
</div>
<?php
}
?>
-
pastikan tabel MySql kita ada field foto dengan tipe varchar size 250 default NULL.
-
buat sub-directory /data utk menyimpan file foto di dalam dir /images. Pastikan direktory tempat menyimpan (images/data) itu mempunyai permission mode: 775 (gunakan perintah Set permission).
-
Skrg waktu mencoba…jika tidak ada halangan di field foto tabel mysql akan ada detil lengkap nama file yg diupload.
-
Untuk menampilkan di _view.php:
<?php
if(!empty($data->foto)){ //jika record foto nya tidak kosong
echo CHtml::image(Yii::app()->request->baseUrl.$data->foto,"Image-".$data->id,array("width"=>40,"height"=>40));
}
?>
Untuk tampil di view.php:
if(!empty($model->foto))
{
echo CHtml::image(Yii::app()->request->baseUrl.$model->foto,"Image".$model->id,array("width"=>320));
}
- That’s it…semoga berhasil ya.
Salam hangat, MH