Does anyone have an idea to why the correct_type value is not displaying and the images it is supposed to display.
Here is the php code for the first php page.
Here is the PHP for the show.php file.PHP Code:<?php
include_once "connection/connectdb.php";
class ScriptCheck {
protected $image_dir = './images/';
protected $extensions = array();
protected $images = array();
protected static $_instance;
protected $watermark = 'watermark.png';
protected $watermark_size = null;
protected $tempimagesdir = './temp/';
protected $types = array ('gun', 'cash');
protected $max_correct_percent = 0.6;
private function __construct($image_dir = null, $extensions = null, $padding = null) {
session_start();
$this->image_dir = $image_dir ? $image_dir : dirname(__FILE__);
$this->image_dir = rtrim($this->image_dir, '/');
$this->watermark = $this->image_dir.'/'.$this->watermark;
$this->extensions = $extensions ? $extensions : array ('jpg');
$this->padding = $padding ? (int) $padding : 5;
$this->scanDirForImages();
$this->watermark_size = getimagesize($this->watermark);
}
private function scanDirForImages() {
if(!file_exists($this->image_dir) || !is_dir($this->image_dir)) {
return false;
}
elseif(is_readable($this->image_dir)) {
$directory_list = opendir($this->image_dir);
while (false !== ($file = readdir($directory_list))) {
if($file != '.' && $file != '..') {
$path = $this->image_dir.'/'.$file;
if(is_readable($path)) {
$subdirectories = explode('/', $path);
if(is_file($path)) {
$ext = explode('.', end($subdirectories));
$extension = end($ext);
if(in_array($extension, $this->extensions) && in_array($ext[1], $this->types)) {
$this->images[$ext[1]][] = $path;
}
}
}
}
}
closedir($directory_list);
foreach ($this->types as $key => $type) {
if (!is_array($this->images[$type]) || count($this->images[$type]) < 1) {
unset($this->images[$type]);
unset($this->types[$key]);
}
}
return true;
}else{
return false;
}
}
private function deleteImages($files) {
foreach ($files as $file => $temp) {
@unlink($this->tempimagesdir.'/'.$file.'.png');
}
}
public function showImages() {
if (is_array($_SESSION['scrpt_images'])) {
return $this->outputHTML();
}
$images = $this->images;
$types = $this->types;
$selected = array();
$total = 8;
$total_types = count($types);
$correct_type = mt_rand(0, $total_types-1);
$correct_total = count($images[$types[$correct_type]]);
$correct_amount = mt_rand(1, round($total*$this->max_correct_percent)-1);
$_SESSION['correct_type'] = $types[$correct_type];
$_SESSION['correct_amount'] = $correct_amount;
for ($i=0;$i<$correct_amount;$i++) {
$img = mt_rand(0, $correct_total-1);
$image = $images[$types[$correct_type]][$img];
$selected[] = $this->processImage($image, true);
$correct_total--;
unset($images[$types[$correct_type]][$img]);
$images[$types[$correct_type]] = array_values($images[$types[$correct_type]]);
}
unset($images[$types[$correct_type]]);
unset($types[$correct_type]);
$types = array_values($types);
$total_types--;
for ($i=0;$i<($total-$correct_amount);$i++) {
$type = mt_rand(0, $total_types-1);
$img = mt_rand(0, count($images[$types[$type]])-1);
$selected[] = $this->processImage($images[$types[$type]][$img], false);
unset($images[$types[$type]][$img]);
$images[$types[$type]] = array_values($images[$types[$type]]);
}
$_SESSION['scrpt_images'] = $this->shuffleImages($_SESSION['scrpt_images']);
return $this->outputHTML();
}
public function outputHTML() {
include('./scriptcheck/show.php');
}
private function shuffleImages ($array) {
while (count($array) > 0) {
$val = array_rand($array);
$new_arr[$val] = $array[$val];
unset($array[$val]);
}
return $new_arr;
}
public function processImage($image_path, $add = true) {
$type = @exif_imagetype($image_path);
$size = @getimagesize($image_path);
$open = null;
switch ($size[2]) {
case IMAGETYPE_GIF:
$open = imagecreatefromgif($image_path);
break;
case IMAGETYPE_JPEG:
$open = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$open = imagecreatefrompng($image_path);
break;
default:
return false;
break;
}
$max_x = $size[0] - $this->watermark_size[0] - 5;
$max_y = $size[1] - $this->watermark_size[1] - 5;
$min_x = 5;
$min_y = 5;
$x = mt_rand($min_x, $max_x);
$y = mt_rand($min_y, $max_y);
$water = imagecreatefrompng($this->watermark);
imageAlphaBlending($water, false);
imageSaveAlpha($water, true);
imagecopy($open, $water, $x, $y, 0, 0, $this->watermark_size[0], $this->watermark_size[1]);
imagedestroy($water);
$name = md5(microtime());
imagepng($open, $this->tempimagesdir.'/'.$name.'.png');
if ($add) {
$_SESSION['scrpt_images'][$name] = 1;
}
else {
$_SESSION['scrpt_images'][$name] = 0;
}
imagedestroy($open);
return $name;
}
public function testImages($images) {
$match = true;
if (!$_SESSION['scrpt_images']) {
return false;
}
foreach ($images as $image => $key) {
if ($_SESSION['scrpt_images'][$image] != (int)$key) {
$match = false;
}
}
$this->deleteImages($_SESSION['scrpt_images']);
unset($_SESSION['scrpt_images']);
return $match;
}
public static function getInstance($image_dir = null, $extensions = null, $padding = null) {
if (null === self::$_instance) {
self::$_instance = new self($image_dir, $extensions, $padding);
}
return self::$_instance;
}
}
if ($_POST) {
$scriptcheck = ScriptCheck::getInstance('./images/');
$check = $scriptcheck->testImages($_POST['images']);
if ($check)
{
$newtime = time() + (60 * 7);
mysql_query("UPDATE users SET last_script_check='$time' WHERE username='$username'");
echo "<script>document.location=document.location</script>";
}
else
{
echo "<div class='error'>Wrong!</div>";
}
}
else {
$scriptcheck = ScriptCheck::getInstance('./images/');
$scriptcheck->showImages();
}
exit();
}
?>
PHP Code:Select <?= $_SESSION['correct_amount'] ?> pictures of <?= $_SESSION['correct_type'] ?>:
<?php
$i = 0;
if(is_array($_SESSION['scrpt_images'])){
foreach ($_SESSION['scrpt_images'] as $image => $correct) {
echo "<div id='div{$i}' class='normal'><input id='select{$i}' type='hidden' name='images[$image]' value='0'><img id='img{$i}' src='temp/{$image}.png' class='normal' onclick='toggleImg(\"{$i}\");return false;' /></div>";
$i++;
if ($i % 4 == 0) {
echo "<div style='clear: both;'> </div>";
}
}}
?>


Reply With Quote
Bookmarks