PHP iterates through the directory output directory and all the image files below it

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.