2008-04-08 21:58:17 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2010-01-05 10:32:39 +00:00
|
|
|
* Name: Handle ICO
|
2008-04-08 21:58:17 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Description: Handle windows icons
|
|
|
|
*/
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class IcoFileHandler extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
2008-04-08 21:58:17 +00:00
|
|
|
$hash = $event->hash;
|
|
|
|
$ha = substr($hash, 0, 2);
|
|
|
|
if(!move_upload_to_archive($event)) return;
|
|
|
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
|
|
|
$image = $this->create_image_from_data("images/$ha/$hash", $event->metadata);
|
|
|
|
if(is_null($image)) {
|
2009-01-04 14:01:59 +00:00
|
|
|
throw new UploadException("Icon handler failed to create image object from data");
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|
2012-08-18 18:45:39 +00:00
|
|
|
$iae = new ImageAdditionEvent($image);
|
2009-07-16 19:20:53 +00:00
|
|
|
send_event($iae);
|
|
|
|
$event->image_id = $iae->image->id;
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-04-08 21:58:17 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onThumbnailGeneration(ThumbnailGenerationEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
if($this->supported_ext($event->type)) {
|
2008-04-08 21:58:17 +00:00
|
|
|
$this->create_thumb($event->hash);
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-04-08 21:58:17 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
2009-07-16 19:20:53 +00:00
|
|
|
global $page;
|
2009-05-11 21:09:24 +00:00
|
|
|
if($this->supported_ext($event->image->ext)) {
|
2009-07-16 19:20:53 +00:00
|
|
|
$this->theme->display_image($page, $event->image);
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2008-04-14 10:50:46 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2009-07-16 19:20:53 +00:00
|
|
|
global $config, $database, $page;
|
2009-05-11 21:09:24 +00:00
|
|
|
if($event->page_matches("get_ico")) {
|
2008-04-14 10:50:46 +00:00
|
|
|
$id = int_escape($event->get_arg(0));
|
2009-05-11 14:04:33 +00:00
|
|
|
$image = Image::by_id($id);
|
2008-04-14 10:50:46 +00:00
|
|
|
$hash = $image->hash;
|
|
|
|
$ha = substr($hash, 0, 2);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
$page->set_type("image/x-icon");
|
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_data(file_get_contents("images/$ha/$hash"));
|
2008-04-14 10:50:46 +00:00
|
|
|
}
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2008-04-08 21:58:17 +00:00
|
|
|
private function supported_ext($ext) {
|
|
|
|
$exts = array("ico", "ani", "cur");
|
2009-06-05 19:53:00 +00:00
|
|
|
return in_array(strtolower($ext), $exts);
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-08 21:58:17 +00:00
|
|
|
private function create_image_from_data($filename, $metadata) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$image = new Image();
|
|
|
|
|
|
|
|
$fp = fopen($filename, "r");
|
|
|
|
$header = unpack("snull/stype/scount", fread($fp, 6));
|
|
|
|
|
|
|
|
$subheader = unpack("cwidth/cheight/ccolours/cnull/splanes/sbpp/lsize/loffset", fread($fp, 16));
|
|
|
|
fclose($fp);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-08 21:58:17 +00:00
|
|
|
$image->width = $subheader['width'];
|
|
|
|
$image->height = $subheader['height'];
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-04-08 21:58:17 +00:00
|
|
|
$image->filesize = $metadata['size'];
|
|
|
|
$image->hash = $metadata['hash'];
|
|
|
|
$image->filename = $metadata['filename'];
|
|
|
|
$image->ext = $metadata['extension'];
|
2009-01-24 19:05:07 +00:00
|
|
|
$image->tag_array = Tag::explode($metadata['tags']);
|
2008-04-08 21:58:17 +00:00
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function check_contents($file) {
|
|
|
|
if(!file_exists($file)) return false;
|
|
|
|
$fp = fopen($file, "r");
|
|
|
|
$header = unpack("snull/stype/scount", fread($fp, 6));
|
|
|
|
fclose($fp);
|
|
|
|
return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function create_thumb($hash) {
|
|
|
|
global $config;
|
|
|
|
|
2010-02-02 01:04:38 +00:00
|
|
|
$inname = warehouse_path("images", $hash);
|
|
|
|
$outname = warehouse_path("thumbs", $hash);
|
2008-04-08 21:58:17 +00:00
|
|
|
|
|
|
|
$w = $config->get_int("thumb_width");
|
|
|
|
$h = $config->get_int("thumb_height");
|
|
|
|
$q = $config->get_int("thumb_quality");
|
2012-05-22 11:26:47 +00:00
|
|
|
$mem = $config->get_int("thumb_mem_limit") / 1024 / 1024; // IM takes memory in MB
|
2008-04-08 21:58:17 +00:00
|
|
|
|
|
|
|
if($config->get_bool("ico_convert")) {
|
|
|
|
// "-limit memory $mem" broken?
|
|
|
|
exec("convert {$inname}[0] -geometry {$w}x{$h} -quality {$q} jpg:$outname");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copy($inname, $outname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|