示例#1
0
 public function addText($text)
 {
     $group = 'Текст';
     if (empty($this->photos[$group])) {
         $this->photos[$group] = [];
     }
     //нужно сгенерить фотку из текста
     $style = Style::findOne(['id' => $this->style_id]);
     if (empty($style)) {
         return ['error' => ['msg' => Yii::t('app', 'Стиль для фотокниги не найден')]];
     }
     $font = Font::findOne(['id' => $style->font_id]);
     if (empty($font)) {
         return ['error' => ['msg' => Yii::t('app', 'Шрифт для фотокниги не найден')]];
     }
     $font_path = UserUrl::font(false) . DIRECTORY_SEPARATOR . UserUrl::fontFile($font->file);
     if (!file_exists($font_path)) {
         return ['error' => ['msg' => Yii::t('app', 'Файл шрифта не найден.')]];
     }
     $place_width = 350;
     $place_height = 350;
     $text_color = '#000000';
     //Ставим черный нужно брать из стиля но пока нет поля
     $image_data = Utils::makeTextImage($text, $place_width, $place_height, $text_color, $font_path, 5);
     $text_photo_id = AlphaId::id(rand(10000000000, 9999999999999));
     $text_photo_path = UserUrl::photobookPhotos(false, $this->id, $this->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($text_photo_id, UserUrl::IMAGE_ORIGINAL, 'png');
     file_put_contents($text_photo_path, $image_data);
     foreach (UserUrl::$IMAGE_SIZE as $key => $size) {
         if ($key != UserUrl::IMAGE_ORIGINAL) {
             $image = Yii::$app->image->load($text_photo_path);
             $type = Yii\image\drivers\Image::HEIGHT;
             if ($size['width'] > 0 && $size['height'] > 0) {
                 $type = Yii\image\drivers\Image::AUTO;
             }
             if ($size['width'] > 0 && $size['height'] == 0) {
                 $type = Yii\image\drivers\Image::WIDTH;
             }
             $file_resize_path = UserUrl::photobookPhotos(false, $this->id, $this->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($text_photo_id, $key, 'png');
             $image->resize($size['width'], $size['height'], $type);
             $image->save($file_resize_path);
             //$paths[]=$file_resize_path;
         }
     }
     if (empty($this->photos[$group]['photos'])) {
         $this->photos[$group]['photos'] = [];
     }
     if (empty($this->photos[$group]['texts'])) {
         $this->photos[$group]['texts'] = [];
     }
     $this->photos[$group]['photos'][] = $text_photo_id;
     $this->photos[$group]['texts'][$text_photo_id] = ['photo_id' => $text_photo_id, 'place_width' => $place_width, 'place_height' => $place_height, 'text_color' => $text_color, 'font_file' => $font->file, 'font_id' => $font->id, 'text' => $text];
     if ($this->save()) {
         $current_photo = ThumbInGroup::widget(['photobook_id' => $this->id, 'photo_id' => $text_photo_id, 'user_id' => $this->user_id]);
         return ['response' => ['status' => true, 'photos' => $this->photos, 'current_photo' => $current_photo, 'photo_id' => $text_photo_id]];
         //return ['response'=>['status'=>true, 'page'=>$this->data['pages'][$page_index]]];
     } else {
         return ['error' => ['msg' => Yii::t('app', 'Не удалось записать в базу-данных')]];
     }
 }