int imagecolorallocate(resource image, int red, int green, int blue)
* This source code was highlighted with Source Code Highlighter .
andint imagecolorallocatealpha(resource image, int red, int green, int blue, int alpha)
* This source code was highlighted with Source Code Highlighter .
both functions return a color id for the image. The only difference in the alpha parameter, which sets the transparency of the image. But since there have been problems with PNG transparency in IE since ancient times, it is better to use the first function. The first image argument can be obtained using the function:resource imagecreate( int x, int y)
* This source code was highlighted with Source Code Highlighter .
it returns an image id representing an empty x-by-y palette image.CREATE TABLE IF NOT EXISTS `di_invite` (
`invite_id` int (15) unsigned NOT NULL AUTO_INCREMENT,
`invite_hash` varchar (32) COLLATE utf8_unicode_ci DEFAULT NULL ,
`invite_serialize` text COLLATE utf8_unicode_ci,
`invite_username_owner` varchar (25) COLLATE utf8_unicode_ci DEFAULT NULL ,
`invite_username_recipient` varchar (25) COLLATE utf8_unicode_ci DEFAULT NULL ,
PRIMARY KEY (`invite_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE =utf8_unicode_ci AUTO_INCREMENT=1 ;
* This source code was highlighted with Source Code Highlighter .
The database will store the unique identifier of each invitation (invite_id), invitation hash (invite_hash), an array with four RGB components, a byte-stream representation (invite_serialize), the nickname of the invitation owner (invite_username_owner) and the nickname of the user who activated it (invite_username_recipient).We need to think about how this invitation to give to the user. Of course, this can be done most easily by presenting it in the form of a picture. When generating an image, you should take into account that instead of an activated invite, you should give a black and white picture, otherwise a full-fledged four-color invite.* This source code was highlighted with Source Code Highlighter .
- function add_invite () {
- // Creates four random colors and writes them into an array
- $ rgb_array = Array ();
- for ($ idx = 0; $ idx <4; ++ $ idx) {
- $ rgb = array ();
- for ($ idx2 = 0; $ idx2 <3; ++ $ idx2) $ rgb [] = mt_rand (0, 255);
- $ rgb_array [] = $ rgb;
- }
- // Transposes the array to a byte-stream representation for writing to the database.
- $ serialize = serialize ($ rgb_array);
- // Hash invite
- $ hash = md5 ($ serialize);
- // Inserts
- $ _CORE [ 'db' ] -> query ( 'INSERT INTO `di_invite` (` invite_hash`, `invite_serialize`,` invite_username_owner`) VALUES (?,?,?);' , $ Hash, $ serialize, $ _CORE [ 'user' ] -> user_info [ 'user_name' ]);
- if ($ _CORE [ 'db' ] -> affected_rows () == 1) return array ( true , 'Invite' ' . $ hash. ' "created ' );
- else return array ( false , 'ERROR - database error' );
- return true ;
- }
It remains the case for small, namely, to check the validity of the invitation. To do this, it is enough to count the color value in the four corners of the picture into an array, and try to find the hash of the resulting array in the database.* This source code was highlighted with Source Code Highlighter .
- if (isset ($ _ GET [ 'hash' ] {31})) {
- // searches the database for an invite with such a hash
- $ result = $ _CORE [ 'db' ] -> query ( 'SELECT `di_invite``ininite_serialize`,` di_invite``ininite_username_recipient` FROM `di_invite` WHERE` di_invite``invite_hash` =?;' , $ hash );
- // if found
- if ($ result && $ _CORE [ 'db' ] -> num_rows () == 1) {
- // if the invitee has already been used, then give the black and white picture
- if ($ _CORE [ 'db' ] -> result ($ result, 0, 'invite_username_recipient' )! = = ' )) {
- // Give the browser information that this is a picture
- header ( 'Content-type: image / png' );
- // create a square 51px at 51px
- $ im = ImageCreate (51, 51) or die ( 'Error creating image' );
- // selects two colors gray and white, and white will be used as a background for the image
- $ color [1] = imagecolorallocate ($ im, 255, 255, 255);
- $ color [2] = imagecolorallocate ($ im, 200, 200, 200);
- // draws two gray rectangles diagonally
- ImageFilledRectangle ($ im, 26, 0, 50, 25, $ color [2]);
- ImageFilledRectangle ($ im, 0, 25, 25, 50, $ color [2]);
- // Display the image in the browser
- ImagePng ($ im);
- // Destroys it
- imagedestroy ($ im);
- // otherwise, if the invite is not yet used
- } else {
- // restores the array
- $ rgb_array = unserialize (mysql_result ($ result, 0, 'invite_serialize' ));
- header ( 'Content-type: image / png' );
- $ im = ImageCreate (51, 51) or die ( 'Error creating image' );
- // selects 4 colors from the array values
- for ($ i = 0; $ i <4; ++ $ i)
- $ color [$ i] = imagecolorallocate ($ im, $ rgb_array [$ i] [0], $ rgb_array [$ i] [1], $ rgb_array [$ i] [2]);
- // draws three rectangles, the fourth (i.e., the zero element of the array) the default color is the background of the image
- ImageFilledRectangle ($ im, 0, 0, 25, 25, $ color [1]);
- ImageFilledRectangle ($ im, 26, 0, 50, 25, $ color [2]);
- ImageFilledRectangle ($ im, 0, 25, 25, 50, $ color [3]);
- ImagePng ($ im);
- imagedestroy ($ im);
- }
- } else {
- exit ();
- }
- } else {
- exit ();
- }
It turns out that something similar to:* This source code was highlighted with Source Code Highlighter .
- function validate_invite ($ hash_serialize, $ userfile) {
- // if there were no errors when loading the image, the image size does not exceed the allowable one and the type is adequate
- if (isset ($ userfile) && is_uploaded_file ($ userfile [ 'tmp_name' ]) && $ userfile [ 'size' ] <5 * 1024 && $ userfile [ 'type' ] == 'image / png' && $ userfile [ ' error ' ] == 0) {
- // create an image based on the loaded
- $ source = ImageCreateFromPNG ($ userfile [ 'tmp_name' ]);
- // determine the points at which the color will be checked
- $ x [] = 48; $ y [] = 48; $ x [] = 2; $ y [] = 2; $ x [] = 48; $ y [] = 2; $ x [] = 2; $ y [] = 48;
- $ rgb_array = Array ();
- for ($ i = 0; $ i <4; ++ $ i) {
- // gets the color index at point
- $ color_index = imagecolorat ($ source, $ x [$ i], $ y [$ i]);
- // gets the color for the index
- $ color_tran = imagecolorsforindex ($ source, $ color_index);
- // written to array
- $ rgb = Array ();
- $ rgb [] = $ color_tran [ 'red' ];
- $ rgb [] = $ color_tran [ 'green' ];
- $ rgb [] = $ color_tran [ 'blue' ];
- $ rgb_array [] = $ rgb;
- }
- // converts the array to a byte-stream representation for matching with the entry in the array
- $ serialize_rgb_array = serialize ($ rgb_array);
- // gets hash
- $ hash_serialize_rgb_array = md5 ($ serialize_rgb_array);
- // searches record in db
- $ _CORE [ 'db' ] -> query ( 'SELECT `di_invite``ininite_id` FROM` di_invite` WHERE `di_invite`.`invite_hash` =?;' , $ Hash_serialize_rgb_array);
- // if found, clears the memory, and returns true, otherwise clears the memory and returns false
- if ($ _CORE [ 'db' ] -> num_rows () <1) {
- imagedestroy ($ source);
- return false ;
- } else {
- imagedestroy ($ source);
- $ hash_serialize = $ hash_serialize_rgb_array;
- return true ;
- }
- } else return false ;
- }
Source: https://habr.com/ru/post/121921/
All Articles