/** * Возвращает html тег изображения. Производит ресайз * @param string $path путь к файлу * @param int $width ширина изображения * @param int $height высота изображения * @return string */ protected function renderImageTag($path, $width, $height) { if (!is_file($path) or !FileHelper::isImage($path)) { return ""; } $options = array_merge(["src" => Yii::$app->resizer->resize($path, $width, $height), "class" => "img-thumbnail detail-img"], $this->imageOptions); return Html::tag('img', '', $options); }
/** * Загрузка файлов по имени. Возвращает массив путей к загруженным файлам, относительно DOCUMENT_ROOT. * @param $name имя файла * @return array */ public function uploadFiles($name) { $this->checkModelFolder(); // Множественная загрузка файлов $files = UploadedFile::getInstancesByName($name); // Единичная загрузка. Удаляем старые файлы if (empty($files) and $file = UploadedFile::getInstanceByName($name)) { $this->deleteFiles(); $files = array($file); } $fileNames = []; if (!empty($files)) { foreach ($files as $file) { $fileName = FileHelper::getNameForSave($this->getSavePath(), $file->name); $fileNames[] = $this->getRelPath() . $fileName; $savePath = $this->getSavePath() . $fileName; $file->saveAs($savePath); chmod($savePath, $this->filePerm); if (FileHelper::isImage($savePath)) { Yii::$app->resizer->resizeIfGreater($savePath, $this->maxWidth, $this->maxHeight); } } } return $fileNames; }
<?php if (is_array($files)) { $i = 0; foreach ($files as $file) { ?> <li> <div class="uploader-widget-name"><?php echo basename($file["file"]); ?> </div> <div class="uploader-widget-preview"> <?php if (is_file(Yii::getAlias($webroot . $file["file"])) and FileHelper::isImage(Yii::getAlias($webroot . $file["file"]))) { ?> <img src="<?php echo $file["file"]; ?> " width="150" alt=""/> <?php } ?> </div> <div class="uploader-widget-progress progress-bar"></div> <input type="hidden" name="<?php
/** * Является ли файл изображением * @return bool */ public function isImage() { return FileHelper::isImage($this->path); }