完整代码:鼠标右键实现命令行裁剪图片及添加水印代码,视频演示地址:
https://www.ixigua.com/7027055168101286437
#!/usr/bin/env php
<?php
//教程1 https://micuu.com/new/2305.html
// cmd /c D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\Desktop\index.php -a=%1
// echo '您输入的信息是:'.$name."\r\n";
// composer require intervention/image //下载完成后运行命令
// http://image.intervention.io/use/basics //官方文档地址
// $argv 获得所有空格分隔的参数列表
$arg = getopt("a:");
print_r($arg['a'].'\n');
$path = $arg["a"];
require __DIR__.'/vendor/autoload.php';
use Intervention\Image\ImageManagerStatic as Image;
//需要用户输入的字段
$needInput = [
'width' => "",
'height' => "",
'logo' => "",
];
//字段提示 注意:其中 key 需要和 needinput数组中的 key 对应
$outMsg = [
'width' => "宽度",
'height' => "高度",
'logo' => "水印",
];
$fs = true; //用户输入状态 当用户灭有输入的时候是true,输入后是false
foreach($needInput as $k => $v){
do{
if($fs){
fwrite(STDOUT,$outMsg[$k]);
$fs = false;
}else{
fwrite(STDOUT,"抱歉,{$outMsg[$k]} 不能为空,请重新输入{$outMsg[$k]}:");
}
$needInput[$k] = trim(fgets(STDIN));
if($needInput[$k] != ''){
$fs = true;
}
}while($needInput[$k] == '');
}
print_r($needInput);
// and you are ready to go ...
$img = Image::make($path)->resize($needInput["width"], $needInput["height"]);
// save file as jpg with medium quality
$res = $img->save($path."副本.jpg", 100);
if($res == true){
echo "图片处理成功";
}else{
echo "图片处理失败";
}
// // create new Intervention Image
// $img = Image::make('public/foo.jpg');
// // paste another image
// $img->insert('public/bar.png');
// // create a new Image instance for inserting
// $watermark = Image::make('public/watermark.png');
// $img->insert($watermark, 'center');
// // insert watermark at bottom-right corner with 10px offset
// $img->insert('public/watermark.png', 'bottom-right', 10, 10);