another minor fix to prior commit
[openemr.git] / library / classes / thumbnail / ThumbnailGenerator.php
blobc36aa01379b5bd11c02e4cdb1cfab7f80798f411
1 <?php
2 /**
3 * Adding thumbnails to all files
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 3
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Amiel Elboim <amielel@matrix.co.il>
18 * @link http://www.open-emr.org
21 require_once dirname(__FILE__) . '/../../../interface/globals.php';
22 require_once dirname(__FILE__) . '/Thumbnail.class.php';
23 require_once dirname(__FILE__) . '/../CouchDB.class.php';
25 /**
26 * Class ThumbnailGenerator
28 class ThumbnailGenerator{
30 public static $types_support = array('image/png', 'image/jpeg', 'image/jpg', 'image/gif');
31 private $thumb_obj = null;
32 private $couch_obj = null;
34 /**
35 * ThumbnailGenerator constructor.
37 public function __construct()
39 $thumb_size = ($GLOBALS['thumb_doc_max_size'] > 0) ? $GLOBALS['thumb_doc_max_size'] : null;
40 $this->thumb_obj = new Thumbnail($thumb_size);
43 /**
44 * @return array
46 public static function get_types_support()
48 foreach(self::$types_support as $value){
49 $types_support[] = "'$value'";
51 return $types_support;
54 static function count_not_generated()
56 $sql = "SELECT COUNT(*) AS c FROM documents
57 WHERE mimetype IN (" . implode(',', self::get_types_support()) . ") AND thumb_url IS NULL";
59 $results = sqlStatement($sql);
60 $row = sqlFetchArray($results);
62 return $row['c'];
65 /**
66 * Generating all files with match format that still isn't generated
67 * @return array $result (count failed/success)
69 public function generate_all()
72 $feedback = array('sum_success' => 0, 'sum_failed' => 0, 'success' => array(), 'failed' => array());
74 $sql = "SELECT id, url, couch_docid, storagemethod, path_depth FROM documents
75 WHERE mimetype IN (" . implode(',', self::get_types_support()) . ") AND thumb_url IS NULL";
77 $results = sqlStatement($sql);
78 while($row = sqlFetchArray($results)) {
80 switch((int)$row['storagemethod']) {
81 //for hard disk store
82 case 0:
83 $new_file = $this->generate_HD_file($row['url'], $row['path_depth']);
84 break;
85 //for CouchDB store
86 case 1:
87 $new_file = $this->generate_couch_file($row['couch_docid'], $row['url']);
88 break;
89 default:
90 $this->error_log($row['url']);continue;
92 // Write error to log if failed
93 if (!$new_file) {
94 $this->error_log($row['url']);
95 $feedback['sum_failed'] ++;
96 $feedback['failed'][] = $row['url'];
97 continue;
100 $sql = "UPDATE documents SET thumb_url = ? WHERE id = ?";
101 $update = sqlStatement($sql, array($new_file, $row['id']));
102 if($update) {
103 $feedback['sum_success'] ++;
104 $feedback['success'][] = $row['url'];
108 return $feedback;
112 * Generate new file and store it in hard disk
113 * @param $url
114 * @return bool|string
116 private function generate_HD_file($url, $path_depth)
118 //remove 'file://'
119 $url = preg_replace("|^(.*)://|","",$url);
121 //change full path to current webroot. this is for documents that may have
122 //been moved from a different filesystem and the full path in the database
123 //is not current. this is also for documents that may of been moved to
124 //different patients. Note that the path_depth is used to see how far down
125 //the path to go. For example, originally the path_depth was always 1, which
126 //only allowed things like documents/1/<file>, but now can have more structured
127 //directories. For example a path_depth of 2 can give documents/encounters/1/<file>
128 // etc.
129 // NOTE that $from_filename and basename($url) are the same thing
130 $from_all = explode("/",$url);
131 $from_filename = array_pop($from_all);
132 $from_pathname_array = array();
133 for ($i=0;$i<$path_depth;$i++) {
134 $from_pathname_array[] = array_pop($from_all);
136 $from_pathname_array = array_reverse($from_pathname_array);
137 $from_pathname = implode("/",$from_pathname_array);
139 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
141 if (file_exists($temp_url)) {
142 $url = $temp_url;
143 } else {
144 return false;
147 $path_parts = pathinfo($url);
149 $resource = $this->thumb_obj->create_thumbnail($url);
150 if(!$resource) {
151 return false;
154 $thumb_name = $this->get_thumb_name($path_parts['basename']);
155 $full_path = $path_parts['dirname'] . '/' . $thumb_name;
156 $new_thumb_file = $this->thumb_obj->image_to_file($resource, $full_path);
158 return ($new_thumb_file) ? 'file://' . $full_path : false;
163 * Generate new file and store it in CouchDB
164 * @param $doc_id
165 * @return bool|string
167 private function generate_couch_file($doc_id, $file_name)
169 if( is_null($this->couch_obj)) {
170 $this->couch_obj = new CouchDB();
172 $data = array($GLOBALS['couchdb_dbase'],$doc_id);
173 $resp = $this->couch_obj->retrieve_doc($data);
175 if(empty($resp->data)) {
176 return false;
179 $resource = $this->thumb_obj->create_thumbnail(null,base64_decode($resp->data));
180 if(!$resource) {
181 return false;
184 $new_file_content = $this->thumb_obj->get_string_file($resource);
186 if(!$new_file_content) {
187 return false;
189 $couch_row = get_object_vars($resp);
191 $couch_row['th_data'] = json_encode(base64_encode($new_file_content));
192 $array_update = array_values($couch_row);
193 array_unshift($array_update,$GLOBALS['couchdb_dbase']);
194 $update_couch = $this->couch_obj->update_doc($array_update);
196 $thumb_name = $this->get_thumb_name($file_name);
197 return $thumb_name;
201 * Return file name for thumbnail (adding 'th_')
202 * @param $file_name
204 private function get_thumb_name($file_name) {
205 return 'th_' . $file_name;
210 * save error in error log
211 * @param $url
213 private function error_log($url){
215 error_log('Failed to create thumbnail of ' . $url);