This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/handle_ico/main.php

98 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2009-08-20 22:37:17 +00:00
/*
* Name: Handle ICO
* Author: Shish <webmaster@shishnet.org>
* Description: Handle windows icons
*/
class IcoFileHandler extends Extension {
public function onDataUpload(DataUploadEvent $event) {
2009-05-11 21:09:24 +00:00
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
$hash = $event->hash;
$ha = substr($hash, 0, 2);
move_upload_to_archive($event);
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
$image = $this->create_image_from_data("images/$ha/$hash", $event->metadata);
if(is_null($image)) {
throw new UploadException("Icon handler failed to create image object from data");
}
$iae = new ImageAdditionEvent($image);
2009-07-16 19:20:53 +00:00
send_event($iae);
$event->image_id = $iae->image->id;
}
2009-05-11 21:09:24 +00:00
}
public function onThumbnailGeneration(ThumbnailGenerationEvent $event) {
2009-05-11 21:09:24 +00:00
if($this->supported_ext($event->type)) {
$this->create_thumb($event->hash);
}
2009-05-11 21:09:24 +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);
}
2009-05-11 21:09:24 +00:00
}
2017-09-19 17:55:43 +00:00
private function supported_ext(string $ext): bool {
$exts = array("ico", "ani", "cur");
2009-06-05 19:53:00 +00:00
return in_array(strtolower($ext), $exts);
}
2009-01-04 19:18:37 +00:00
2017-09-19 17:55:43 +00:00
private function create_image_from_data(string $filename, array $metadata) {
$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
2016-09-02 06:16:07 +00:00
$width = $subheader['width'];
$height = $subheader['height'];
$image->width = $width == 0 ? 256 : $width;
$image->height = $height == 0 ? 256 : $height;
2009-01-04 19:18:37 +00:00
$image->filesize = $metadata['size'];
$image->hash = $metadata['hash'];
$image->filename = $metadata['filename'];
$image->ext = $metadata['extension'];
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;
}
2017-09-19 17:55:43 +00:00
private function check_contents(string $file): bool {
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));
}
2017-09-19 17:55:43 +00:00
private function create_thumb(string $hash): bool {
global $config;
2010-02-02 01:04:38 +00:00
$inname = warehouse_path("images", $hash);
$outname = warehouse_path("thumbs", $hash);
$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
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;
}
}