How JAVA is transplanted to PHP

Source: Internet
Author: User
The principle in JAVA is transplanted to PHP. Below is a piece of JAVA source code that I downloaded on CSDN. The purpose is to calculate the image fingerprint, public & nbsp; static & nbsp; String & nbsp; produceFingerPrint (String & nbsp; filename) & nbsp; {principles of Buffered JAVA are transplanted to PHP
Here is a piece of JAVA source code that I downloaded on CSDN. The purpose is to calculate the image fingerprint,
Public static String produceFingerPrint (String filename ){
BufferedImage source = ImageHelper. readPNGImage (filename); // read the file

Int width = 8;
Int height = 8;

// Step 1: reduce the size.
// Reduce the image size to 8x8, with a total of 64 pixels. The purpose of this step is to remove the image details, retain only the basic information such as structure and brightness, and discard the image differences caused by different sizes and proportions.
BufferedImage thumb = ImageHelper. thumb (source, width, height, false );

// Step 2: simplify the color.
// Convert the reduced image to 64-level gray scale. That is to say, all pixels have only 64 colors in total.
Int [] pixels = new int [width * height];
For (int I = 0; I <width; I ++ ){
For (int j = 0; j Pixels [I * height + j] = ImageHelper. rgbToGray (thumb. getRGB (I, j ));
}
}

// Step 3: calculate the average value.
// Calculate the average gray scale of all 64 pixels.
Int avgPixel = ImageHelper. average (pixels );

// Step 4: compare the gray scale of pixels.
// Compare the gray scale of each pixel with the average value. If the value is greater than or equal to the average value, it is recorded as 1. if the value is smaller than the average value, it is recorded as 0.
Int [] comps = new int [width * height];
For (int I = 0; I <comps. length; I ++ ){
If (pixels [I]> = avgPixel ){
Comps [I] = 1;
} Else {
Comps [I] = 0;
}
}

// Step 5: calculate the hash value.
// Combine the comparison results in the previous step to form a 64-bit integer, which is the fingerprint of the image. The order of the combination is not important, as long as all images are in the same order.
StringBuffer hashCode = new StringBuffer ();
For (int I = 0; I <comps. length; I + = 4 ){
Int result = comps [I] * (int) Math. pow (2, 3) + comps [I + 1] * (int) Math. pow (2, 2) + comps [I + 2] * (int) Math. pow (2, 1) + comps [I + 2];
HashCode. append (binaryToHex (result ));
}

// After obtaining the fingerprint, you can compare different images to see how many digits are different in the 64-bit format.
Return hashCode. toString ();
}


Now I want to use PHP as a web page to implement this function. which expert can give the source code for conversion to PHP?
------ Solution --------------------
The algorithm description is clear, so it is not difficult to implement it.
Note that php does not support 64-bit integer numbers, so the function provides several possible return styles.
/**
* Fingerprint generation
* $ Filename: the image file name, which can be a URL. Only images supported by GD can be used.
* $ Retmode return format: 0 binary representation 1 array 2 hexadecimal representation
**/
Function produceFingerPrint ($ filename, $ retmode = 0 ){
$ Sim = imagecreatefromstring (file_get_contents ($ filename ));
$ Dim = imagecreate (8, 8 );
Imagecopyresized ($ dim, $ sim, 0, 0, 0, 0, 8, 8, imagesx ($ sim), imagesy ($ sim ));
Imagetruecolortopalette ($ sim, true, 64 );
// Imagefilter ($ sim, IMG_FILTER_GRAYSCALE );

For ($ x = 0; $ x <8; $ x ++) for ($ y = 0; $ y <8; $ y ++ ){
$ C = imagecolorat ($ dim, $ x, $ y );
$ P [] = array_sum (imagecolorsforindex ($ dim, $ c)/3;
}
$ Avl = array_sum ($ p)/count ($ p );
$ R = '';
Foreach ($ p as $ v) $ r. = $ v >=$ avl? 1: 0;

If ($ retmode = 0) return $ r;
$ P = array_map ('bindec ', str_split ($ r, 8 ));
If ($ retmode = 1) return $ p;
If ($ retmode = 2 ){

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.