123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace app\command;
- use app\model\Goods;
- use app\model\GoodsDetail;
- use Intervention\Image\Gd\Driver;
- use Intervention\Image\ImageManager;
- use Intervention\Image\ImageManagerStatic;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Output\OutputInterface;
- use Tinywan\Storage\Exception\StorageException;
- class GoodsThumbCommand extends Command
- {
- protected static $defaultName = 'GoodsThumbCommand';
- protected static $defaultDescription = 'GoodsThumbCommand';
- /**
- * @return void
- */
- protected function configure()
- {
- $this->addArgument('name', InputArgument::OPTIONAL, '商品主图片批量缩略');
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- * @return int
- */
- // protected function execute(InputInterface $input, OutputInterface $output): int
- // {
- // $goods = Goods::select('goods_id','goods_cover')->skip(10)->limit(10)->get()->toArray();
- //
- // foreach ($goods as $good){
- // if (!file_exists(public_path($good['goods_cover']))){
- // echo "【".$good['goods_id']."】源文件不存在,跳过 \n";
- // continue;
- // }
- // $fileNameArray = explode(DIRECTORY_SEPARATOR,$good['goods_cover']);
- // $two = $fileNameArray[count($fileNameArray)-2];
- // if ($two == 'thumb'){
- // echo "【".$good['goods_id']."】已存在,跳过 \n";
- // continue;
- // }
- // array_splice($fileNameArray,-1,0,'thumb');
- //
- // $thumbPath = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$fileNameArray),DIRECTORY_SEPARATOR));
- // if (file_exists($thumbPath)){
- // echo "【".$good['goods_id']."】缩略图已存在,跳过 \n";
- // continue;
- // }
- // $path = array_slice($fileNameArray,0,-1);
- // $pathStr = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$path),DIRECTORY_SEPARATOR));
- //
- // if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)){
- // throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
- // }
- //
- // $image = ImageManagerStatic::make(public_path($good['goods_cover']));
- //
- // $imgWidth = $image->width();
- // $imgHeight = $image->height();
- // $rate = round($imgWidth / 200, 2);
- // $height = intval($imgHeight / $rate);
- // $image = $image->resize(200, $height);
- // $encoded = $image->encode('jpg');
- // $encoded->save($thumbPath);
- //
- // Goods::where('goods_id',$good['goods_id'])->update(['goods_cover'=>implode(DIRECTORY_SEPARATOR,$fileNameArray)]);
- //
- // echo "【".$good['goods_id']."】已完成 \n";
- // }
- //
- //
- //
- // return self::SUCCESS;
- // }
- protected function execute(InputInterface $input, OutputInterface $output): int
- {
- $details = GoodsDetail::select('join_detail_goods_id', 'goods_detail_slider_json')
- ->skip(0)
- ->limit(1)
- ->get()
- ->toArray();
- foreach ($details as $detail) {
- if (!empty($detail['goods_detail_slider_json'])) {
- $sliderJson = json_decode($detail['goods_detail_slider_json'], true);
- $sliders = explode(',', $sliderJson['slider']);
- $sliderData = [];
- foreach ($sliders as $slider) {
- if (!file_exists(public_path($slider))) {
- $sliderData[] = $slider;
- echo "【" . $slider . "】源文件不存在,跳过 \n";
- continue;
- }
- $fileNameArray = explode('/', $slider);
- $two = $fileNameArray[count($fileNameArray) - 2];
- if ($two == 'thumb') {
- echo "【" . $detail['join_detail_goods_id'] . "】已存在,跳过 \n";
- continue;
- }
- array_splice($fileNameArray, -1, 0, 'thumb');
- $thumbPath = public_path(ltrim(implode('/', $fileNameArray), '/'));
- if (file_exists($thumbPath)) {
- $sliderData[] = implode('/', $fileNameArray);
- echo "【" . $detail['join_detail_goods_id'] . "】缩略图已存在,跳过 \n";
- continue;
- }
- $path = array_slice($fileNameArray, 0, -1);
- $pathStr = public_path(ltrim(implode('/', $path), '/'));
- if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)) {
- throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
- }
- $image = ImageManagerStatic::make(public_path($slider));
- $imgWidth = $image->width();
- $imgHeight = $image->height();
- $rate = round($imgWidth / 200, 2);
- $height = intval($imgHeight / $rate);
- $image = $image->resize(200, $height);
- $encoded = $image->encode('jpg');
- $encoded->save($thumbPath);
- $sliderData[] = implode('/', $fileNameArray);
- }
- if (empty($sliderData)){
- echo "【" . $detail['join_detail_goods_id'] . "】跳过 \n";
- continue;
- }
- GoodsDetail::where('join_detail_goods_id',$detail['join_detail_goods_id'])
- ->update(['goods_detail_slider_json'=>json_encode(['slider'=>implode(',',$sliderData)])]);
- echo "【" . $detail['join_detail_goods_id'] . "】已完成 \n";
- }
- }
- return self::SUCCESS;
- }
- }
|