Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
systems:yii2:file_upload [2018/02/28 16:25] smayr [Controller] |
systems:yii2:file_upload [2018/03/01 16:22] (current) smayr [View] |
||
---|---|---|---|
Line 468: | Line 468: | ||
== Example Image Upload and Save Filename in DB == | == Example Image Upload and Save Filename in DB == | ||
+ | |||
+ | === Model === | ||
+ | In the model, override '' | ||
+ | <code php> | ||
+ | public function afterSave($insert, | ||
+ | { | ||
+ | if(isset($this-> | ||
+ | $this-> | ||
+ | if(is_object($this-> | ||
+ | $path = Yii:: | ||
+ | $this-> | ||
+ | $this-> | ||
+ | \Yii:: | ||
+ | -> | ||
+ | -> | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
=== Controller === | === Controller === | ||
- | Add action '' | + | Add action '' |
<code php> | <code php> | ||
public function actionCreate() | public function actionCreate() | ||
Line 485: | Line 504: | ||
} | } | ||
} | } | ||
+ | </ | ||
- | public function afterSave($insert, $changedAttributes) | + | === View === |
+ | <code php> | ||
+ | <?= $form-> | ||
+ | </ | ||
+ | |||
+ | Source: [[http:// | ||
+ | |||
+ | == Example Image File Upload == | ||
+ | |||
+ | === Model === | ||
+ | File '' | ||
+ | <code php> | ||
+ | <?php | ||
+ | namespace app\models; | ||
+ | |||
+ | use yii; | ||
+ | use yii\base\Model; | ||
+ | use yii\web\UploadedFile; | ||
+ | |||
+ | defined(' | ||
+ | |||
+ | class ImageUploadForm extends Model | ||
{ | { | ||
- | | + | |
- | | + | * @var UploadedFile |
- | | + | */ |
- | | + | public |
- | $this->logo-> | + | |
- | | + | public function rules() |
- | | + | |
- | -> | + | |
- | -> | + | // see ' |
+ | [[' | ||
+ | ' | ||
+ | | ||
+ | ' | ||
+ | //' | ||
+ | //'mimeTypes' | ||
+ | | ||
+ | ' | ||
+ | | ||
+ | ]; | ||
+ | } | ||
+ | |||
+ | public function upload() | ||
+ | { | ||
+ | if ($this->validate()) { | ||
+ | //$this->imageFile->saveAs( | ||
+ | | ||
+ | //); // save to [app]/ | ||
+ | $this->imageFile->saveAs("uploads/" | ||
+ | return true; | ||
+ | } else { | ||
+ | return false; | ||
} | } | ||
} | } | ||
+ | | ||
+ | public function getFileName() | ||
+ | { | ||
+ | return strtolower(" | ||
+ | } | ||
+ | | ||
+ | public function getFilePath() | ||
+ | { | ||
+ | return Yii:: | ||
+ | } | ||
+ | | ||
+ | public function getFileUrl() | ||
+ | { | ||
+ | return Yii:: | ||
+ | } | ||
+ | | ||
} | } | ||
+ | ?> | ||
</ | </ | ||
- | Source: [[http://stackoverflow.com/ | + | File '' |
+ | <code php> | ||
+ | <?php | ||
- | == Example Image File Upload == | + | namespace app\models; |
- | === Model === | + | use Yii; |
+ | use yii\base\Model; // parent for model classes not associated with database tables | ||
+ | //use yii\db\ActiveRecord; | ||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | define(' | ||
+ | |||
+ | /** | ||
+ | * This is the model class for table " | ||
+ | * | ||
+ | * @property array $countries | ||
+ | * @property array $countryNames | ||
+ | * @property array $languages | ||
+ | * @property array $states | ||
+ | */ | ||
+ | class Tool extends Model | ||
+ | { | ||
+ | //... | ||
+ | | ||
+ | /** | ||
+ | * @inheritdoc | ||
+ | * Usage: | ||
+ | | ||
+ | */ | ||
+ | public static function generateEmptyPng() | ||
+ | { | ||
+ | $image | ||
+ | $canvas = imagecreate(PNG_DPI * LABEL_WIDTH, | ||
+ | | ||
+ | // Setup color | ||
+ | // | ||
+ | $color_background = imagecolorallocate($image, | ||
+ | $color_text | ||
+ | | ||
+ | // Background | ||
+ | imagefill($image, | ||
+ | | ||
+ | // Image border | ||
+ | // | ||
+ | // LABEL_MARGIN, | ||
+ | // (LABEL_WIDTH*PNG_DPI)-LABEL_MARGIN, | ||
+ | // (LABEL_HEIGHT*PNG_DPI)-LABEL_MARGIN, | ||
+ | // $color_text | ||
+ | //); | ||
+ | | ||
+ | // Text content | ||
+ | imagestring($image, | ||
+ | |||
+ | // Copy image to target canvas | ||
+ | imagecopy( | ||
+ | $canvas, | ||
+ | $image, | ||
+ | 0, 0, // dst x,y | ||
+ | 0, 0, // src x,y | ||
+ | (LABEL_WIDTH | ||
+ | (LABEL_HEIGHT * PNG_DPI) | ||
+ | ); | ||
+ | imagedestroy($image); | ||
+ | |||
+ | // Capture the full image from canvas | ||
+ | ob_start(); | ||
+ | imagepng($canvas); | ||
+ | $imageData = ob_get_contents(); | ||
+ | ob_end_clean(); | ||
+ | |||
+ | return $imageData; | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
=== Controller === | === Controller === | ||
+ | File '' | ||
<code php> | <code php> | ||
//... | //... | ||
Line 530: | Line 685: | ||
if ($model-> | if ($model-> | ||
// file is uploaded successfully | // file is uploaded successfully | ||
- | Yii:: | + | Yii:: |
+ | | ||
+ | | ||
| | ||
// Assign file to item | // Assign file to item | ||
Line 560: | Line 717: | ||
if ($resultsImport) { | if ($resultsImport) { | ||
Yii:: | Yii:: | ||
- | "Item image <code> | + | "Item image <tt> |
); | ); | ||
} else { | } else { | ||
- | $msg = "Item image <code> | + | $msg = "Item image <tt> |
- | $msg .= "- Source:<code> | + | $msg .= "- Source:<tt> |
- | $msg .= "- Target:<code> | + | $msg .= "- Target:<tt> |
Yii:: | Yii:: | ||
} | } | ||
Line 584: | Line 741: | ||
=== View === | === View === | ||
+ | |||
+ | File '' | ||
+ | <code php> | ||
+ | <?php | ||
+ | use yii\helpers\Html; | ||
+ | use yii\helpers\Url; | ||
+ | use yii\widgets\ActiveForm; | ||
+ | use yii\helpers\ArrayHelper; | ||
+ | |||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | |||
+ | ?> | ||
+ | |||
+ | <?php $form = ActiveForm:: | ||
+ | |||
+ | < | ||
+ | | ||
+ | <div class=" | ||
+ | <?php if(Yii:: | ||
+ | <div class=" | ||
+ | <?= Yii:: | ||
+ | </ | ||
+ | <?php endif; ?> | ||
+ | <?php if(Yii:: | ||
+ | <div class=" | ||
+ | <?= Yii:: | ||
+ | </ | ||
+ | <?php endif; ?> | ||
+ | </ | ||
+ | | ||
+ | <?= $form-> | ||
+ | |||
+ | <button class=' | ||
+ | |||
+ | <?php ActiveForm:: | ||
+ | </ | ||
+ | |||
+ | File '' | ||
+ | <code php> | ||
+ | <?php | ||
+ | $itemImage = (!empty($model-> | ||
+ | | ||
+ | '' | ||
+ | ); | ||
+ | ?> | ||
+ | |||
+ | <!-- Image Thumbnail --> | ||
+ | <?php if(!empty($itemImage)): | ||
+ | <img class=" | ||
+ | <?php else: ?> | ||
+ | <img class=" | ||
+ | <?php endif; ?> | ||
+ | |||
+ | <!-- Button trigger modal for Image --> | ||
+ | <?= Html:: | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]) ?> | ||
+ | <?= Html:: | ||
+ | |||
+ | <!-- Modal for Image --> | ||
+ | <div class=" | ||
+ | <div class=" | ||
+ | <div class=" | ||
+ | <div class=" | ||
+ | <h5 class=" | ||
+ | <button type=" | ||
+ | <span aria-hidden=" | ||
+ | </ | ||
+ | </ | ||
+ | <div class=" | ||
+ | <?php if(!empty($itemImage)): | ||
+ | <img class=" | ||
+ | <?php else: ?> | ||
+ | <img class=" | ||
+ | <?php endif; ?> | ||
+ | </ | ||
+ | <div class=" | ||
+ | <button type=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Or simply use the Yii modal helper: | ||
+ | <code php> | ||
+ | <?php | ||
+ | $itemImage = (!empty($model-> | ||
+ | Yii:: | ||
+ | '' | ||
+ | ); | ||
+ | |||
+ | // Image Thumbnail | ||
+ | if(!empty($itemImage)) { | ||
+ | echo "< | ||
+ | } else { | ||
+ | echo "< | ||
+ | } | ||
+ | | ||
+ | echo Html:: | ||
+ | [' | ||
+ | [' | ||
+ | ); | ||
+ | |||
+ | // Modal Window | ||
+ | yii\bootstrap\Modal:: | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ], | ||
+ | ]); | ||
+ | |||
+ | if(!empty($itemImage)) { | ||
+ | echo "< | ||
+ | } else { | ||
+ | echo "< | ||
+ | } | ||
+ | echo '< | ||
+ | echo ' | ||
+ | echo '</ | ||
+ | |||
+ | yii\bootstrap\Modal:: | ||
+ | ?> | ||
+ | </ |