Hi, Guys I like this awesome class for image/file manipulation.
I have an auto listing webapp. I have everything working, but not images. So I have spent my time trying to get this class working in yii application following prchakal post. I have two models one for handling listing information and one for handling image information.
In may Listing model I have the relationship
<?php
class Listing extends CActiveRecord
{
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
//Other relationship here …………………………….
'images' => array(self::HAS_MANY,'AutoListingImages','listing_id'),
);
//Other methods here
//The method for image upload
public function saveImage()
{
//import the class for image manipulation from the extension folder
Yii::import('application.extensions.upload.Upload');
//Receive file/image data from the post request
$Upload = new Upload((isset($_FILES['images']) ? $_FILES['images'] : null) );
$Upload->jpeg_quality = 100;
$Upload->no_script = false;
$Upload->image_resize = true;
$Upload->image_x = 300;
$Upload->image_y = 250;
$Upload->image_ratio = true;
//some vars
$rand = rand(1000,9000);
$newName = $rand;
$image_path = Yii::app()->getBasePath().'/upload_images/listing_uploads/';
$image_thumb_path = Yii::app()->getBasePath().'/upload_images/listing_uploads/thumbnails/';
$destName = '';
//Verify if was uloaded
if($Upload->uploaded) {
$Upload->file_new_name_body = $newName;
$Upload->process($image_path);
//if was processed
if($Upload->processed) {
$destName = $Upload->file_dst_name;
$thumbDestName = 'thumb_'.$destName;
//write image file on the database
$image = new AutoListingImages();
$image->image_path = $image_path/$destName;
$image->image_thumb_path = $image_thumb_path/$thumbDestName;
$image->listing_id = $_POST[listing_id];
//$image->create_time = new CDbExpression('NOW()');
//$image->author_id = $author_id;
$image->save();
//Create the thumb
unset ($Upload);
$xUpload = new Upload($image_thumb_path.$destName);
$Upload->file_new_name_body = 'thumb_' . $newName;
$Upload->no_script = false;
$Upload->image_resize = true;
$Upload->image_x = 85;
$Upload->image_y = 50;
$Upload->image_ratio = true;
$Upload->process($image_thumb_path);
} else {
echo ($Upload->error);
}
} else {
echo "Select an image to upload";
}
}
In my ListingImages model I have
<?php
class ListingImages extends CActiveRecord
{
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('image_path, image_thumb_path', 'required'),
array('main_image, listing_id', 'numerical', 'integerOnly'=>true),
array('image_path, image_thumb_path', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('listing_id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
//Other relationship here …………………………….
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'listing' => array(self::BELONGS_TO, 'Listing', 'listing_id'),
); );
}
Here is my controller which is the controller for my model Listing
<?php
class AutoListingsController extends Controller
{
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model = new Listing;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Listing']))
{
$model->attributes=$_POST['Listing'];
//This is how you capture those uploaded images.
//$model->images = CUploadedFile::getInstance($model, 'images');
if($model->save())
{
if($model->images !== null)
$model->saveImage();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
}
And here is my partial _form.
<div class="form">
<?php $form=$this->beginWidget('CActiveForm',array(
'id'=>'listing-form',
'enableClientValidation' => true,
'enableAjaxValidation'=>true,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'ad_title'); ?>
<?php echo $form->textField($model,'ad_title',array('size'=>30,'maxlength'=>60)); ?>Example. New Toyota corolla in Kayole
<?php echo $form->error($model,'ad_title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'images'); ?>
<?php //echo CHtml::activeFileField($model, "[0]images");?>
<br/>
<?php //echo CHtml::activeFileField($model, "[1]images");?>
<br/>
<?php echo CHtml::activeFileField($model, "images");?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Add Listing' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
With this arrangement of models, controllers and the form My images are not uploading and no errors showing up. I don’t know where the issues is. Also how can I support multiple image upload. The only information I need for image table is image_path, image_thumb_path, author_id, listing_id and create_time. Please help.