test.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Imagick;
  6. use ImagickDraw;
  7. use ImagickPixel;
  8. class Test extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'test:test';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Test.';
  22. /**
  23. * Create a new command instance.
  24. *
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. // dump(json_decode('[{"title":"首页","url":"https:\/\/vienblog.com"},{"title":"关于作者","url":"https:\/\/vienblog.com"},{"title":"打赏作者","url":"https:\/\/viencoding.com\/pay"},{"title":"建站教程","url":"https:\/\/vienblog.com"},{"title":"科学上网","url":"https:\/\/viencoding.com\/article\/122"},{"title":"网站导航","url":"https:\/\/vienblog.com"},{"title":"机器学习","url":"https:\/\/vienblog.com\/category\/机器学习"},{"title":"Laravel教程","url":"https:\/\/vienblog.com"},{"title":"Python教程","url":"https:\/\/vienblog.com"},{"title":"Git教程","url":"https:\/\/vienblog.com"},{"title":"Docker教程","url":"https:\/\/vienblog.com"},{"title":"友情链接","url":"https:\/\/vienblog.com"}]'));
  38. $str="Vien@vienblog.com";
  39. $imgPath = "/Users/vien/PhpstormProjects/laravel-blog/public/images/avatars/lbxx-3.jpg";
  40. // $imgPath = "/Users/vien/PhpstormProjects/laravel-blog/public/images/avatars/ttt.png";
  41. $savePath = "public/bar.jpg";
  42. // watermark($imgPath, $savePath, $str);
  43. $this->tree(storage_path('app/public'));
  44. }
  45. public function tree($directory)
  46. {
  47. $mydir = dir($directory);
  48. while($file = $mydir->read())
  49. {
  50. if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
  51. {
  52. // echo "$file\n";
  53. $this->tree("$directory/$file");
  54. }
  55. else
  56. // echo "$file\n";
  57. if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ['jpeg', 'jpg', 'png'])) {
  58. echo "$directory/$file\n";
  59. watermark("$directory/$file", "$directory/$file", "vienblog.com");
  60. }
  61. }
  62. $mydir->close();
  63. }
  64. // public function addWatermark()
  65. // {
  66. //
  67. // }
  68. public function water($imgPath, $savePath, $str)
  69. {
  70. $ext = strtolower(pathinfo($imgPath, PATHINFO_EXTENSION));
  71. if ($ext == "png") {
  72. $im = imagecreatefrompng($imgPath);
  73. } else if (in_array($ext, ['jpeg', 'jpg'])) {
  74. $im = imagecreatefromjpeg($imgPath);
  75. } else {
  76. // 格式不是png和jpg不加水印
  77. return;
  78. }
  79. // 载入字体zt.ttf
  80. $fnt = resource_path("/fonts/fzsj.ttf");
  81. // 文字相对于高度或者宽度的比例
  82. $scale = 0.04;
  83. // 设置颜色,用于文字字体的白和阴影的黑
  84. $white=imagecolorallocate($im,255,255,255);
  85. $black=imagecolorallocate($im,50,50,50);
  86. // 计算文字大小和位置
  87. $imgInfo = getimagesize($imgPath);
  88. $width = $imgInfo[0];
  89. $height = $imgInfo[1];
  90. $textSize = ($height + $width) / 2 * $scale;
  91. $bbox = imagettfbbox($textSize, 0, $fnt, $str);
  92. $textWidth = abs($bbox[2] - $bbox[0]);
  93. $textHeight = abs($bbox[3] - $bbox[5]);
  94. $top = $height - $textHeight / 1.8;
  95. $left = $width - $textSize - $textWidth;
  96. // 超出画幅不加水印
  97. if ($top < 0 || $left < 0) return;
  98. //在图片中添加文字,imagettftext (image,size,angle, x, y,color,fontfile,text)
  99. imagettftext($im,$textSize, 0, $left+1, $top+1, $black, $fnt, $str);
  100. imagettftext($im,$textSize, 0, $left, $top, $white, $fnt, $str);
  101. //将$im输出
  102. ImageJpeg($im, $savePath);
  103. //销毁$im对象
  104. ImageDestroy($im);
  105. }
  106. public function water2() {
  107. $canvas = new Imagick();
  108. $canvas->newImage(500, 200, 'white');
  109. $canvas->setImageFormat('png');
  110. $text = new Imagick();
  111. $text->newImage(500, 200, 'none');
  112. $text->setImageFormat('png');
  113. $draw = new ImagickDraw();
  114. $draw->setFillColor(new ImagickPixel('#f00'));
  115. $draw->setFontSize(50);
  116. $draw->annotation(100, 60, 'welcome');
  117. $text->drawImage($draw);
  118. $draw->setFillColor(new ImagickPixel('#fff'));
  119. $draw->setFontSize(50);
  120. $draw->annotation(100, 120, 'welcome');
  121. $text->drawImage($draw);
  122. // $shadow_layer = $text->clone();
  123. $shadow_layer = clone $text;
  124. $shadow_layer->setImageBackgroundColor(new ImagickPixel('#555'));
  125. $shadow_layer->shadowImage(80, 0.8, 0, 0);
  126. $shadow_layer->compositeImage($text, Imagick::COMPOSITE_OVER, 0, 0 );
  127. $canvas->compositeImage($shadow_layer, imagick::COMPOSITE_DEFAULT, 0, 0);
  128. header("Content-type: image/{$canvas->getImageFormat()}");
  129. echo $canvas->getImageBlob();
  130. }
  131. }