How to Use PHP code to implement this Java code.

Source: Internet
Author: User
{Code...} is trying to use PHP to write verification code identification. Refer to a Java code. However, I don't know how to use PHP to write this code. The general meaning is to open the image in the directory, and then talk about the image as the map key, the file name as the value. Then I wrote the following code, but I don't know what to do later...
public static Map
  
    loadTrainData() throws Exception {        Map
   
     map = new HashMap
    
     ();        File dir = new File("train");        File[] files = dir.listFiles();        for (File file : files) {            map.put(ImageIO.read(file), file.getName().charAt(0) + "");        }        return map;    }
    
   
  

When trying to use PHP to write a verification code for recognition, refer to a Java code. However, I don't know how to use PHP to write this code.

The general meaning is to open the image in the directory, and then talk about the image as the map key, the file name as the value.

Then I wrote the following code, but I don't know what to do later.

Public function loadTrainData () {$ data = array (); $ path = "train/train1/"; $ handler = opendir ($ path ); while ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {$ Files [] = file_get_contents ($ path. $ filename) ;}} closedir ($ handler); # code how to write it here, or write the function return $ data in a better way ;}

Reply content:
public static Map
  
    loadTrainData() throws Exception {        Map
   
     map = new HashMap
    
     ();        File dir = new File("train");        File[] files = dir.listFiles();        for (File file : files) {            map.put(ImageIO.read(file), file.getName().charAt(0) + "");        }        return map;    }
    
   
  

When trying to use PHP to write a verification code for recognition, refer to a Java code. However, I don't know how to use PHP to write this code.

The general meaning is to open the image in the directory, and then talk about the image as the map key, the file name as the value.

Then I wrote the following code, but I don't know what to do later.

Public function loadTrainData () {$ data = array (); $ path = "train/train1/"; $ handler = opendir ($ path ); while ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {$ Files [] = file_get_contents ($ path. $ filename) ;}} closedir ($ handler); # code how to write it here, or write the function return $ data in a better way ;}

YourtrainIs the file name under the directory like this?A B C D ... 0 1 2 3
JavaThe Code Section is to traverse the directory and then createMap.

Your PHP code should look like this:

$files[$filename] = file_get_contents($path.$filename);

Resolved.

public function loadTrainData()    {        $path = "train/tran1/";        $handler = opendir($path);          while (($filename = readdir($handler)) !== false) {              if ($filename != "." && $filename != "..") {                  $sampleMap[substr($filename, 0, 1)] = imagecreatefromjpeg($path.$filename);           }          }        closedir($handler);         return $sampleMap;    }

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.