public function insert() { /** @var \app\services\GetArticle\ExtractorInterface $extractor */ $extractor = null; switch ($this->provider) { case 'verhosvet': $extractor = new \app\services\GetArticle\Verhosvet($this->url); break; case 'youtube': $extractor = new \app\services\GetArticle\YouTube($this->url); break; } if (is_null($extractor)) { throw new Exception('Не верный extractor'); } $row = $extractor->extract(); $articleObject = Article::insert(['header' => $row['header'], 'content' => $row['content'], 'description' => $row['description'], 'source' => $this->url, 'id_string' => Str::rus2translit($row['header']), 'date_insert' => gmdate('YmdHis'), 'tree_node_id_mask' => (new BitMask($this->tree_node_id_mask))->getMask()]); $this->id = $articleObject->getId(); $image = $row['image']; $imageContent = file_get_contents($image); $imageUrl = parse_url($image); $pathInfo = pathinfo($imageUrl['path']); $pathInfo['extension']; $fields = \cs\Widget\FileUpload2\FileUpload::save(File::content($imageContent), $pathInfo['extension'], ['image', 'Картинка', 0, 'string', 'widget' => [FileUpload::className(), ['options' => ['small' => \app\services\GsssHtml::$formatIcon]]]], $this); $articleObject->update($fields); return true; }
/** * Сохраняет картинку по формату * * @param \cs\services\File $file * @param \cs\services\SitePath $destination * @param array $field * @param array | false $format => [ * 3000, * 3000, * FileUpload::MODE_THUMBNAIL_OUTBOUND * 'isExpandSmall' => true, * ] , * * @return \cs\services\SitePath */ private static function saveImage($file, $destination, $format, $field) { if ($format === false || is_null($format)) { $file->save($destination->getPathFull()); return $destination; } $widthFormat = 1; $heightFormat = 1; if (is_numeric($format)) { // Обрезать квадрат $widthFormat = $format; $heightFormat = $format; } else { if (is_array($format)) { $widthFormat = $format[0]; $heightFormat = $format[1]; } } // generate a thumbnail image $mode = ArrayHelper::getValue($format, 2, self::MODE_THUMBNAIL_CUT); if ($file->isContent()) { $image = Image::getImagine()->load($file->content); } else { $image = Image::getImagine()->open($file->path); } if (ArrayHelper::getValue($format, 'isExpandSmall', true)) { $image = self::expandImage($image, $widthFormat, $heightFormat, $mode); } $quality = ArrayHelper::getValue($field, 'widget.1.options.quality', 80); $options = ['quality' => $quality]; $image->thumbnail(new Box($widthFormat, $heightFormat), $mode)->save($destination->getPathFull(), $options); return $destination; }
/** * Добавить послание из GetArticle * * @param \app\services\GetArticle\ExtractorInterface $extractor * * @return static * @throws \yii\base\Exception */ public static function insertExtractorInterface($extractor) { $row = $extractor->extract(); if (is_null($row['header'])) { throw new Exception('Нет заголовка'); } if ($row['header'] == '') { throw new Exception('Нет заголовка'); } if (is_null($row['description'])) { throw new Exception('Нет описания'); } if ($row['description'] == '') { throw new Exception('Нет описания'); } $fields = ['header' => $row['header'], 'content' => $row['content'], 'description' => $row['description'], 'source' => $extractor->getUrl(), 'id_string' => Str::rus2translit($row['header']), 'date_insert' => gmdate('YmdHis'), 'date' => gmdate('Ymd'), 'img' => '']; $articleObject = self::insert($fields); $model = new \app\models\Form\Chenneling(); $model->id = $articleObject->getId(); $image = $row['image']; if ($image) { try { $imageContent = file_get_contents($image); $imageUrl = parse_url($image); $pathInfo = pathinfo($imageUrl['path']); $pathInfo['extension']; $fields = FileUpload::save(File::content($imageContent), $pathInfo['extension'], ['img', 'Картинка', 0, 'string', 'widget' => [FileUpload::className(), ['options' => ['small' => \app\services\GsssHtml::$formatIcon]]]], $model); $articleObject->update($fields); } catch (\Exception $e) { } } return $articleObject; }