📜 ⬆️ ⬇️

Image Generator invites v. 2.0

I would like to share the method of generating image invites with almost no database.

This article has pushed me to this invention.



Today I taught an article about the generation of invites, but I was immediately zaminusovali and I decided to remove the article in drafts.
')
The program consists of the following files:

Principle of operation:
The picture is generated in a pseudo-random way, with a certain regularity.
And the tester checks the presence of these patterns.

Previously, an invite was generated only by the code word and checked for it, but as devpony said,

So that's better.
index.php
<? header('Content-Type: text/html; charset=utf-8'); ?> <fieldset> <legend>Get invite</legend> <button onclick="document.getElementById('img').src='image.php?rand='+(Math.floor(Math.random()*999))"></button> <br> <br> IMG: <img src="image.php?in=" id="img" onclick="window.open(this.src)" width="200" height="200"> <br>      . </fieldset> <fieldset> <legend>Test invite</legend> <form method="post" enctype="multipart/form-data" action="test.php"> <input type="hidden" name="UserID" value="<?php $ip = $_SERVER['REMOTE_ADDR']; $ua = $_SERVER['HTTP_USER_AGENT']; $d = getdate(); $UserID = sha1($ip.$ua.$d["mday"].'Secret'); $UserID = base64_encode($UserID); $UserID = rtrim($UserID,"="); echo $UserID; ?>"> <input type="file" name="uploadfile" style="width:300px;height:100px;background:#fc0;"> <br> <button type="submit">SUBMIT</button> </form> </fieldset> 

UserID is an identifier, so that if someone uploads images at the same time, they will not overflow.

image.php
 <? include('generator.php');//  header('Content-Disposition: attachment; filename="image.png"');//    header ("Content-type: image/png");//  imagepng(generateImage());//  ?> 


generator.php
 <? function generateImage($str){ $secret="SECRET_STRING"; if(!isset($str)){ $str = sha1(rand(-99999,99999).$secret);// -  }else{ $str = sha1($str.$secret); } for($i=0;$i<6*$WH+1;$i++){ $str .= sha1($str.$secret); } $WH = 200;//   $im = imagecreatetruecolor($WH, $WH); $im1 = imagecreatetruecolor($WH, $WH); $im2 = imagecreatetruecolor($WH, $WH); $im3 = imagecreatetruecolor($WH, $WH); $im4 = imagecreatetruecolor($WH, $WH);//5  $arr = array(); for($i=0;$i<round(strlen($str)/6, 0);$i++){ $arr[] = hex2rgb(substr($str, $i*6, 6)); //  } for ($i=0;$i<$WH/2;$i++){ $r = $arr[$i][0]; $g = $arr[$i][1]; $b = $arr[$i][2]; $color = imagecolorallocate($im, $r, $g, $b); imagefilledrectangle($im1, $i, $i, $WH-$i, $WH-$i, $color); $color = imagecolorallocate($im, $b, $r, $g); imagefilledrectangle($im2, $i, $i, $WH-$i, $WH-$i, $color); $color = imagecolorallocate($im, $g, $r, $b); imagefilledrectangle($im3, $i, $i, $WH-$i, $WH-$i, $color); $color = imagecolorallocate($im, $r, $b, $g); imagefilledrectangle($im4, $i, $i, $WH-$i, $WH-$i, $color); } imagecopy($im, $im1, 0, 0, 0, 0, $WH/2, $WH/2); imagecopy($im, $im2, $WH/2, 0, $WH/2, 0, $WH/2, $WH/2); imagecopy($im, $im3, 0, $WH/2, 0, $WH/2, $WH/2, $WH/2); imagecopy($im, $im4, $WH/2, $WH/2, $WH/2, $WH/2, $WH, $WH); return $im; } function hex2rgb($hex){ $hex = str_replace("#", "", $hex); if(strlen($hex) == 3) { $r = hexdec(substr($hex,0,1).substr($hex,0,1)); $g = hexdec(substr($hex,1,1).substr($hex,1,1)); $b = hexdec(substr($hex,2,1).substr($hex,2,1)); } else { $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2)); $b = hexdec(substr($hex,4,2)); } $rgb = array($r, $g, $b); return $rgb; } 

As we can see, the colors are the same, but the channels are confused.

test.php
 <?php error_reporting(0); include('generator.php'); $UserID = rtrim(base64_encode(sha1($_POST['UserID'])), '='); $name1 = './files/image1_'.$UserID.'.png'; if (copy($_FILES['uploadfile']['tmp_name'], $name1)){ $im = imagecreatefrompng($name1); function getColor($im, $x, $y){ $rgb = imagecolorat($im, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; return array($r, $g, $b); } $max_count = 0; $counted = 0; for($i=0;$i<100;$i++){ $max_count++; if( getColor($im, 99, $i)[0] == getColor($im, 100, $i)[1] AND getColor($im, 99, $i)[1] == getColor($im, 100, $i)[2] AND getColor($im, 99, $i)[2] == getColor($im, 100, $i)[0] AND (getColor($im, 99, $i)[0] != getColor($im, 100, $i)[0] OR getColor($im, 99, $i)[1] != getColor($im, 100, $i)[1] OR getColor($im, 99, $i)[2] != getColor($im, 100, $i)[2])){ $counted++; } } for($i=0;$i<100;$i++){ $max_count++; if( getColor($im, $i, 99)[0] == getColor($im, $i, 100)[1] AND getColor($im, $i, 99)[1] == getColor($im, $i, 100)[0] AND getColor($im, $i, 99)[2] == getColor($im, $i, 100)[2] AND (getColor($im, $i, 99)[0] != getColor($im, $i, 100)[0] OR getColor($im, $i, 99)[1] != getColor($im, $i, 100)[1] OR getColor($im, $i, 99)[2] != getColor($im, $i, 100)[2])){ $counted++; } } for($i=101;$i<200;$i++){ $max_count++; if( getColor($im, $i, 99)[0] == getColor($im, $i, 100)[1] AND getColor($im, $i, 99)[1] == getColor($im, $i, 100)[0] AND getColor($im, $i, 99)[2] == getColor($im, $i, 100)[2] AND (getColor($im, $i, 99)[0] != getColor($im, $i, 100)[0] OR getColor($im, $i, 99)[1] != getColor($im, $i, 100)[1] OR getColor($im, $i, 99)[2] != getColor($im, $i, 100)[2])){ $counted++; } } for($i=101;$i<200;$i++){ $max_count++; if( getColor($im, 99, $i)[0] == getColor($im, 100, $i)[2] AND getColor($im, 99, $i)[1] == getColor($im, 100, $i)[0] AND getColor($im, 99, $i)[2] == getColor($im, 100, $i)[1] AND (getColor($im, 99, $i)[0] != getColor($im, 100, $i)[0] OR getColor($im, 99, $i)[1] != getColor($im, 100, $i)[1] OR getColor($im, 99, $i)[2] != getColor($im, 100, $i)[2])){ $counted++; } } if($counted>$max_count*0.9){//  90% echo " COMPLETE"; }else{ echo " FAILED"; } }else{ echo "ERROR ".$_FILES['uploadfile']['error']; } ?> 

Here more.
These 4 cycles are testing the axes (the place of the “joint of four parts”) + checking that they are not the same.



Well that's all.
True, there are some cons.
Namely, there is no protection against reuse.
You can use, for example, recursive hash or come up with some kind of time limit.
I still think about it.
Demo: http://app.blastorq.pp.ua/ImgInvite/ (in Ukrainian)
GitHub: https://github.com/da411d/ImgInviteGenerator

If you find an inaccuracy or error - write to me, not much minus

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


All Articles