MediaLibraryService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Services;
  3. use Carbon\Carbon;
  4. use Exception;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Str;
  7. use Intervention\Image\Facades\Image;
  8. use App\Services\CallApiService;
  9. use Intervention\Image\ImageManagerStatic;
  10. class MediaLibraryService
  11. {
  12. private $callApiService;
  13. private $gateToken;
  14. public function __construct(CallApiService $callApiService)
  15. {
  16. $this->callApiService = $callApiService;
  17. }
  18. public function setGateToken($gateToken)
  19. {
  20. $this->gateToken = $gateToken;
  21. }
  22. public function getCurrSetupFilePath($setup): string
  23. {
  24. $monthYearFolder = '';
  25. $subDir = '';
  26. if ($setup['SubDir']) {
  27. $subDir = $setup['BrandCode'] . '/';
  28. }
  29. if ($setup['DateFolderType'] === '0') {
  30. $monthYearFolder = date('/Y/m/');
  31. } else if ($setup['DateFolderType'] === '1') {
  32. $monthYearFolder = date('/Y/m/d/');
  33. }
  34. return '/uploads' . $monthYearFolder . $subDir;
  35. }
  36. public function getSetup($brandCode)
  37. {
  38. $response = $this->callApiService->callApi([
  39. 'url' => 'setup-get',
  40. 'data' => [
  41. 'SetupCode' => 'media-body',
  42. 'BrandCode' => $brandCode
  43. ],
  44. 'headers' => $this->callApiService->getHeader($this->gateToken)
  45. ]);
  46. if ($this->callApiService->verifyApiError($response)) {
  47. throw new Exception('Dabory Api Error', $response['apiStatus']);
  48. }
  49. $response['BrandCode'] = $brandCode;
  50. return $response;
  51. }
  52. public function lastSlipNoGet(): string
  53. {
  54. $response = $this->callApiService->callApi([
  55. 'url' => 'last-slip-no-get',
  56. 'data' => [
  57. 'TableName' => 'common/media',
  58. 'YYMMDD' => Carbon::now()->format('ymd'),
  59. ],
  60. 'headers' => $this->callApiService->getHeader($this->gateToken)
  61. ]);
  62. if ($this->callApiService->verifyApiError($response)) {
  63. throw new Exception('Dabory Api Error', $response['apiStatus']);
  64. }
  65. return Carbon::now()->format('ymd') . '-' . $response['LastSlipNo'];
  66. }
  67. public function makeGifBd($file, $media)
  68. {
  69. $setup = $media['setup'];
  70. $fullName = $media['name'];
  71. $extension = $file->extension();
  72. // $extension = $file->extension();
  73. $onlyName = explode('.' . $extension, $fullName)[0];
  74. $bdPage = [];
  75. $reSizeList = collect($setup)->except(['IsCutThumbImage', 'DateFolderType', 'BrandCode', 'SubDir'])->toArray();
  76. foreach ($reSizeList as $key => $item) {
  77. if ($item['Width'] === 0 || $item['Height'] === 0) {
  78. continue;
  79. }
  80. if (Str::contains(strtolower($key), 'mob')) {
  81. $mob = preg_replace('/_size$/', '', Str::snake($key));
  82. $filePath = "{$onlyName}_${$mob}.{$extension}";
  83. } else {
  84. $filePath = "{$onlyName}_{$item['Width']}x{$item['Height']}.{$extension}";
  85. }
  86. $toFilePath = "{$media['path']}{$filePath}";
  87. $file->storeAs($media['path'], $filePath, ['disk' => getDisk()]);
  88. array_push($bdPage, [
  89. 'ImageType' => explode('size', strtolower($key))[0],
  90. 'BdFileUrl' => $toFilePath,
  91. 'BdFileSize' => round($file->getSize() / 1024),
  92. 'BdWidth' => $item['Width'],
  93. 'BdHeight' => $item['Height'],
  94. ]);
  95. }
  96. return $bdPage;
  97. }
  98. public function makeImageBd($file, $media, $isCropImage = true)
  99. {
  100. $setup = $media['setup'];
  101. $fullName = $media['name'];
  102. $extension = $file->extension();
  103. // $extension = $file->extension();
  104. $onlyName = explode('.' . $extension, $fullName)[0];
  105. $bdPage = [];
  106. $reSizeList = collect($setup)->except(['IsCutThumbImage', 'IsCutThumbMobImage', 'DateFolderType', 'BrandCode', 'SubDir', 'IsMonthYearFolder'])->toArray();
  107. foreach ($reSizeList as $key => $item) {
  108. if ($item['Width'] === 0 || $item['Height'] === 0) {
  109. continue;
  110. }
  111. // $img = Image::make($file)
  112. // ->resize($item['Width'], null, function ($constraint) {
  113. // $constraint->aspectRatio();
  114. // });
  115. $img = Image::make($file)
  116. ->resize($item['Width'], null, function ($constraint) {
  117. $constraint->aspectRatio();
  118. })->encode('webp');
  119. // if ($key == 'ThumbSize' && !$setup['IsCutThumbImage']) {
  120. // $img = Image::make($file)
  121. // ->resize($item['Width'], null, function ($constraint) {
  122. // $constraint->aspectRatio();
  123. // });
  124. // } else {
  125. // $img = Image::make($file)
  126. // ->resize($item['Width'], $item['Height']);
  127. // }
  128. if (Str::contains(strtolower($key), 'mob')) {
  129. $mob = preg_replace('/_size$/', '', Str::snake($key));
  130. $filePath = "{$onlyName}_{$mob}.webp";
  131. } else {
  132. $filePath = "{$onlyName}_{$img->width()}x{$img->height()}.webp";
  133. }
  134. // $filePath = "{$onlyName}_{$mob}{$img->width()}x{$img->height()}.{$extension}";
  135. $formPath = "{$media['path']}Resize_{$filePath}";
  136. $toFilePath = "{$media['path']}{$filePath}";
  137. // 로컬 일 때
  138. // $img->save(Storage::disk(getDisk())->path($formPath));
  139. $fileSize = 0;
  140. $bdWidth = 0;
  141. $bdHeight = 0;
  142. if ($isCropImage) {
  143. // S3 일 떄
  144. $img->encode();
  145. Storage::disk(getDisk())->put($formPath, $img->__toString(), ['visibility' => 'public']);
  146. //
  147. $fileSize = round($img->filesize() / 1024);
  148. $bdWidth = $img->width();
  149. $bdHeight = $img->height();
  150. if (Storage::disk(getDisk())->exists($toFilePath)) {
  151. Storage::disk(getDisk())->delete($toFilePath);
  152. }
  153. Storage::disk(getDisk())->move($formPath, $toFilePath);
  154. }
  155. array_push($bdPage, [
  156. 'ImageType' => explode('size', strtolower($key))[0],
  157. 'BdFileUrl' => $toFilePath,
  158. 'BdFileSize' => $fileSize,
  159. 'BdWidth' => $bdWidth,
  160. 'BdHeight' => $bdHeight,
  161. ]);
  162. }
  163. return $bdPage;
  164. }
  165. }