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:32] smayr [View] |
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, | + | === View === |
- | { | + | <code php> |
- | if(isset($this->logo)) { | + | <? |
- | $this->logo = UploadedFile:: | + | |
- | if(is_object($this->logo)) { | + | |
- | $path = Yii:: | + | |
- | $this-> | + | |
- | $this-> | + | |
- | \Yii:: | + | |
- | -> | + | |
- | ->execute(); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
</ | </ | ||
Line 507: | Line 516: | ||
=== Model === | === Model === | ||
+ | File '' | ||
<code php> | <code php> | ||
<?php | <?php | ||
Line 572: | Line 582: | ||
</ | </ | ||
+ | File '' | ||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | namespace app\models; | ||
+ | |||
+ | use Yii; | ||
+ | use yii\base\Model; | ||
+ | //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 651: | Line 742: | ||
=== View === | === View === | ||
- | File '' | + | File '' |
<code php> | <code php> | ||
<?php | <?php | ||
Line 690: | Line 781: | ||
</ | </ | ||
- | File '' | + | File '' |
<code php> | <code php> | ||
<?php | <?php | ||
Line 737: | Line 828: | ||
</ | </ | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | 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:: | ||
+ | ?> | ||
</ | </ |