public function run() { $image_posX = 0; $image_posY = 0; /* $scale=2; $image_real_width=$this->place_width*$scale; $image_real_height=$this->place_height*$scale;*/ //$img_path=UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id).'/'.UserUrl::imageFile($this->photo_id, $this->image_size, $this->ext); //$size=getimagesize($img_path); //$img_path=UserUrl::photobookPhotos(false, $this->photobook_id, $this->user_id).'/'.UserUrl::imageFile($this->photo_id, $this->image_size); $image_real_width = $this->real_width; //$size[0]; $image_real_height = $this->real_height; //$size[1]; $thumb_width = $this->place_width; $thumb_height = $this->place_height; $width = $image_real_width; $height = $image_real_height; $original_aspect = $width / $height; $thumb_aspect = $thumb_width / $thumb_height; if ($original_aspect >= $thumb_aspect) { // If image is wider than thumbnail (in aspect ratio sense) $new_height = $thumb_height; $new_width = $width / ($height / $thumb_height); } else { // If the thumbnail is wider than the image $new_width = $thumb_width; $new_height = $height / ($width / $thumb_width); } $scale = $this->scale; $image_posX = 0 - ($new_width * $scale - $thumb_width) / 2; $image_posY = 0 - ($new_height * $scale - $thumb_height) / 2; $image_real_width = $new_width; $image_real_height = $new_height; $image_posX = $image_posX - $this->pos_dx; $image_posY = $image_posY - $this->pos_dy; if ($image_posX > 0) { $image_posX = 0; } if ($image_posY > 0) { $image_posY = 0; } if ($image_posX < 0 - $image_real_width * $scale + $thumb_width) { $image_posX = 0 - $image_real_width * $scale + $thumb_width; } if ($image_posY < 0 - $image_real_height * $scale + $thumb_height) { $image_posY = 0 - $image_real_height * $scale + $thumb_height; } $img_url = UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id) . '/' . UserUrl::imageFile($this->photo_id, $this->image_size, $this->ext); $img_path = UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id) . '/' . UserUrl::imageFile($this->photo_id, $this->image_size, $this->ext); $uid = rand(1, 99999999999999999); $lastModified = $this->mtime; //filemtime($img_path); if ($this->view == 'svg') { $border = ''; if ($this->image_size !== UserUrl::IMAGE_ORIGINAL) { $border = '<path stroke-width="0.5" fill="transparent" stroke="' . $this->passpartu_left_top_border_color . '" d="M 0 {height} L 0 0 L {width} 0"/> <path stroke-width="0.5" fill="transparent" stroke="' . $this->passpartu_right_bottom_border_color . '" d="M {width} 0 L {width} {height} L 0 {height}"/>'; } $content = '<g xmlns:xlink="http://www.w3.org/1999/xlink" class="placeholder" transform="translate(-{w2}, -{h2})" width="{width}" height="{height}" clip-path="url(#clip-path-{uid})" > <defs> <clipPath id="clip-path-{uid}"> <path d="M 0 0 h {width} v {height} h -{width} v -{height} z"/> </clipPath> </defs> <path fill="#ffffff" fill-opacity="1" clip-path="url(#clip-path-{uid})" d="M 0 0 h {width} v {height} h -{width} v -{height} z"/> <g > <g transform="translate({img_pos_x},{img_pos_y}) scale({scale},{scale})" width="{width}" height="{height}" pointer-events="none"> <image width="{image_real_width}" height="{image_real_height}" xlink:href="{img_url}"/> </g> ' . $border . ' </g> </g>'; //stroke-opacity="0.5" $img_url = $img_url . '?v=' . $lastModified; $mime_type = "image/jpeg"; if ($this->ext == 'png') { $mime_type = "image/png"; } if ($this->image_size == UserUrl::IMAGE_THUMB || $this->image_size == UserUrl::IMAGE_SMALL) { // $img_path=UserUrl::photobookPhotos(false, $this->photobook_id, $this->user_id).'/'.UserUrl::imageFile($this->photo_id, UserUrl::IMAGE_SMALL); $imageData = base64_encode(file_get_contents($img_path)); $img_url = 'data:' . $mime_type . ';base64,' . $imageData; } else { if ($this->image_size == UserUrl::IMAGE_ORIGINAL) { $imageData = base64_encode(file_get_contents($img_path)); $img_url = 'data:' . $mime_type . ';base64,' . $imageData; //$img_url='http://'.$_SERVER['HTTP_HOST'].UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id).'/'.UserUrl::imageFile($this->photo_id, $this->image_size);; } } echo Yii::t('app', $content, ['width' => $this->place_width, 'height' => $this->place_height, 'uid' => $uid, 'image_width' => $this->image_width, 'image_height' => $this->image_height, 'image_real_width' => $image_real_width, 'image_real_height' => $image_real_height, 'first_image_real_width' => $image_real_width, 'first_image_real_height' => $image_real_height, 'scale' => $scale, 'img_url' => $img_url, 'w2' => $this->place_width / 2, 'h2' => $this->place_height / 2, 'img_pos_x' => $image_posX, 'img_pos_y' => $image_posY]); } else { if ($this->view == 'json') { echo json_encode(['width' => $this->place_width, 'height' => $this->place_height, 'uid' => $uid, 'image_width' => $this->image_width, 'image_height' => $this->image_height, 'image_real_width' => $image_real_width, 'image_real_height' => $image_real_height, 'first_image_real_width' => $image_real_width, 'first_image_real_height' => $image_real_height, 'scale' => $scale, 'img_url' => $img_url . '?v=' . $lastModified, 'photo_id' => $this->photo_id, 'w2' => $this->place_width / 2, 'h2' => $this->place_height / 2, 'img_pos_x' => $image_posX, 'img_pos_y' => $image_posY, 'ext' => $this->ext, 'last_modified' => $lastModified]); } } }
public function actionLayouts() { $this->layout = 'layouts'; $ref = Yii::$app->request->get('ref'); $id = Yii::$app->request->get('id'); //$selected_style_id= Yii::$app->request->get('style_id', 0); $reset = Yii::$app->request->get('reset', 0); if (!empty($ref) && !empty($id)) { $user_id = AlphaId::id($ref, true); $pb_id = AlphaId::id($id, true); $model = new PhotobookForm(); if (!$model->loadById($pb_id)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Фотокнига не найдена')); $this->redirect(Url::toRoute(['photobooks/index'])); return; } //$styles=Style::find()->where(['delete'=>0])->all(); if (empty($model->data)) { $model->data = []; } if (empty($model->data['processed'])) { $model->data['processed'] = []; } if ($reset == 1) { $pages = $model->generatePages($model->style_id); if (empty($pages)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Необходимо загрузить как минимум по одному фото в группу')); $this->redirect(Url::toRoute(['photobooks/upload-photos', 'id' => $id, 'ref' => $ref])); return; } $model->data['pages'] = $pages; } else { $pages = $model->updatePages($model->style_id); if (empty($pages)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Необходимо загрузить как минимум по одному фото в группу')); $this->redirect(Url::toRoute(['photobooks/upload-photos', 'id' => $id, 'ref' => $ref])); } $model->data['pages'] = $pages; } //$model->style_id=$selected_style_id; if (!$model->save()) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Не удалось сохранить')); $this->redirect(Url::toRoute(['photobooks/index'])); } /* if($reset==1){ $this->redirect(Url::toRoute(['photobooks/layouts', 'ref'=>$ref, 'id'=>$id])); }*/ $photos = []; $groups = $model->photos; foreach ($groups as $group_name => $group) { foreach ($group['photos'] as $key => $photo_id) { $mtime = UserUrl::photobookPhotos(false, $model->id, $model->user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, UserUrl::IMAGE_THUMB); $photos[] = ['photo_id' => $photo_id, 'mtime' => $mtime]; } } //$model->data['processed'] return $this->redirect(Url::toRoute(['photobooks/edit', 'ref' => $ref, 'id' => $id])); // return $this->render('layouts', ['model'=>$model, 'ref'=>$ref, 'id'=>$id, 'pb_id'=>$pb_id, 'user_id'=>$user_id, 'pages'=>$pages, 'styles'=>$styles, 'photos'=>$photos, 'processed'=>$model->data['processed']]); } else { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Страница не найдена')); $this->redirect(Url::toRoute(['photobooks/index'])); } }
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', 'Не удалось записать в базу-данных')]]; } }
?> <?php $photo_id = $photo['photo_id']; ?> <?php $mtime = $photo['mtime']; ?> <div data-id="<?php echo $photo_id; ?> " class="all-thumb photo_<?php echo $photo_id; ?> " > <img src="<?php echo UserUrl::photobookPhotos(true, $model->id, $model->user_id) . '/' . UserUrl::imageFile($photo_id, UserUrl::IMAGE_THUMB) . '?v=' . $mtime; ?> " data-id="<?php echo $photo_id; ?> " > <a title="" data-placement="top" data-toggle="tooltip" class="badge tooltips <?php if (!empty($processed[$photo_id]) && $processed[$photo_id]) { echo 'active'; } ?> " data-original-title="Обработан" > <i class="fa fa-check"></i> </a> </div> <?php }
public function actionImageRotate() { $result = []; $this->layout = 'json'; $ref = Yii::$app->request->get('ref'); $id = Yii::$app->request->get('id'); $photo_id = Yii::$app->request->get('photo_id', ''); $deg = intval(Yii::$app->request->get('deg', 0)); $page = Yii::$app->request->get('page', -1); $place_num = intval(Yii::$app->request->get('place_num', -1)); if (!empty($id)) { $model = new PhotobookForm(); $user_id = AlphaId::id($ref, true); $pb_id = AlphaId::id($id, true); if ($model->loadById($pb_id)) { $file_path = UserUrl::photobookPhotos(false, $pb_id, $user_id); $paths = []; $paths[] = $file_path; $ext = 'jpg'; if ($model->isText($photo_id)) { $ext = 'png'; } $photo_o_path = $file_path . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, UserUrl::IMAGE_ORIGINAL, $ext); $image = Yii::$app->image->load($photo_o_path); $image->rotate($deg); $image->save($photo_o_path); foreach (UserUrl::$IMAGE_SIZE as $key => $size) { if ($key != UserUrl::IMAGE_ORIGINAL) { $image = Yii::$app->image->load($photo_o_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, $pb_id, $user_id) . DIRECTORY_SEPARATOR . UserUrl::imageFile($photo_id, $key, $ext); $image->resize($size['width'], $size['height'], $type); $image->save($file_resize_path); unset($image); $image = null; } } if ($page < 0 || $place_num < 0) { $result = ['response' => ['status' => true, 'photo_id' => $photo_id]]; } else { $result = $model->setImagePosAndScale($page, $place_num, 0, 0, 1); } $last_modified = filemtime($photo_o_path); if (!empty($result['response'])) { $result['response']['photo_id'] = $photo_id; $result['response']['page_index'] = $page; $result['response']['place_num'] = $place_num; $result['response']['last_modified'] = $last_modified; } } else { $result = ['error' => ['msg' => Yii::t('app', 'Фотокнига не найдена')]]; } } else { $result = ['error' => ['msg' => Yii::t('app', 'Фотокнига не найдена')]]; } return $this->render('json', ['result' => $result]); }
public function run() { $img = Html::img(UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id) . '/' . UserUrl::imageFile($this->photo_id, UserUrl::IMAGE_THUMB), ['data-id' => $this->photo_id]); $a = Html::a($img, UserUrl::photobookPhotos(true, $this->photobook_id, $this->user_id) . '/' . UserUrl::imageFile($this->photo_id, UserUrl::IMAGE_MIDDLE), ['class' => 'thumb thumb-' . $this->photo_id, 'data-id' => $this->photo_id, 'data-gallery' => '']); echo $a; }