GoodsThumbCommand.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\command;
  3. use app\model\Goods;
  4. use app\model\GoodsDetail;
  5. use Intervention\Image\Gd\Driver;
  6. use Intervention\Image\ImageManager;
  7. use Intervention\Image\ImageManagerStatic;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Input\InputOption;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Tinywan\Storage\Exception\StorageException;
  14. class GoodsThumbCommand extends Command
  15. {
  16. protected static $defaultName = 'GoodsThumbCommand';
  17. protected static $defaultDescription = 'GoodsThumbCommand';
  18. /**
  19. * @return void
  20. */
  21. protected function configure()
  22. {
  23. $this->addArgument('name', InputArgument::OPTIONAL, '商品主图片批量缩略');
  24. }
  25. /**
  26. * @param InputInterface $input
  27. * @param OutputInterface $output
  28. * @return int
  29. */
  30. // protected function execute(InputInterface $input, OutputInterface $output): int
  31. // {
  32. // $goods = Goods::select('goods_id','goods_cover')->skip(10)->limit(10)->get()->toArray();
  33. //
  34. // foreach ($goods as $good){
  35. // if (!file_exists(public_path($good['goods_cover']))){
  36. // echo "【".$good['goods_id']."】源文件不存在,跳过 \n";
  37. // continue;
  38. // }
  39. // $fileNameArray = explode(DIRECTORY_SEPARATOR,$good['goods_cover']);
  40. // $two = $fileNameArray[count($fileNameArray)-2];
  41. // if ($two == 'thumb'){
  42. // echo "【".$good['goods_id']."】已存在,跳过 \n";
  43. // continue;
  44. // }
  45. // array_splice($fileNameArray,-1,0,'thumb');
  46. //
  47. // $thumbPath = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$fileNameArray),DIRECTORY_SEPARATOR));
  48. // if (file_exists($thumbPath)){
  49. // echo "【".$good['goods_id']."】缩略图已存在,跳过 \n";
  50. // continue;
  51. // }
  52. // $path = array_slice($fileNameArray,0,-1);
  53. // $pathStr = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$path),DIRECTORY_SEPARATOR));
  54. //
  55. // if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)){
  56. // throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
  57. // }
  58. //
  59. // $image = ImageManagerStatic::make(public_path($good['goods_cover']));
  60. //
  61. // $imgWidth = $image->width();
  62. // $imgHeight = $image->height();
  63. // $rate = round($imgWidth / 200, 2);
  64. // $height = intval($imgHeight / $rate);
  65. // $image = $image->resize(200, $height);
  66. // $encoded = $image->encode('jpg');
  67. // $encoded->save($thumbPath);
  68. //
  69. // Goods::where('goods_id',$good['goods_id'])->update(['goods_cover'=>implode(DIRECTORY_SEPARATOR,$fileNameArray)]);
  70. //
  71. // echo "【".$good['goods_id']."】已完成 \n";
  72. // }
  73. //
  74. //
  75. //
  76. // return self::SUCCESS;
  77. // }
  78. protected function execute(InputInterface $input, OutputInterface $output): int
  79. {
  80. $details = GoodsDetail::select('join_detail_goods_id', 'goods_detail_slider_json')
  81. ->skip(0)
  82. ->limit(1)
  83. ->get()
  84. ->toArray();
  85. foreach ($details as $detail) {
  86. if (!empty($detail['goods_detail_slider_json'])) {
  87. $sliderJson = json_decode($detail['goods_detail_slider_json'], true);
  88. $sliders = explode(',', $sliderJson['slider']);
  89. $sliderData = [];
  90. foreach ($sliders as $slider) {
  91. if (!file_exists(public_path($slider))) {
  92. $sliderData[] = $slider;
  93. echo "【" . $slider . "】源文件不存在,跳过 \n";
  94. continue;
  95. }
  96. $fileNameArray = explode('/', $slider);
  97. $two = $fileNameArray[count($fileNameArray) - 2];
  98. if ($two == 'thumb') {
  99. echo "【" . $detail['join_detail_goods_id'] . "】已存在,跳过 \n";
  100. continue;
  101. }
  102. array_splice($fileNameArray, -1, 0, 'thumb');
  103. $thumbPath = public_path(ltrim(implode('/', $fileNameArray), '/'));
  104. if (file_exists($thumbPath)) {
  105. $sliderData[] = implode('/', $fileNameArray);
  106. echo "【" . $detail['join_detail_goods_id'] . "】缩略图已存在,跳过 \n";
  107. continue;
  108. }
  109. $path = array_slice($fileNameArray, 0, -1);
  110. $pathStr = public_path(ltrim(implode('/', $path), '/'));
  111. if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)) {
  112. throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
  113. }
  114. $image = ImageManagerStatic::make(public_path($slider));
  115. $imgWidth = $image->width();
  116. $imgHeight = $image->height();
  117. $rate = round($imgWidth / 200, 2);
  118. $height = intval($imgHeight / $rate);
  119. $image = $image->resize(200, $height);
  120. $encoded = $image->encode('jpg');
  121. $encoded->save($thumbPath);
  122. $sliderData[] = implode('/', $fileNameArray);
  123. }
  124. if (empty($sliderData)){
  125. echo "【" . $detail['join_detail_goods_id'] . "】跳过 \n";
  126. continue;
  127. }
  128. GoodsDetail::where('join_detail_goods_id',$detail['join_detail_goods_id'])
  129. ->update(['goods_detail_slider_json'=>json_encode(['slider'=>implode(',',$sliderData)])]);
  130. echo "【" . $detail['join_detail_goods_id'] . "】已完成 \n";
  131. }
  132. }
  133. return self::SUCCESS;
  134. }
  135. }