Closed hfageeh closed 7 years ago
What version of cropbox? And could you give me your code?
3.0
view:
<?php
use yii\helpers\Html; use yii\widgets\ActiveForm; use bupy7\cropbox\Cropbox; / @var $this yii\web\View / / @var $model backend\models\Projectsgroups / / @var $form yii\widgets\ActiveForm / ?>
model:
<?php
namespace backend\models;
use Yii; use yii\helpers\FileHelper; use yii\imagine\Image; use yii\helpers\Json; use Imagine\Image\Box; use Imagine\Image\Point; /**
@property Projects[] $projects */ class Projectsgroups extends \yii\db\ActiveRecord { /**
/**
/**
/**
// rendering information about crop of ONE option $cropInfo = Json::decode($this->crop_info)[0]; $cropInfo['dWidth'] = (int)$cropInfo['dWidth']; //new width image $cropInfo['dHeight'] = (int)$cropInfo['dHeight']; //new height image $cropInfo['x'] = $cropInfo['x']; //begin position of frame crop by X $cropInfo['y'] = $cropInfo['y']; //begin position of frame crop by Y // Properties bolow we don't use in this example //$cropInfo['ratio'] = $cropInfo['ratio'] == 0 ? 1.0 : (float)$cropInfo['ratio']; //ratio image. //$cropInfo['width'] = (int)$cropInfo['width']; //width of cropped image //$cropInfo['height'] = (int)$cropInfo['height']; //height of cropped image //$cropInfo['sWidth'] = (int)$cropInfo['sWidth']; //width of source image //$cropInfo['sHeight'] = (int)$cropInfo['sHeight']; //height of source image
//delete old images $oldImages = FileHelper::findFiles(Yii::getAlias('@path/to/save/image'), [ 'only' => [ $this->id . '.', 'thumb_' . $this->id . '.', ], ]); for ($i = 0; $i != count($oldImages); $i++) { @unlink($oldImages[$i]); }
//saving thumbnail $newSizeThumb = new Box($cropInfo['dWidth'], $cropInfo['dHeight']); $cropSizeThumb = new Box(200, 200); //frame size of crop $cropPointThumb = new Point($cropInfo['x'], $cropInfo['y']); $pathThumbImage = Yii::getAlias('@path/to/save/image') . '/thumb_' . $this->id . '.' . $this->image->getExtension();
$image->resize($newSizeThumb) ->crop($cropPointThumb, $cropSizeThumb) ->save($pathThumbImage, ['quality' => 100]);
//saving original $this->image->saveAs( Yii::getAlias('@path/to/save/image') . '/' . $this->id . '.' . $this->image->getExtension() ); } }
controller: public function actionCreate() { $model = new Projectsgroups(); //$upload = new UploadForm(); if ($model->load(Yii::$app->request->post())) { // get uploaded file information $groupName = strtolower($model->en_groupname); $imageName = str_replace(' ', '',$groupName );; $model->image = UploadedFile::getInstance($model, 'image'); $model->image->saveAs('../../frontend/web/images/project/' . $imageName.'.'.$model->image->extension);
// save the path in DB
$model->imageFile = $imageName.'.'.$model->image->extension;
$model->created = date('Y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->projgroup_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Wow, very dirty... You should wrap you code in markdown code tag, please.
Could you give me screenshot of the error message?
It's a bug. You can fix it following code or use 4 version of cropbox:
<?= $form->field($model, 'image')->widget(Cropbox::className(), [
'attributeCropInfo' => 'crop_info',
'optionsCropbox' => [
'cropSettings' => [],
],
]); ?>
The 'icon-framework' option has not been setup in Yii params. Check your configuration file.
its work you the man 💯
I dont know what wrong I just install it as you said step by step please advise