In doing the site, you need to give all the images in the article to add logo, how to use thinkphp to achieve.
Thinkphp for us to provide a good image processing class, to all the pictures in the article Watermark ideas, upload the picture files are saved in a folder below, I just need to get the folder and subfolders below all the pictures, traverse once, add watermark, So the current difficulty is just how to get the picture in the file and traverse it once. [The traversal of a two-dimensional array will appear in the Imagescontroller controller]; Therefore, the problem of adding a watermark to the image is converted to the use of PHP to traverse the directory output directory and all the image files below it.
//Traverse Directory output directory and all files under it recursive resolution of the use of functionsfunction My_scandir ($dir) {$files=Array (); if(Is_dir ($dir)) {if($handle =Opendir ($dir)) { while(($file =readdir ($handle))!==false){ if($file! ='.'&& $file! =".."){ if(Is_dir ($dir."/". $file)) {$files [$file]=my_scandir ($dir."/". $file); }Else{$files []= $dir."/". $file;//get the full path to the file$filesnames []= $file;//gets the file name of the file}}}}} Closedir ($handle); return$files; //return $filesnames;}
The next step is to deal with the picture watermark problem,
//traversal for one-dimensional arrays$PICARR = My_scandir ('./public/uploads/txtpig');//returns the array-formatted datadump ($PICARR); foreach($PICARR as$value) {$image=New\think\image (); $image->open ($value)->text ('Pengjun','./public/consola.ttf', -,'#000000', \think\image::image_water_southeast)Save ($value); }
//A php two-dimensional array traversal, using two foreach () traversal can be$PICARR 2 = My_scandir ('./public/plugins\kindeditor\attached\image'); Dump ($PICARR 2); foreach($PICARR 2 as$key =$value 1) { foreach($value 1 as$key 2 =$value 2) {$image=New\think\image (); $image->open ($value 2)->text ('Pengjun','./public/consola.ttf', -,'#000000', \think\image::image_water_southeast)Save ($value 2); } }
PHP iterates through the directory output directory and all the image files below it