0.9.0 - the ZapV version updated to current php standards. Brave search used, while Bing and Google search api are deprecated
This commit is contained in:
parent
3d4298fe8b
commit
4ddaddda3d
31 changed files with 1778 additions and 1 deletions
121
lib/ZAPZap.class.php
Executable file
121
lib/ZAPZap.class.php
Executable file
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
/**
|
||||
* ZAP factory
|
||||
*/
|
||||
class ZAPZap {
|
||||
private $tpl = '';
|
||||
private $folder;
|
||||
private $height;
|
||||
private $width;
|
||||
private $words;
|
||||
|
||||
public function __construct() {
|
||||
ob_start();
|
||||
if (!isset($_SESSION['collages']) || $_SESSION['collages'] <= 0) {
|
||||
$this->destroySession();
|
||||
$this->tpl .= ob_get_clean();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->width = $_SESSION['width'];
|
||||
$this->height = $_SESSION['height'];
|
||||
$this->folder = $_SESSION['folder'];
|
||||
$this->words = $_SESSION['words'];
|
||||
|
||||
if (!empty($this->words)) {
|
||||
// Still downloading source images for the words.
|
||||
$this->showFoundImage();
|
||||
} elseif (empty($_SESSION['images'])) {
|
||||
// Start a new collage.
|
||||
$this->buildNextCollage();
|
||||
} else {
|
||||
// Add another source image to the current collage.
|
||||
$imgName = array_shift($_SESSION['images']);
|
||||
$image = $this->createImage($_SESSION['collageNr']);
|
||||
$image->addToCanvas($imgName);
|
||||
}
|
||||
$this->tpl .= ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the next collage if any remain, otherwise finish.
|
||||
*/
|
||||
private function buildNextCollage() {
|
||||
if ($_SESSION['collages'] <= 0) {
|
||||
$this->destroySession();
|
||||
return;
|
||||
}
|
||||
|
||||
// On the very first collage, load the downloaded source images.
|
||||
// On later collages, reuse the original list.
|
||||
if (!isset($_SESSION['imgOriginal'])) {
|
||||
$_SESSION['images'] = ImgTools::convertGifsToPng($this->retrieveImages());
|
||||
$_SESSION['imgOriginal'] = $_SESSION['images'];
|
||||
} else {
|
||||
$_SESSION['images'] = $_SESSION['imgOriginal'];
|
||||
shuffle($_SESSION['images']);
|
||||
}
|
||||
|
||||
if (empty($_SESSION['images'])) {
|
||||
$this->destroySession();
|
||||
return;
|
||||
}
|
||||
|
||||
// Track the current collage number separately so addToCanvas always
|
||||
// reads/writes the same file while this collage is being built.
|
||||
$_SESSION['collageNr'] = $_SESSION['collages'];
|
||||
$_SESSION['collages']--;
|
||||
|
||||
$firstIm = array_shift($_SESSION['images']);
|
||||
$image = $this->createImage($_SESSION['collageNr']);
|
||||
$image->createBasicCanvas($firstIm);
|
||||
}
|
||||
|
||||
private function createImage($collages) {
|
||||
return new ImgProcess($this->folder, $this->width, $this->height, $collages);
|
||||
}
|
||||
|
||||
private function destroySession() {
|
||||
if (Session::destroy()) {
|
||||
$this->tpl .= "<p>" . ERROR_PREFIX . "Image ready</p>";
|
||||
} else {
|
||||
$this->tpl .= "<p>" . ERROR_PREFIX . "Strange... Could not end PHP session";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private function setSessionVar($key, $value) {
|
||||
$_SESSION[$key] = $value;
|
||||
}
|
||||
|
||||
private function showFoundImage() {
|
||||
$keyWord = array_shift($this->words);
|
||||
$search = new ImgSearch($this->folder, $keyWord);
|
||||
$imageUrl = $search->getImage();
|
||||
|
||||
if ($imageUrl) {
|
||||
ImgIOTools::urlOnScreen($imageUrl);
|
||||
} else {
|
||||
ImgIOTools::urlOnScreen(ZAP_APP_BASE_DIR . DIRECTORY_SEPARATOR
|
||||
. ZAP_WEB_DIR . DIRECTORY_SEPARATOR
|
||||
. 'images' . DIRECTORY_SEPARATOR . 'error_noHit.png');
|
||||
}
|
||||
$this->setSessionVar('words', $this->words);
|
||||
}
|
||||
|
||||
private function retrieveImages() {
|
||||
$img_ar = glob('{' . $this->folder . DIRECTORY_SEPARATOR .'*.jpg,'
|
||||
. $this->folder . DIRECTORY_SEPARATOR .'*.png,'
|
||||
. $this->folder . DIRECTORY_SEPARATOR .'*.jpeg,'
|
||||
. $this->folder . DIRECTORY_SEPARATOR .'*.JPG,'
|
||||
. $this->folder . DIRECTORY_SEPARATOR .'*.gif'
|
||||
. '}', GLOB_BRACE);
|
||||
shuffle($img_ar);
|
||||
return $img_ar;
|
||||
}
|
||||
|
||||
public function getContent() {
|
||||
return $this->tpl;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue