get_random_image in 2.2 also
git-svn-id: file:///home/shish/svn/shimmie2/branches/branch_2.2@866 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
parent
482312b11e
commit
f180624ebf
35
contrib/random_image/main.php
Normal file
35
contrib/random_image/main.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Name: Random Image
|
||||||
|
* Author: Shish <webmaster@shishnet.org>
|
||||||
|
* License: GPLv2
|
||||||
|
* Description: Do things with a random image
|
||||||
|
*/
|
||||||
|
|
||||||
|
class RandomImage extends Extension {
|
||||||
|
public function receive_event($event) {
|
||||||
|
if(is_a($event, 'PageRequestEvent') && ($event->page_name == "random_image")) {
|
||||||
|
global $database;
|
||||||
|
|
||||||
|
if($event->count_args() == 1) {
|
||||||
|
$action = $event->get_arg(0);
|
||||||
|
$search_terms = array();
|
||||||
|
}
|
||||||
|
else if($event->count_args() == 2) {
|
||||||
|
$action = $event->get_arg(0);
|
||||||
|
$search_terms = explode(' ', $event->get_arg(1));
|
||||||
|
}
|
||||||
|
$image = $database->get_random_image($search_terms);
|
||||||
|
|
||||||
|
if($event->get_arg(0) == "download") {
|
||||||
|
if(!is_null($image)) {
|
||||||
|
$event->page->set_mode("data");
|
||||||
|
$event->page->set_type("image/jpeg");
|
||||||
|
$event->page->set_data(file_get_contents($image->get_image_filename()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add_event_listener(new RandomImage());
|
||||||
|
?>
|
@ -90,19 +90,23 @@ class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// misc {{{
|
// misc {{{
|
||||||
public function count_pages($tags=array()) {
|
public function count_images($tags=array()) {
|
||||||
global $config;
|
|
||||||
$images_per_page = $config->get_int('index_width') * $config->get_int('index_height');
|
|
||||||
if(count($tags) == 0) {
|
if(count($tags) == 0) {
|
||||||
return ceil($this->db->GetOne("SELECT COUNT(*) FROM images") / $images_per_page);
|
return $this->db->GetOne("SELECT COUNT(*) FROM images");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$querylet = $this->build_search_querylet($tags);
|
$querylet = $this->build_search_querylet($tags);
|
||||||
$result = $this->execute($querylet->sql, $querylet->variables);
|
$result = $this->execute($querylet->sql, $querylet->variables);
|
||||||
return ceil($result->RecordCount() / $images_per_page);
|
return $result->RecordCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function count_pages($tags=array()) {
|
||||||
|
global $config;
|
||||||
|
$images_per_page = $config->get_int('index_width') * $config->get_int('index_height');
|
||||||
|
return ceil($this->count_images($tags) / $images_per_page);
|
||||||
|
}
|
||||||
|
|
||||||
public function execute($query, $args=array()) {
|
public function execute($query, $args=array()) {
|
||||||
$result = $this->db->Execute($query, $args);
|
$result = $this->db->Execute($query, $args);
|
||||||
if($result === False) {
|
if($result === False) {
|
||||||
@ -362,6 +366,14 @@ class Database {
|
|||||||
return ($row ? new Image($row) : null);
|
return ($row ? new Image($row) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_random_image($tags=array()) {
|
||||||
|
$max = $this->count_images($tags);
|
||||||
|
$rand = mt_rand(0, $max);
|
||||||
|
$set = $this->get_images($rand, 1, $tags);
|
||||||
|
if(count($set) > 0) return $set[0];
|
||||||
|
else return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function get_image_by_hash($hash) {
|
public function get_image_by_hash($hash) {
|
||||||
$image = null;
|
$image = null;
|
||||||
$row = $this->db->GetRow("{$this->get_images} WHERE hash=?", array($hash));
|
$row = $this->db->GetRow("{$this->get_images} WHERE hash=?", array($hash));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user