page == "regen_thumb")) { global $user; if($user->is_admin() && isset($_POST['program']) && isset($_POST['image_id'])) { $this->make_thumb($_POST['program'], $_POST['image_id']); } } if(is_a($event, 'DisplayingImageEvent')) { global $page; global $user; if($user->is_admin()) { $page->add_side_block(new Block("Regen Thumb", $this->build_regen_buttons($event->image))); } } } // }}} // do things {{{ // FIXME: make locations of convert / epeg config variables private function make_thumb($program, $image_id) { global $database; global $config; $i_image_id = int_escape($image_id); $image = $database->get_image($i_image_id); $f_image = $this->check_filename($image->get_image_filename()); $f_thumb = $this->check_filename($image->get_thumb_filename()); $w = $config->get_int('thumb_width'); $h = $config->get_int('thumb_height'); $q = $config->get_int('thumb_quality'); switch($program) { case 'convert': if(file_exists($f_thumb)) unlink($f_thumb); exec("convert $f_image -geometry {$w}x{$h} -quality {$q} $f_thumb"); break; case 'gd': $this->make_thumb_gd($f_image, $f_thumb); break; default: break; } global $page; $page->set_title("Thumbnail Regenerated"); $page->set_heading("Thumbnail Regenerated"); $page->add_side_block(new NavBlock()); $page->add_main_block(new Block("Thumbnail", build_thumb_html($image))); } private function check_filename($filename) { $filename = preg_replace("#[^a-zA-Z0-9/\._]#", "", $filename); return $filename; } // }}} // GD thumber {{{ private function read_file($fname) { $fp = fopen($fname, "r"); if(!$fp) return false; $data = fread($fp, filesize($fname)); fclose($fp); return $data; } private function make_thumb_gd($inname, $outname) { global $config; $thumb = $this->get_thumb($inname); return imagejpeg($thumb, $outname, $config->get_int('thumb_quality')); } 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(); if($memory_use > $memory_limit) { $thumb = imagecreatetruecolor($max_width, min($max_height, 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; } } // }}} // page building {{{ private function build_regen_buttons($image) { global $user; if($user->is_admin()) { return "
"; } } // }}} } add_event_listener(new RegenThumb()); ?>