Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- в beforeSave
- $images = Helper::getImagePath($this->text);
- if ($images && is_array($images)) {
- $this->text = $this->uploadFiles($images);
- }
- /**
- * @param string $html
- * @return array $files
- */
- public static function getImagePath($html)
- {
- $files = null;
- $html = SimpleHtmlDom::str_get_html($html);
- $images = $html->find('img');
- foreach ($images as $img) {
- $src = $img->getAttribute('src');
- if ($src) {
- $files[] = $src;
- }
- }
- return $files;
- }
- /**
- * @param array $files
- * @return string $text
- */
- public function uploadFiles($files)
- {
- $extensions = array('jpg', 'gif', 'png', 'jpeg', 'bmp', 'svg');
- $text = $this->text;
- $dir = Yii::getAlias('@storage/web');
- $name = $dir . "/articles/article{$this->id}/";
- if (!is_dir($name)) {
- mkdir($name, 0777, true);
- }
- foreach ($files as $file) {
- $fileInfo = pathinfo($file);
- if (in_array($fileInfo['extension'], $extensions)) {
- $fileName = $fileInfo['basename'];
- $imgHost = parse_url($file)['host'];
- $baseHost = parse_url(Yii::getAlias('@storageUrl'))['host'];
- if ($imgHost != $baseHost) {
- file_put_contents("{$name}/{$fileName}", fopen($file, 'r'));
- $text = str_replace($file, Url::to("@storageUrl/articles/article{$this->id}/{$fileName}"), $text);
- }
- }
- }
- return $text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement