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_svg/main.php

123 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2009-08-20 22:37:17 +00:00
/*
* Name: Handle SVG
* Author: Shish <webmaster@shishnet.org>
* Link: http://code.shishnet.org/shimmie2/
* Description: Handle SVG files. (No thumbnail is generated for SVG files)
*/
class SVGFileHandler extends Extension {
2012-02-08 11:45:35 +00:00
public function onDataUpload(DataUploadEvent $event) {
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
$hash = $event->hash;
if(!move_upload_to_archive($event)) return;
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
2010-02-02 01:04:38 +00:00
$image = $this->create_image_from_data(warehouse_path("images", $hash), $event->metadata);
if(is_null($image)) {
throw new UploadException("SVG 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;
}
2012-02-08 11:45:35 +00:00
}
2012-02-08 11:45:35 +00:00
public function onThumbnailGeneration(ThumbnailGenerationEvent $event) {
if($this->supported_ext($event->type)) {
$hash = $event->hash;
2012-02-08 11:45:35 +00:00
copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash));
}
2012-02-08 11:45:35 +00:00
}
2012-02-08 11:45:35 +00:00
public function onDisplayingImage(DisplayingImageEvent $event) {
global $page;
if($this->supported_ext($event->image->ext)) {
2009-07-16 19:20:53 +00:00
$this->theme->display_image($page, $event->image);
}
2012-02-08 11:45:35 +00:00
}
2009-01-04 19:18:37 +00:00
2012-02-08 11:45:35 +00:00
public function onPageRequest(PageRequestEvent $event) {
2015-09-12 10:43:28 +00:00
global $page;
2012-02-08 11:45:35 +00:00
if($event->page_matches("get_svg")) {
$id = int_escape($event->get_arg(0));
$image = Image::by_id($id);
$hash = $image->hash;
2009-01-04 19:18:37 +00:00
2009-07-16 19:20:53 +00:00
$page->set_type("image/svg+xml");
$page->set_mode("data");
2010-02-02 01:04:38 +00:00
$page->set_data(file_get_contents(warehouse_path("images", $hash)));
}
}
/**
2015-09-27 11:38:48 +00:00
* @param string $ext
* @return bool
*/
private function supported_ext($ext) {
$exts = array("svg");
2009-06-05 19:53:00 +00:00
return in_array(strtolower($ext), $exts);
}
/**
2015-09-27 11:38:48 +00:00
* @param string $filename
* @param mixed[] $metadata
* @return Image
*/
private function create_image_from_data($filename, $metadata) {
$image = new Image();
$msp = new MiniSVGParser($filename);
$image->width = $msp->width;
$image->height = $msp->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'];
2009-01-24 19:05:07 +00:00
$image->tag_array = Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
return $image;
}
/**
2015-09-27 11:38:48 +00:00
* @param string $file
* @return bool
*/
private function check_contents($file) {
if(!file_exists($file)) return false;
2009-01-04 19:18:37 +00:00
$msp = new MiniSVGParser($file);
return bool_escape($msp->valid);
}
}
class MiniSVGParser {
/** @var bool */
public $valid=false;
/** @var int */
public $width=0;
/** @var int */
public $height=0;
/** @param string $file */
2014-03-22 09:00:59 +00:00
function __construct($file) {
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, "startElement"), array($this, "endElement"));
$this->valid = bool_escape(xml_parse($xml_parser, file_get_contents($file), true));
xml_parser_free($xml_parser);
}
function startElement($parser, $name, $attrs) {
if($name == "SVG") {
$this->width = int_escape($attrs["WIDTH"]);
$this->height = int_escape($attrs["HEIGHT"]);
}
}
function endElement($parser, $name) {
}
}