FileConsole.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\command;
  4. use DirectoryIterator;
  5. use SplFileInfo;
  6. // 引入框架内置类
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\input\Argument;
  10. use think\console\input\Option;
  11. use think\console\Output;
  12. use think\facade\Config;
  13. use think\File;
  14. use app\model\FileManager;
  15. class FileConsole extends Command
  16. {
  17. protected function configure()
  18. {
  19. // 指令配置
  20. $this->setName('fileconsole')
  21. ->addArgument('name', Argument::OPTIONAL, "console name")
  22. ->addOption('dir', null, Option::VALUE_REQUIRED, 'directory name')
  23. ->addOption('savedir', null, Option::VALUE_REQUIRED, 'savedir name')
  24. ->setDescription('the fileconsole command');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $name = trim($input->getArgument('name'));
  29. $name = $name ?: 'thinkphp';
  30. if ($input->hasOption('dir')) {
  31. $dir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('dir'));
  32. } else {
  33. $dir = str_replace("\\", '/', Config::get('filesystem.disks.public.root')) . '/';
  34. }
  35. if ($input->hasOption('savedir')) {
  36. $savedir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('savedir'));
  37. // 指令输出
  38. $output->writeln('fileconsole :' . $name . "--dir: " . $dir . '--savedir' . $savedir);
  39. $this->$name($dir, $savedir);
  40. } else {
  41. // 指令输出
  42. $output->writeln('fileconsole :' . $name . "--dir: " . $dir);
  43. $this->$name($dir);
  44. }
  45. $output->writeln("\n..ok");
  46. }
  47. public function thinkphp()
  48. {
  49. echo "hello thinkphp";
  50. }
  51. /**
  52. * 建立文件管理目录索引(递归遍历目录)
  53. */
  54. public function scan($dir)
  55. {
  56. echo "test" . $dir;
  57. }
  58. /**
  59. * 图片添加水印
  60. */
  61. public function markwater($dir, $savepath = 'test/cpw', $watermark = "test/watermark2.png")
  62. {
  63. $source = str_replace("\\", '/', app()->getRootPath() . $watermark);
  64. $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);
  65. if (is_dir($dir)) {
  66. $dirs = new DirectoryIterator($dir);
  67. foreach ($dirs as $fileInfo) {
  68. if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
  69. continue;
  70. }
  71. $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();
  72. $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());
  73. echo $filename . "\n";
  74. echo $savename . "\n";
  75. $image = \think\Image::open($filename);
  76. $image->water($source, \think\Image::WATER_NORTHWEST)->save($savename);
  77. $image = null;
  78. }
  79. }
  80. }
  81. /**
  82. * 重置图片文件大小
  83. */
  84. public function resize($dir, $savepath = 'test/resize', $xmax = 1000, $ymax = 1000)
  85. {
  86. $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);
  87. if (!file_exists($savepath)) {
  88. mkdir($savepath, 0755);
  89. }
  90. if (is_dir($dir)) {
  91. $dirs = new DirectoryIterator($dir);
  92. foreach ($dirs as $fileInfo) {
  93. if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {
  94. continue;
  95. }
  96. $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();
  97. $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());
  98. echo $filename . "\n";
  99. echo $savename . "\n";
  100. $image = \think\Image::open($filename);
  101. $image->thumb($xmax, $ymax)->save($savename);
  102. $image = null;
  103. }
  104. }
  105. }
  106. }