| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 | <?phpdeclare(strict_types=1);namespace app\command;use DirectoryIterator;use SplFileInfo;// 引入框架内置类use think\console\Command;use think\console\Input;use think\console\input\Argument;use think\console\input\Option;use think\console\Output;use think\facade\Config;use think\File;use app\model\FileManager;class FileConsole extends Command{    protected function configure()    {        // 指令配置        $this->setName('fileconsole')            ->addArgument('name', Argument::OPTIONAL, "console name")            ->addOption('dir', null, Option::VALUE_REQUIRED, 'directory name')            ->addOption('savedir', null, Option::VALUE_REQUIRED, 'savedir name')            ->setDescription('the fileconsole command');    }    protected function execute(Input $input, Output $output)    {        $name = trim($input->getArgument('name'));        $name = $name ?: 'thinkphp';        if ($input->hasOption('dir')) {            $dir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('dir'));        } else {            $dir = str_replace("\\", '/', Config::get('filesystem.disks.public.root')) . '/';        }        if ($input->hasOption('savedir')) {            $savedir = str_replace("\\", '/', app()->getRootPath() . $input->getOption('savedir'));            // 指令输出            $output->writeln('fileconsole :' . $name . "--dir: " . $dir . '--savedir' . $savedir);            $this->$name($dir, $savedir);        } else {            // 指令输出            $output->writeln('fileconsole :' . $name . "--dir: " . $dir);                $this->$name($dir);        }        $output->writeln("\n..ok");    }    public function thinkphp()    {        echo "hello thinkphp";    }    /**     * 建立文件管理目录索引(递归遍历目录)     */    public function scan($dir)    {        echo "test" . $dir;    }    /**     * 图片添加水印     */    public function markwater($dir, $savepath = 'test/cpw', $watermark = "test/watermark2.png")    {        $source = str_replace("\\", '/', app()->getRootPath() . $watermark);        $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);        if (is_dir($dir)) {            $dirs = new DirectoryIterator($dir);            foreach ($dirs as $fileInfo) {                if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {                    continue;                }                $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();                $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());                echo $filename . "\n";                echo $savename . "\n";                $image = \think\Image::open($filename);                $image->water($source, \think\Image::WATER_NORTHWEST)->save($savename);                $image = null;            }        }    }    /**     * 重置图片文件大小     */    public function resize($dir, $savepath = 'test/resize', $xmax = 1000, $ymax = 1000)    {        $savepath = str_replace("\\", '/', app()->getRootPath() . $savepath);                if (!file_exists($savepath)) {            mkdir($savepath, 0755);        }        if (is_dir($dir)) {            $dirs = new DirectoryIterator($dir);            foreach ($dirs as $fileInfo) {                if ($fileInfo->isDot() || $fileInfo->getFilename() == '.gitignore') {                    continue;                }                $filename = $dirs->getPath() . '/' . $fileInfo->getFilename();                $savename = str_replace("\\", '/', $savepath . '/' . $fileInfo->getFilename());                echo $filename . "\n";                echo $savename . "\n";                $image = \think\Image::open($filename);                $image->thumb($xmax, $ymax)->save($savename);                $image = null;            }        }    }}
 |