📜 ⬆️ ⬇️

Generating invitations similar to invites from habrahabr

The script generates invitations to register on the site in the form of a picture of 51x51 pixels of PNG format, written in PHP, uses MySQL as a database. Made for the sake of interest, it will be interesting only for beginners.

With the help of the GD library, you can easily create and edit images of various formats. To create images we need several functions:
int imagecolorallocate(resource image, int red, int green, int blue)

* This source code was highlighted with Source Code Highlighter .
and
int 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.

First you need to create a table in the database:
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).

Now you need to create an array of 3x4, with the values ​​of the red, green and blue components for each of the four colors and write it in the database.
  1. function add_invite () {
  2. // Creates four random colors and writes them into an array
  3. $ rgb_array = Array ();
  4. for ($ idx = 0; $ idx <4; ++ $ idx) {
  5. $ rgb = array ();
  6. for ($ idx2 = 0; $ idx2 <3; ++ $ idx2) $ rgb [] = mt_rand (0, 255);
  7. $ rgb_array [] = $ rgb;
  8. }
  9. // Transposes the array to a byte-stream representation for writing to the database.
  10. $ serialize = serialize ($ rgb_array);
  11. // Hash invite
  12. $ hash = md5 ($ serialize);
  13. // Inserts
  14. $ _CORE [ 'db' ] -> query ( 'INSERT INTO `di_invite` (` invite_hash`, `invite_serialize`,` invite_username_owner`) VALUES (?,?,?);' , $ Hash, $ serialize, $ _CORE [ 'user' ] -> user_info [ 'user_name' ]);
  15. if ($ _CORE [ 'db' ] -> affected_rows () == 1) return array ( true , 'Invite' ' . $ hash. ' "created ' );
  16. else return array ( false , 'ERROR - database error' );
  17. return true ;
  18. }
* This source code was highlighted with Source Code Highlighter .
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.
')
  1. if (isset ($ _ GET [ 'hash' ] {31})) {
  2. // searches the database for an invite with such a hash
  3. $ result = $ _CORE [ 'db' ] -> query ( 'SELECT `di_invite``ininite_serialize`,` di_invite``ininite_username_recipient` FROM `di_invite` WHERE` di_invite``invite_hash` =?;' , $ hash );
  4. // if found
  5. if ($ result && $ _CORE [ 'db' ] -> num_rows () == 1) {
  6. // if the invitee has already been used, then give the black and white picture
  7. if ($ _CORE [ 'db' ] -> result ($ result, 0, 'invite_username_recipient' )! = = ' )) {
  8. // Give the browser information that this is a picture
  9. header ( 'Content-type: image / png' );
  10. // create a square 51px at 51px
  11. $ im = ImageCreate (51, 51) or die ( 'Error creating image' );
  12. // selects two colors gray and white, and white will be used as a background for the image
  13. $ color [1] = imagecolorallocate ($ im, 255, 255, 255);
  14. $ color [2] = imagecolorallocate ($ im, 200, 200, 200);
  15. // draws two gray rectangles diagonally
  16. ImageFilledRectangle ($ im, 26, 0, 50, 25, $ color [2]);
  17. ImageFilledRectangle ($ im, 0, 25, 25, 50, $ color [2]);
  18. // Display the image in the browser
  19. ImagePng ($ im);
  20. // Destroys it
  21. imagedestroy ($ im);
  22. // otherwise, if the invite is not yet used
  23. } else {
  24. // restores the array
  25. $ rgb_array = unserialize (mysql_result ($ result, 0, 'invite_serialize' ));
  26. header ( 'Content-type: image / png' );
  27. $ im = ImageCreate (51, 51) or die ( 'Error creating image' );
  28. // selects 4 colors from the array values
  29. for ($ i = 0; $ i <4; ++ $ i)
  30. $ color [$ i] = imagecolorallocate ($ im, $ rgb_array [$ i] [0], $ rgb_array [$ i] [1], $ rgb_array [$ i] [2]);
  31. // draws three rectangles, the fourth (i.e., the zero element of the array) the default color is the background of the image
  32. ImageFilledRectangle ($ im, 0, 0, 25, 25, $ color [1]);
  33. ImageFilledRectangle ($ im, 26, 0, 50, 25, $ color [2]);
  34. ImageFilledRectangle ($ im, 0, 25, 25, 50, $ color [3]);
  35. ImagePng ($ im);
  36. imagedestroy ($ im);
  37. }
  38. } else {
  39. exit ();
  40. }
  41. } else {
  42. exit ();
  43. }
* This source code was highlighted with Source Code Highlighter .
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.

  1. function validate_invite ($ hash_serialize, $ userfile) {
  2. // if there were no errors when loading the image, the image size does not exceed the allowable one and the type is adequate
  3. if (isset ($ userfile) && is_uploaded_file ($ userfile [ 'tmp_name' ]) && $ userfile [ 'size' ] <5 * 1024 && $ userfile [ 'type' ] == 'image / png' && $ userfile [ ' error ' ] == 0) {
  4. // create an image based on the loaded
  5. $ source = ImageCreateFromPNG ($ userfile [ 'tmp_name' ]);
  6. // determine the points at which the color will be checked
  7. $ x [] = 48; $ y [] = 48; $ x [] = 2; $ y [] = 2; $ x [] = 48; $ y [] = 2; $ x [] = 2; $ y [] = 48;
  8. $ rgb_array = Array ();
  9. for ($ i = 0; $ i <4; ++ $ i) {
  10. // gets the color index at point
  11. $ color_index = imagecolorat ($ source, $ x [$ i], $ y [$ i]);
  12. // gets the color for the index
  13. $ color_tran = imagecolorsforindex ($ source, $ color_index);
  14. // written to array
  15. $ rgb = Array ();
  16. $ rgb [] = $ color_tran [ 'red' ];
  17. $ rgb [] = $ color_tran [ 'green' ];
  18. $ rgb [] = $ color_tran [ 'blue' ];
  19. $ rgb_array [] = $ rgb;
  20. }
  21. // converts the array to a byte-stream representation for matching with the entry in the array
  22. $ serialize_rgb_array = serialize ($ rgb_array);
  23. // gets hash
  24. $ hash_serialize_rgb_array = md5 ($ serialize_rgb_array);
  25. // searches record in db
  26. $ _CORE [ 'db' ] -> query ( 'SELECT `di_invite``ininite_id` FROM` di_invite` WHERE `di_invite`.`invite_hash` =?;' , $ Hash_serialize_rgb_array);
  27. // if found, clears the memory, and returns true, otherwise clears the memory and returns false
  28. if ($ _CORE [ 'db' ] -> num_rows () <1) {
  29. imagedestroy ($ source);
  30. return false ;
  31. } else {
  32. imagedestroy ($ source);
  33. $ hash_serialize = $ hash_serialize_rgb_array;
  34. return true ;
  35. }
  36. } else return false ;
  37. }
* This source code was highlighted with Source Code Highlighter .
It turns out that something similar to:


PS When building the image, one inaccuracy was allowed, due to which the images are not symmetrical.

Source: https://habr.com/ru/post/121921/


All Articles