Permalink
Browse files

changed up now images are rendered. should cut down alot of pageload …

…time by bypassing php
  • Loading branch information...
1 parent 27614cc commit ae38f3fb90a124891a69778224ed01c02cebd728 @vicapow vicapow committed Mar 4, 2013
View
1 .gitignore
@@ -14,3 +14,4 @@ composer.phar
TRASH
.DS_Store
todo.txt
+public/rendered-images
View
26 libs/OCAL.php
@@ -46,6 +46,20 @@ function nsfw() {
else if(isset($this->nsfw)) return $this->nsfw;
return false;
}
+ function clipart_has_tag($id, $tag){
+ $tag = $this->db->escape($tag);
+ $id = intval($id);
+ $query = "SELECT count(*)
+ FROM openclipart_clipart
+ INNER JOIN openclipart_clipart_tags ON clipart = openclipart_clipart.id
+ INNER JOIN openclipart_tags ON tag = openclipart_tags.id
+ WHERE openclipart_clipart.id = $id
+ AND name = '$tag'";
+ return $app->db->get_value($query) !== 0;
+ }
+ function clipart_is_nsfw($id){
+ return $this->clipart_has_tag($id, 'nsfw');
+ }
function is_clipart_favorite($clipart, $userid){
$clipart = intval($clipart);
$userid = intval($userid);
@@ -476,6 +490,18 @@ function get_clipart($id){
WHERE openclipart_clipart.id = $id";
return $this->db->get_row($query);
}
+ function get_clipart_path($id){
+ // serve the same svg in svg debug mode. this prevents having to download
+ // the entire set of svg files during development
+ if( isset($this->config->svg_debug) && $this->config->svg_debug)
+ return $dir . $this->config->example_svg;
+ $clipart = $this->get_clipart($id);
+ if(!$clipart) return;
+ $username = $clipart['username'];
+ $filename = $clipart['filename'];
+ $dir = $this->config->root_directory;
+ return $dir . "/people/$username/$filename";
+ }
function user_num_remixes($id){
$id= intval($id);
$query = "SELECT COUNT(*)
View
5 libs/System.php
@@ -376,4 +376,9 @@ function redirect($url, $qs = array(), $status = 302){
if(!empty($qs)) $url .= '?' . http_build_query($qs);
parent::redirect($url, $status);
}
+ function end($body, $status = 200){
+ $this->status($status);
+ $res = $this->response();
+ $res->body($body);
+ }
}
View
4 routes/clipart.php
@@ -48,8 +48,8 @@
$editable = false;
$user = $app->user();
// librarians can edit any clipart
- if (isset($app->username)) {
- if ($app->username == $clipart['username'] || $app->is('librarian')) {
+ if (isset($user['username'])) {
+ if ($user['username'] == $clipart['username'] || $app->is('librarian')) {
$editable = true;
}
}
View
128 routes/image.php
@@ -1,91 +1,57 @@
<?php
-
-$serve_image_func = function($width, $user, $file, $download = false) use($app) {
- $width = intval($width);
- $dir = $app->config->root_directory;
- $svg_filename = preg_replace("/.png$/", '.svg', $file);
- $png = $dir . "/people/$user/${width}px-$file";
- if(!isset($app->config->svg_debug) || !$app->config->svg_debug)
- $svg = $dir . "/people/$user/" . $svg_filename;
- else $svg = $dir . $app->config->example_svg;
- $response = $app->response();
-
- $max_res = $app->config->bitmap_resolution_limit;
-
- if ($width > $max_res) {
- $response->status(400);
- // TODO: Generate Error Image
- echo "Resolution couldn't be higher then $max_res px! Please "
- . "download SVG and produce the bitmap locally.";
- } else if ( !file_exists($svg) || filesize($svg) == 0 ){
- return $app->status(404);
- } else {
- $file = $app->db->escape($file);
- $user = $app->db->escape($user);
- $query = "SELECT count(*)
- FROM openclipart_clipart
- INNER JOIN openclipart_users
- ON owner = openclipart_users.id
- INNER JOIN openclipart_clipart_tags ON clipart = openclipart_clipart.id
- INNER JOIN openclipart_tags ON tag = openclipart_tags.id
- WHERE filename = '$file'
- AND username = '$user'
- AND name = 'nsfw'";
- if ($app->nsfw() && $app->db->get_value($query) != 0) {
- $user = $app->config->nsfw_image['user'];
- $filename = $app->config->nsfw_image['filename'];
- $png = $dir . "/people/$user/${width}px-$file-nsfw.png";
- if(!isset($app->config->svg_debug) || !$app->config->svg_debug){
- $svg = $dir . "/people/$user/$filename.svg";
- }else{
- // for develoment so we dont have to rsync ALL the images, just always load the same one
- $svg = $dir . $app->config->example_svg;
- }
- }
- if (file_exists($png)) {
- if($download){
- $response->header('Content-Disposition: attachment; filename=' . basename($png));
- $response->header('Content-Type', 'application/octet-stream');
- }else $response->header('Content-Type', 'image/png');
- echo file_get_contents($png);
- }else{
- $height = $width;
- $app->svg->raster($svg, $width, $height, $png);
- if(!file_exists($png)){
- $app->status(404);
- }else{
- if($download){
- $response->header('Content-Disposition: attachment; filename=' . basename($png));
- $response->header('Content-Type', 'application/octet-stream');
- }else $response->header('Content-Type', 'image/png');
- echo file_get_contents($png);
- }
- }
- }
+$serve_image_func = function($width, $id, $filename = null) use($app) {
+ $width = intval($width);
+ $svg = $app->get_clipart_path($id);
+ if(!$svg) return $app->end("no clipart found in db with id $id", 404);
+ $dir = $app->config->root_directory;
+ $png = $dir . "/rendered-images/$width/$id";
+ $response = $app->response();
+ $max_res = $app->config->bitmap_resolution_limit;
+ if ($width > $max_res)
+ return $app->end("Resolution cant be higher then $max_res px."
+ . "Please download the SVG and reproduce large versions locally.",400);
+ if ( !file_exists($svg) || filesize($svg) == 0 )
+ return $app->status("the clipart exists in the db but the coordisponding"
+ . "svg image could not be found at $svg",404);
+ // NOTE: it should be up to the view to decide to show the nsfw overlay
+ if (file_exists($png)) {
+ if($filename){
+ $response->header('Content-Disposition: attachment; filename='
+ . $filename);
+ $response->header('Content-Type', 'application/octet-stream');
+ }else $response->header('Content-Type', 'image/png');
+ echo file_get_contents($png);
+ return;
+ }else{
+ $height = $width;
+ $app->svg->raster($svg, $width, $height, $png);
+ if(!file_exists($png))
+ return $app->end("unable to generate png for svg $svg",404);
+ if($filename){
+ $response->header('Content-Disposition: attachment; filename='
+ . $filename);
+ $response->header('Content-Type', 'application/octet-stream');
+ }else $response->header('Content-Type', 'image/png');
+ echo file_get_contents($png);
+ return;
+ }
};
-// NON-DOWNLOAD //
-
-// no username provided
-$app->get("/image/:width/:file", function($width, $file) use($app, $serve_image_func) {
- call_user_func($serve_image_func,$width, '', $file);
+$app->get("/rendered-images/:width/:id", function($width, $id) use($app, $serve_image_func){
+ call_user_func($serve_image_func, $width, $id);
});
-// username provided
-$app->get("/image/:width/:user/:file", $serve_image_func);
-
-
-// DOWNLOAD //
-
-// no username provided
-$app->get("/image/download/:width/:filename", function($width, $filename) use($app, $serve_image_func){
- call_user_func($serve_image_func,$width, '', $file, true);
+// Download the file as an attachment //
+$app->get("/rendered-images/download/:width/:id/:filename", function($width, $id, $filename) use($app, $serve_image_func){
+ $filename = preg_replace("/.svg$/", ".png", $filename);
+ call_user_func($serve_image_func, $width, $id, $filename);
});
-
-// username provided
-$app->get("/image/download/:width/:username/:filename", function($width, $username, $filename) use($app, $serve_image_func){
- call_user_func($serve_image_func, $width, $username, $filename, true);
+$app->get("/rendered-images/download/:width/:id", function($width, $id) use($app, $serve_image_func){
+ if(isset($_GET['filename'])) $filename = $_GET['filename'];
+ else $filename = 'image.png';
+ $filename = preg_replace("/.svg$/", ".png", $filename);
+ call_user_func($serve_image_func, $width, $id, $filename);
});
?>
View
15 templates/clipart/detail.template
@@ -34,7 +34,7 @@
<a href="/download/svg/{{username}}/{{filename}}">
<div class="detail-image">
<div class="checker"></div>
- <img src="/image/280/{{username}}/{{filename_png}}" alt="{{title}}" title='"{{title}}" by {{username}}'/>
+ <img src="/rendered-images/280/{{id}}" alt="{{title}}" title='"{{title}}" by {{username}}'/>
</div>
</a>
</div>
@@ -67,9 +67,12 @@
</a>
<ul class="dropdown-menu">
<!-- links -->
- <li><a tabindex="-1" href="/image/download/100/{{username}}/{{filename_png}}">100px</a></li>
- <li><a tabindex="-1" href="/image/download/200/{{username}}/{{filename_png}}">200px</a></li>
- <li><a tabindex="-1" href="/image/download/500/{{username}}/{{filename_png}}">500px</a></li>
+ <li><a tabindex="-1"
+ href="/rendered-images/download/100/{{id}}/{{id}}-{{filename_png}}">100px</a></li>
+ <li><a tabindex="-1"
+ href="/rendered-images/download/200/{{id}}/{{id}}-{{filename_png}}">200px</a></li>
+ <li><a tabindex="-1"
+ href="/rendered-images/download/500/{{id}}/{{id}}-{{filename_png}}">500px</a></li>
</ul>
</li>
</ul>
@@ -88,7 +91,8 @@
<h5>Remixes</h5>
<div class="remixes">
{% for remix in remixes %}
- <a href="/clipart/{{remix.id}}/{{remix.link}}"><img src="/image/75/{{remix.username}}/{{remix.filename}}" /></a>
+ <a href="/clipart/{{remix.id}}/{{remix.link}}"><img
+ src="/rendered-images/75/{{remix.id}}" /></a>
{% endfor %}
</div>
</div>
@@ -176,7 +180,6 @@
</tr>
</table>
</div>
-
{% if editable %}
<div class="span6">
<div class="edit-menu pull-right">
View
2 templates/clipart/edit.template
@@ -5,7 +5,7 @@
<div class="span1"></div>
<div class="span5 center">
<h2> Edit Clipart Details </h2>
- <img src="/image/280/{{username}}/{{filename_png}}" alt="{{title}}" title='"{{title}}" by {{username}}'/>
+ <img src="/rendered-images/280/{{clipart.id}}" alt="{{title}}" title='"{{title}}" by {{username}}'/>
</div>
<div class="span5">
<form class="form-horizontal" method="POST">
View
4 templates/clipart/item.template
@@ -1,8 +1,8 @@
<li class="span2">
<a href="/clipart/{{clipart.id}}/{{clipart.link}}{{clipart.forward_query}}" title="{{clipart.title}}" class="thumbnail">
<div class="image-center">
- <img src="/image/100/{{clipart.username ? clipart.username ~ '/' : ''}}{{clipart.filename_png}}{{clipart.forward_query}}">
+ <img src="/rendered-images/100/{{clipart.id}}">
</div>
<h5>{{clipart.title}}</h5>
</a>
-</li>
+</li>
View
2 templates/upload.template
@@ -90,7 +90,7 @@
<div class="span12 remix-banner">
<div class="row">
<div class="span2 offset3 thumb">
- <img src="/image/100/{{clipart.username}}/{{clipart.filename_png}}">
+ <img src="/rendered-images/100/{{clipart.id}}">
</div>
<div class="span4">
<h4> Remixing: </h4>

0 comments on commit ae38f3f

Please sign in to comment.