fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / vendor / dompdf / dompdf / include / image_cache.cls.php
blob7d7e5603b36edd31592bf13f41cc4a9a1f12e2a0
1 <?php
2 /**
3 * @package dompdf
4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Helmut Tischer <htischer@weihenstephan.org>
7 * @author Fabien Ménager <fabien.menager@gmail.com>
8 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
9 */
11 /**
12 * Static class that resolves image urls and downloads and caches
13 * remote images if required.
15 * @access private
16 * @package dompdf
18 class Image_Cache {
20 /**
21 * Array of downloaded images. Cached so that identical images are
22 * not needlessly downloaded.
24 * @var array
26 static protected $_cache = array();
28 /**
29 * The url to the "broken image" used when images can't be loade
31 * @var string
33 public static $broken_image;
35 /**
36 * Resolve and fetch an image for use.
38 * @param string $url The url of the image
39 * @param string $protocol Default protocol if none specified in $url
40 * @param string $host Default host if none specified in $url
41 * @param string $base_path Default path if none specified in $url
42 * @param DOMPDF $dompdf The DOMPDF instance
44 * @throws DOMPDF_Image_Exception
45 * @return array An array with two elements: The local path to the image and the image extension
47 static function resolve_url($url, $protocol, $host, $base_path, DOMPDF $dompdf) {
48 $parsed_url = explode_url($url);
49 $message = null;
51 $remote = ($protocol && $protocol !== "file://") || ($parsed_url['protocol'] != "");
53 $data_uri = strpos($parsed_url['protocol'], "data:") === 0;
54 $full_url = null;
55 $enable_remote = $dompdf->get_option("enable_remote");
57 try {
59 // Remote not allowed and is not DataURI
60 if ( !$enable_remote && $remote && !$data_uri ) {
61 throw new DOMPDF_Image_Exception("DOMPDF_ENABLE_REMOTE is set to FALSE");
64 // Remote allowed or DataURI
65 else if ( $enable_remote && $remote || $data_uri ) {
66 // Download remote files to a temporary directory
67 $full_url = build_url($protocol, $host, $base_path, $url);
69 // From cache
70 if ( isset(self::$_cache[$full_url]) ) {
71 $resolved_url = self::$_cache[$full_url];
74 // From remote
75 else {
76 $tmp_dir = $dompdf->get_option("temp_dir");
77 $resolved_url = tempnam($tmp_dir, "ca_dompdf_img_");
78 $image = "";
80 if ($data_uri) {
81 if ($parsed_data_uri = parse_data_uri($url)) {
82 $image = $parsed_data_uri['data'];
85 else {
86 set_error_handler("record_warnings");
87 $image = file_get_contents($full_url);
88 restore_error_handler();
91 // Image not found or invalid
92 if ( strlen($image) == 0 ) {
93 $msg = ($data_uri ? "Data-URI could not be parsed" : "Image not found");
94 throw new DOMPDF_Image_Exception($msg);
97 // Image found, put in cache and process
98 else {
99 //e.g. fetch.php?media=url.jpg&cache=1
100 //- Image file name might be one of the dynamic parts of the url, don't strip off!
101 //- a remote url does not need to have a file extension at all
102 //- local cached file does not have a matching file extension
103 //Therefore get image type from the content
104 file_put_contents($resolved_url, $image);
109 // Not remote, local image
110 else {
111 $resolved_url = build_url($protocol, $host, $base_path, $url);
114 // Check if the local file is readable
115 if ( !is_readable($resolved_url) || !filesize($resolved_url) ) {
116 throw new DOMPDF_Image_Exception("Image not readable or empty");
119 // Check is the file is an image
120 else {
121 list($width, $height, $type) = dompdf_getimagesize($resolved_url);
123 // Known image type
124 if ( $width && $height && in_array($type, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP)) ) {
125 //Don't put replacement image into cache - otherwise it will be deleted on cache cleanup.
126 //Only execute on successful caching of remote image.
127 if ( $enable_remote && $remote || $data_uri ) {
128 self::$_cache[$full_url] = $resolved_url;
132 // Unknown image type
133 else {
134 throw new DOMPDF_Image_Exception("Image type unknown");
138 catch(DOMPDF_Image_Exception $e) {
139 $resolved_url = self::$broken_image;
140 $type = IMAGETYPE_PNG;
141 $message = $e->getMessage()." \n $url";
144 return array($resolved_url, $type, $message);
148 * Unlink all cached images (i.e. temporary images either downloaded
149 * or converted)
151 static function clear() {
152 if ( empty(self::$_cache) || DEBUGKEEPTEMP ) return;
154 foreach ( self::$_cache as $file ) {
155 if (DEBUGPNG) print "[clear unlink $file]";
156 unlink($file);
159 self::$_cache = array();
162 static function detect_type($file) {
163 list(, , $type) = dompdf_getimagesize($file);
164 return $type;
167 static function type_to_ext($type) {
168 $image_types = array(
169 IMAGETYPE_GIF => "gif",
170 IMAGETYPE_PNG => "png",
171 IMAGETYPE_JPEG => "jpeg",
172 IMAGETYPE_BMP => "bmp",
175 return (isset($image_types[$type]) ? $image_types[$type] : null);
178 static function is_broken($url) {
179 return $url === self::$broken_image;
183 Image_Cache::$broken_image = DOMPDF_LIB_DIR . "/res/broken_image.png";