From fb9df0c269f36703909b8b259abbdbed29881ecd Mon Sep 17 00:00:00 2001 From: tcit Date: Thu, 24 Jul 2014 21:56:04 +0200 Subject: use directly MOBIClass --- .../libraries/MOBIClass/PreprocessedArticle.php | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 inc/3rdparty/libraries/MOBIClass/PreprocessedArticle.php (limited to 'inc/3rdparty/libraries/MOBIClass/PreprocessedArticle.php') diff --git a/inc/3rdparty/libraries/MOBIClass/PreprocessedArticle.php b/inc/3rdparty/libraries/MOBIClass/PreprocessedArticle.php new file mode 100644 index 00000000..2e992404 --- /dev/null +++ b/inc/3rdparty/libraries/MOBIClass/PreprocessedArticle.php @@ -0,0 +1,89 @@ +text = $textData; + $this->metadata = $metadata; + + $this->images = $this->downloadImages($imageLinks); + } + + /** + * Create a Preprocessed article from a json string + * @param string $json JSON data. Should be of the following format: + * {"text": "TEXT", "images: ["imageURL1", "imageURL2"], "metadata": {"key": "value"}} + * + * Note: Any image tags should have the recindex attribute set to the appropriate index (the + * same index as the image in the array) + * @return PreprocessedArticle The generated preprocessed array + */ + static public function CreateFromJson($json){ + $data = json_decode($json); + return new PreprocessedArticle($data["text"], $data["images"], $data["metadata"]); + } + + /** + * Get the text data to be integrated in the MOBI file + * @return string + */ + public function getTextData(){ + return $this->text; + } + /** + * Get the images (an array containing the jpeg data). Array entry 0 will + * correspond to image record 0. + * @return array + */ + public function getImages(){ + return $this->images; + } + /** + * Get the metadata in the form of a hashtable (for example, title or author). + * @return array + */ + public function getMetaData(){ + return $this->metadata; + } + /** + * + * @param DOMElement $dom + * @return array + */ + private function downloadImages($links){ + $images = array(); + foreach($links as $link) { + $imgFile = @imagecreatefromstring(Http::Request($link)); + + if($imgFile === false){ + $imgFile = @imagecreate(1, 1); + $black = @imagecolorallocate($imgFile, 255, 255, 255); + } + if($imgFile !== false){ + @imagefilter($imgFile, IMG_FILTER_GRAYSCALE); + + ob_start(); + @imagejpeg($imgFile); + $image = ob_get_contents(); + ob_end_clean(); + + $images[$this->imgCounter] = new FileRecord(new Record($image)); + imagedestroy($imgFile); + + $this->imgCounter++; + } + } + + return $images; + } +} +?> -- cgit v1.2.3