Skip to content

@Chase-san /chibichan.php
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A very tiny danbooru style image gallery!
<?php /* Copyright(c) 2013 Robert Maupin. Released under the ZLIB License. */
date_default_timezone_set('GMT');
define('DB','chan.db');
$max_image_size_kb = 1024*1024;
$thumbnail_size = 150;
$thumbnail_quality = 70;
$db = new PDO('sqlite:'.DB, 0, 0, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
function query($sql, $params = NULL) {
global $db;
$s = $db->prepare($sql);
$s->execute(array_values((array) $params));
return $s;
}
function ext($id) {
$ex=query('SELECT e FROM i WHERE i=?',$id)->fetch(PDO::FETCH_NUM);
return $ex[0];
}
if(!is_file(DB)) {
/* (I)mage: (I)D, FILE (M)D5, (E)xtension
* (T)ags: Image (I)D, (T)ext
*/
query('CREATE TABLE i (i INTEGER PRIMARY KEY,m BLOB(16) UNIQUE ON CONFLICT ROLLBACK,e TEXT)');
query('CREATE TABLE t (i INTEGER,t TEXT(32),PRIMARY KEY (i, t))');
}
function insert($table,$data) {
global $db;
query("INSERT INTO $table(" . join(',', array_keys($data)) . ')VALUES('
. str_repeat('?,', count($data)-1). '?)', $data);
return $db->lastInsertId();
}
function tags($id) {
return query('SELECT t FROM t WHERE i=?',$id)->fetchAll(PDO::FETCH_COLUMN);
}
if(count($_FILES) > 0) {
extract($_FILES['file']);
list($w,$h,$type)=getimagesize($tmp_name);
if(!$type||$type>3||filesize($tmp_name)>$max_image_size_kb) exit();
$fex=strlen($ext=image_type_to_extension($type,false))==4?'jpg':$ext;
$id=insert('i',array('m'=>$md5=md5_file($tmp_name),'e'=>$fex));
if($id < 1) exit();
insert('t',array('i'=>$id,'t'=>'untagged'));
move_uploaded_file($tmp_name,$n="img/$id.$fex");
$i=call_user_func('imagecreatefrom'.$ext,$n);
$s=$thumbnail_size;$r=$w/$h;$tw=$r<1?floor($s*$r):$s;$th=$r<1?$s:floor($s/$r);
$t=imagecreatetruecolor($tw,$th);
imagecopyresampled($t,$i,0,0,0,0,$tw,$th,$w,$h);
imagejpeg($t,"tmb/$id.jpg",$thumbnail_quality);
imagedestroy($t);
echo $id;
exit();
}
$post = isset($_GET['post']) ? $_GET['post']+0 : false;
if($post<0)$post=false;
else {
$utags = isset($_GET['utags']) ? $_GET['utags'] : false;
if($utags) {
$tags=tags($post);
$utags=explode(' ',$utags);
$remove=array_diff($tags,$utags);
if(($c=count($remove)) > 0)
query('DELETE FROM t WHERE i = ? AND t IN (?'.str_repeat(',?',$c-1).')',
array_merge(array($post),$remove));
$add=array_diff($utags,$tags);
if(count($add) > 0)
foreach($add as $t)
insert('t',array('i'=>$post,'t'=>$t));
header('Location: ?post='.$post);
exit();
}
}
$del = isset($_GET['del']) ? $_GET['del']+0 : false;
if($del<0)$del=false;
if($del) {
//delete the images and database entries
$ex=ext($del);
unlink("img/$del.$ex");
unlink("tmb/$del.jpg");
query('DELETE FROM t WHERE i = ?',$del);
query('DELETE FROM i WHERE i = ?',$del);
header('Location: index.php');
exit();
}
$tags = isset($_GET['tags']) ? trim($_GET['tags']) : false;
if(strlen($tags)<1)$tags=false;
?><!DOCTYPE html><html><head><meta charset="UTF-8" /><title>chibichan</title>
<style type="text/css">form,h3,figure { margin: 0; padding: 0 }
#thumb > figure {display: inline-block;vertical-align:middle;text-align:center;padding: 10px}
img {padding:2px;border:1px solid transparent} img:hover {border:1px solid #ddd}
header > form, header > h3 {display: inline-block;padding-right: 2em}
body { font-family: "Arial", "Helvetica", sans-serif; } a { color: #669 }</style>
<?php if(!$post) { ?><script type="text/javascript">var d = document.documentElement;
d.ondragover=function(){return false;};
d.ondragend=function(){return false;};
d.ondrop=function(e){
e.preventDefault();
var fd=new FormData();
fd.append('file', e.dataTransfer.files[0]);
var x=new XMLHttpRequest();
x.onreadystatechange=function() {
if(x.readyState != 4)
return;
if(x.responseText === 'ERR')
location.reload();
else
location.replace('?post='+x.responseText);
};
x.open('POST','index.php',false);
x.send(fd);
return false;
};</script><?php } ?></head><body>
<header><h3><a href="index.php">Chibichan</a></h3><form action="." method="get"><label>Search: </label>
<input type="text" name="tags" <?php if($tags) echo 'value="'.$tags.'"'; ?>><input style="display:none" type="submit" value="Update"></form></header><?php
if($post) {
echo '<section id="post">';
$ex=ext($post);
echo '<figure><img src="img/'."$post.$ex".'" alt=""></figure>';
$tags=implode(' ',tags($post));
echo '<form action="." method="get"><label>Tags: </label><input type="hidden" name="post" value="'.$post.'">'
.'<input type="text" name="utags" value="'.$tags.'"><input style="display:none" type="submit" value="Update"></form>'
.'<form action="." method="get"><input type="hidden" name="del" value="'.$post.'"><input type="submit" value="Delete"></form>';
echo '</section>';
} else {
echo '<section id="thumb">';
$imgs=false;
if($tags) {
$tags = explode(' ',$tags);
$count = count($tags);
$imgs = query('SELECT i FROM t WHERE t IN ('
. str_repeat('?,',$count-1).'?) GROUP BY i HAVING count(distinct t) = '.$count.' ORDER BY i DESC',$tags)->fetchAll(PDO::FETCH_COLUMN);
} else {
$imgs = query('SELECT i FROM i ORDER BY i DESC')->fetchAll(PDO::FETCH_COLUMN);
}
foreach($imgs as $i)
echo '<figure><a href="?post='.$i.'"><img src="tmb/'.$i.'.jpg" alt=""></a></figure>';
echo '</section>';
}
?></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.