PHP code example of sergeykoz / yii2-imageupload
1. Go to this page and download the library: Download sergeykoz/yii2-imageupload library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sergeykoz / yii2-imageupload example snippets
use ssoft\imageupload\ImageUpload;
echo $form = ActiveForm::begin(['id' => 'image-form', 'options' => ['enctype'=>'multipart/form-data']]);
// first way
echo ImageUpload::widget([
'model' => $photoModel,
'attribute' => 'photo',
'parametersAttibute' => 'photo_parameters',
'addClass' => 'col-sm-8',
'imageUrl' => Yii::getAlias('@web') . '/files',
'imagePath' => Yii::getAlias('@webroot') . '/files',
'placeholder' => 'Photo',
'size' => ['height' => 300, 'width' => 100],
'aspectRatio' => 0.33,
'disabled' => false
]);
// second way
echo $form->field($photoModel, 'image')->widget(ImageUpload::className(), [
'parametersAttibute' => 'image_parameters',
'imageUrl' => Yii::getAlias('@web') . '/files',
'imagePath' => Yii::getAlias('@webroot') . '/files',
'placeholder' => 'Image'
]);
echo ActiveForm::end();
'controllerMap' => [
'imageupload' => 'ssoft\imageupload\ImageController',
],
use ssoft\imageupload\Image;
// create instance of the image
$image = new Image([
'imagePath' => Yii::getAlias('@webroot') . '/files',
'imageFile' => $photoModel->photo,
'parameters' => $photoModel->photo_parameters,
]);
// save the image with size 100x300 with name Filename100x300.Ext
$image->save(
Yii::getAlias('@webroot') . '/files',
Image::thumbnailName($photoModel->photo, ['height' => 300, 'width' => 100]),
['height' => 300, 'width' => 100]
);
// get content of the image png
echo $image->show('png', ['height' => 600, 'width' => 600]);