2007-12-06 02:26:34 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Pixel File Handler
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Description: Handle JPG, PNG, GIF, etc files
|
|
|
|
*/
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class PixelFileHandler implements Extension {
|
2007-12-06 02:26:34 +00:00
|
|
|
var $theme;
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2008-09-06 17:10:27 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-12-06 02:26:34 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
2007-12-06 02:26:34 +00:00
|
|
|
$hash = $event->hash;
|
|
|
|
$ha = substr($hash, 0, 2);
|
2008-07-21 11:13:25 +00:00
|
|
|
if(!move_upload_to_archive($event)) return;
|
2007-12-06 02:26:34 +00:00
|
|
|
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("Pixel Handler failed to create image object from data");
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|
2007-12-08 02:48:50 +00:00
|
|
|
|
|
|
|
$iae = new ImageAdditionEvent($event->user, $image);
|
2009-01-04 14:01:59 +00:00
|
|
|
send_event($iae); // this might raise an exception, but all we'd do is re-throw it...
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) {
|
2007-12-06 02:26:34 +00:00
|
|
|
$this->create_thumb($event->hash);
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) {
|
2007-12-06 02:26:34 +00:00
|
|
|
$this->theme->display_image($event->page, $event->image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function supported_ext($ext) {
|
|
|
|
$exts = array("jpg", "jpeg", "gif", "png");
|
2008-04-07 23:11:47 +00:00
|
|
|
return array_contains($exts, strtolower($ext));
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-06 02:26:34 +00:00
|
|
|
private function create_image_from_data($filename, $metadata) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$image = new Image();
|
|
|
|
|
|
|
|
$info = "";
|
|
|
|
if(!($info = getimagesize($filename))) return null;
|
|
|
|
|
|
|
|
$image->width = $info[0];
|
|
|
|
$image->height = $info[1];
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-06 02:26:34 +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']);
|
2007-12-06 02:26:34 +00:00
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function check_contents($file) {
|
2008-03-29 03:59:34 +00:00
|
|
|
$valid = Array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG);
|
|
|
|
if(!file_exists($file)) return false;
|
|
|
|
$info = getimagesize($file);
|
|
|
|
if(is_null($info)) return false;
|
|
|
|
if(array_contains($valid, $info[2])) return true;
|
|
|
|
return false;
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function create_thumb($hash) {
|
|
|
|
$ha = substr($hash, 0, 2);
|
|
|
|
$inname = "images/$ha/$hash";
|
|
|
|
$outname = "thumbs/$ha/$hash";
|
|
|
|
global $config;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-06 02:26:34 +00:00
|
|
|
$ok = false;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-06 02:26:34 +00:00
|
|
|
switch($config->get_string("thumb_engine")) {
|
|
|
|
default:
|
|
|
|
case 'gd':
|
|
|
|
$ok = $this->make_thumb_gd($inname, $outname);
|
|
|
|
break;
|
|
|
|
case 'convert':
|
|
|
|
$ok = $this->make_thumb_convert($inname, $outname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IM thumber {{{
|
|
|
|
private function make_thumb_convert($inname, $outname) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$w = $config->get_int("thumb_width");
|
|
|
|
$h = $config->get_int("thumb_height");
|
|
|
|
$q = $config->get_int("thumb_quality");
|
|
|
|
$mem = $config->get_int("thumb_max_memory") / 1024 / 1024; // IM takes memory in MB
|
|
|
|
|
|
|
|
// "-limit memory $mem" broken?
|
|
|
|
exec("convert {$inname}[0] -geometry {$w}x{$h} -quality {$q} jpg:$outname");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// }}}
|
2008-04-29 22:52:06 +00:00
|
|
|
// epeg thumber {{{
|
|
|
|
private function make_thumb_epeg($inname, $outname) {
|
|
|
|
global $config;
|
|
|
|
$w = $config->get_int("thumb_width");
|
|
|
|
exec("epeg $inname -c 'Created by EPEG' --max $w $outname");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// }}}
|
2007-12-06 02:26:34 +00:00
|
|
|
// GD thumber {{{
|
|
|
|
private function make_thumb_gd($inname, $outname) {
|
|
|
|
global $config;
|
|
|
|
$thumb = $this->get_thumb($inname);
|
2008-01-26 11:40:50 +00:00
|
|
|
$ok = imagejpeg($thumb, $outname, $config->get_int('thumb_quality'));
|
|
|
|
imagedestroy($thumb);
|
|
|
|
return $ok;
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function get_thumb($tmpname) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$info = getimagesize($tmpname);
|
|
|
|
$width = $info[0];
|
|
|
|
$height = $info[1];
|
|
|
|
|
|
|
|
$memory_use = (filesize($tmpname)*2) + ($width*$height*4) + (4*1024*1024);
|
|
|
|
$memory_limit = get_memory_limit();
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-06 02:26:34 +00:00
|
|
|
if($memory_use > $memory_limit) {
|
|
|
|
$w = $config->get_int('thumb_width');
|
|
|
|
$h = $config->get_int('thumb_height');
|
|
|
|
$thumb = imagecreatetruecolor($w, min($h, 64));
|
|
|
|
$white = imagecolorallocate($thumb, 255, 255, 255);
|
|
|
|
$black = imagecolorallocate($thumb, 0, 0, 0);
|
|
|
|
imagefill($thumb, 0, 0, $white);
|
|
|
|
imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black);
|
|
|
|
return $thumb;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$image = imagecreatefromstring($this->read_file($tmpname));
|
|
|
|
$tsize = get_thumbnail_size($width, $height);
|
|
|
|
|
|
|
|
$thumb = imagecreatetruecolor($tsize[0], $tsize[1]);
|
|
|
|
imagecopyresampled(
|
|
|
|
$thumb, $image, 0, 0, 0, 0,
|
|
|
|
$tsize[0], $tsize[1], $width, $height
|
|
|
|
);
|
|
|
|
return $thumb;
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 03:09:42 +00:00
|
|
|
|
|
|
|
private function read_file($fname) {
|
|
|
|
$fp = fopen($fname, "r");
|
|
|
|
if(!$fp) return false;
|
|
|
|
|
|
|
|
$data = fread($fp, filesize($fname));
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2007-12-06 02:26:34 +00:00
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new PixelFileHandler());
|
|
|
|
?>
|