/** * Action upload anh */ public function actionAdditemimage() { // Cac thong so mac dinh cua image // Kieu upload $type = Yii::$app->request->post('type'); // Module upload $module = Yii::$app->request->post('module'); // Cac truong cua image $columns = \yii\helpers\Json::decode(Yii::$app->request->post('columns')); // danh sach cac anh duoc upload $gallery = []; // Id cua gallery $id = uniqid('g_'); // Begin upload image if ($type == Gallery::TYPE_UPLOAD) { // Upload anh khi chon type la upload $images = \yii\web\UploadedFile::getInstancesByName('image'); if (empty($images)) { return; } foreach ($images as $image) { // Tao lai id khi upload nhieu anh $id = uniqid('g_'); $ext = FileHelper::getExtention($image); if (!empty($ext)) { $fileDir = strtolower($module) . '/' . date('Y/m/d/'); $fileName = uniqid() . '.' . $ext; $folder = Yii::getAlias(Yii::$app->getModule('gallery')->syaDirPath) . Yii::$app->getModule('gallery')->syaDirUpload . '/' . $fileDir; FileHelper::createDirectory($folder); $image->saveAs($folder . $fileName); $gallery[$id] = ['url' => $fileDir . $fileName, 'type' => $type]; } } } elseif ($type == Gallery::TYPE_URL) { // Lay ra duong dan anh khi type la url $image = Yii::$app->request->post('image'); if (empty($image)) { return; } $gallery[$id] = ['url' => $image, 'type' => $type]; } elseif ($type == Gallery::TYPE_PATH) { $image = Yii::$app->request->post('image'); if (empty($image)) { return; } $images = explode(',', $image); if (!empty($image) && is_array($images)) { foreach ($images as $img) { $id = uniqid('g_'); $gallery[$id] = ['url' => $img, 'type' => $type]; } } } // End upload image echo \sya\gallery\models\Gallery::generateGalleryTemplate($gallery, $module, $columns); }
/** * Ham lay ra anh theo kieu upload * @param string $urlImg duong dan anh * @param string $type kieu upload anh * @return string */ private static function generateImageByType($urlImg, $type) { $options = ['style' => 'width: 100px; height: 100px;']; $template = null; switch ($type) { case \sya\gallery\Gallery::TYPE_URL: $template = Html::img($urlImg, $options); break; default: $template = Html::img(FileHelper::getFileUploaded($urlImg), $options); break; } return $template; }
/** * Action upload anh */ public function actionAdditemimage() { // Cac thong so mac dinh cua image // Kieu upload $type = Yii::$app->request->post('type'); // Module upload $module = Yii::$app->request->post('module'); // Attribute name $attribute = Yii::$app->request->post('attribute'); // Cac truong cua image $columns = Json::decode(Yii::$app->request->post('columns')); $templateInfomationImage = Yii::$app->request->post('templateInfomationImage'); $templateInfomationImageDetail = Yii::$app->request->post('templateInfomationImageDetail'); // danh sach cac anh duoc upload $gallery = []; // Column defalt image $columnsDefault = ['title' => '', 'caption' => '', 'alt_text' => '']; // Id cua gallery $id = uniqid('g_'); // Begin upload image if ($type == Gallery::TYPE_UPLOAD) { // Upload anh khi chon type la upload $images = UploadedFile::getInstancesByName('image'); if (empty($images)) { return; } foreach ($images as $image) { // Tao lai id khi upload nhieu anh $id = uniqid('g_'); $ext = FileHelper::getExtention($image); if (!empty($ext)) { $fileDir = strtolower($module) . '/' . date('Y/m/d/'); $fileName = pathinfo($image, PATHINFO_BASENAME); $folder = Yii::getAlias(Yii::$app->getModule('gallery')->syaDirPath) . Yii::$app->getModule('gallery')->syaDirUpload . '/' . $fileDir; FileHelper::createDirectory($folder); $image->saveAs($folder . $fileName); $columnsDefault['title'] = reset(explode('.', $fileName)); $gallery[$id] = ArrayHelper::merge(['url' => $fileDir . $fileName, 'type' => $type], $columnsDefault); } } $template = Gallery::generateGalleryTemplateByPath($gallery); } elseif ($type == Gallery::TYPE_URL) { // Lay ra duong dan anh khi type la url $image = Yii::$app->request->post('image'); $columnsDefault = Json::decode($image); if (empty($image)) { return; } $gallery[$id] = ArrayHelper::merge(['type' => $type], $columnsDefault); $template = Gallery::generateGalleryTemplate($gallery, $columns, $module, $attribute, $templateInfomationImage, $templateInfomationImageDetail); } elseif ($type == Gallery::TYPE_PATH) { $image = Yii::$app->request->post('image'); if (empty($image)) { return; } $images = explode(';', $image); if (!empty($image) && is_array($images)) { foreach ($images as $img) { $columnsDefault = Json::decode($img); $id = uniqid('g_'); $gallery[$id] = ArrayHelper::merge(['type' => $type], $columnsDefault); } } $template = Gallery::generateGalleryTemplate($gallery, $columns, $module, $attribute, $templateInfomationImage, $templateInfomationImageDetail); } // End upload image echo $template; }
/** * Function generate preview infomation image * @param $image Infomation image * @return string */ public static function generatePreviewImage($image) { $image = Json::decode($image); $url = ArrayHelper::getValue($image, 'url'); $title = ArrayHelper::getValue($image, 'title'); $caption = ArrayHelper::getValue($image, 'caption'); $alt_text = ArrayHelper::getValue($image, 'alt_text'); $imageUrl = Yii::getAlias('@web') . DIRECTORY_SEPARATOR . Yii::$app->getModule('gallery')->syaDirUpload . DIRECTORY_SEPARATOR . $url; $imagePath = Yii::getAlias(Yii::$app->getModule('gallery')->syaDirPath) . Yii::$app->getModule('gallery')->syaDirUpload . DIRECTORY_SEPARATOR . $url; $info = FileHelper::getInfomation($imagePath); $basename = ArrayHelper::getValue($info, 'basename'); $filesize = ArrayHelper::getValue($info, 'filesize'); $fileatime = ArrayHelper::getValue($info, 'fileatime'); $width = ArrayHelper::getValue($info, 'width'); $height = ArrayHelper::getValue($info, 'height'); $template = Html::tag('h3', Yii::t('gallery', 'ATTACHMENT DETAILS'), []); // Info img $template .= Html::beginTag('div', ['class' => 'sya_info_galllery row']); $template .= Html::beginTag('div', ['class' => 'col-md-6', 'style' => 'padding: 0;']); $template .= Html::img($imageUrl, []); $template .= Html::endTag('div'); $template .= Html::beginTag('div', ['class' => 'col-md-6', 'style' => 'padding: 0; word-break: break-word;']); // File Name $template .= Html::beginTag('div', ['style' => 'font-weight: bold;']); $template .= $basename; $template .= Html::endTag('div'); $template .= Html::beginTag('div', []); $template .= date('d/m/Y', $fileatime); $template .= Html::endTag('div'); // File size $template .= Html::beginTag('div', []); $template .= $filesize; $template .= Html::endTag('div'); $template .= Html::beginTag('div', []); $template .= $width . ' x ' . $height; $template .= Html::endTag('div'); $template .= Html::endTag('div'); $template .= Html::endTag('div'); // Attribute img $template .= Html::beginTag('div', ['class' => 'col-md-12 form-horizontal', 'style' => 'margin-top: 20px;']); $template .= Html::beginForm(' ', ' ', ['id' => 'sya_gallery_form_preview']); // Url $template .= Html::beginTag('div', ['class' => 'form-group field-gallery-url']); $template .= Html::tag('label', 'Url', ['class' => 'control-label col-sm-3']); $template .= Html::beginTag('div', ['class' => 'col-sm-9']); $template .= Html::input('text', 'sya_url', $url, ['class' => 'col-sm-10 form-control', 'readonly' => true]); $template .= Html::endTag('div'); $template .= Html::endTag('div'); // Title $template .= Html::beginTag('div', ['class' => 'form-group field-gallery-title']); $template .= Html::tag('label', 'Title', ['class' => 'control-label col-sm-3']); $template .= Html::beginTag('div', ['class' => 'col-sm-9']); $template .= Html::input('text', 'sya_title', $title, ['class' => 'col-sm-10 form-control', 'id' => 'sya_preview_title']); $template .= Html::endTag('div'); $template .= Html::endTag('div'); // Caption $template .= Html::beginTag('div', ['class' => 'form-group field-gallery-caption']); $template .= Html::tag('label', 'Caption', ['class' => 'control-label col-sm-3']); $template .= Html::beginTag('div', ['class' => 'col-sm-9']); $template .= Html::textarea('sya_caption', $caption, ['class' => 'col-sm-10 form-control', 'id' => 'sya_preview_caption']); $template .= Html::endTag('div'); $template .= Html::endTag('div'); // Alt text $template .= Html::beginTag('div', ['class' => 'form-group field-gallery-alt-text']); $template .= Html::tag('label', 'Alt text', ['class' => 'control-label col-sm-3']); $template .= Html::beginTag('div', ['class' => 'col-sm-9']); $template .= Html::input('text', 'sya_alt_text', $alt_text, ['class' => 'col-sm-10 form-control', 'id' => 'sya_preview_alt_text']); $template .= Html::endTag('div'); $template .= Html::endTag('div'); $template .= Html::endForm(); $template .= Html::endTag('div'); return $template; }