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>;.
17 * @author Amiel Elboim <amielel@matrix.co.il>
18 * @link http://www.open-emr.org
23 * Class ThumbnailGenerator
25 class ThumbnailGenerator
{
27 public static $types_support = array('image/png', 'image/jpeg', 'image/jpg', 'image/gif');
28 private $thumb_obj = null;
29 private $couch_obj = null;
32 * ThumbnailGenerator constructor.
34 public function __construct()
36 $thumb_size = ($GLOBALS['thumb_doc_max_size'] > 0) ?
$GLOBALS['thumb_doc_max_size'] : null;
37 $this->thumb_obj
= new Thumbnail($thumb_size);
43 public static function get_types_support()
45 foreach(self
::$types_support as $value){
46 $types_support[] = "'$value'";
48 return $types_support;
51 static function count_not_generated()
53 $sql = "SELECT COUNT(*) AS c FROM documents
54 WHERE mimetype IN (" . implode(',', self
::get_types_support()) . ") AND thumb_url IS NULL";
56 $results = sqlStatement($sql);
57 $row = sqlFetchArray($results);
63 * Generating all files with match format that still isn't generated
64 * @return array $result (count failed/success)
66 public function generate_all()
69 $feedback = array('sum_success' => 0, 'sum_failed' => 0, 'success' => array(), 'failed' => array());
71 $sql = "SELECT id, url, couch_docid, storagemethod, path_depth FROM documents
72 WHERE mimetype IN (" . implode(',', self
::get_types_support()) . ") AND thumb_url IS NULL";
74 $results = sqlStatement($sql);
75 while($row = sqlFetchArray($results)) {
77 switch((int)$row['storagemethod']) {
80 $new_file = $this->generate_HD_file($row['url'], $row['path_depth']);
84 $new_file = $this->generate_couch_file($row['couch_docid'], $row['url']);
87 $this->error_log($row['url']);continue;
89 // Write error to log if failed
91 $this->error_log($row['url']);
92 $feedback['sum_failed'] ++
;
93 $feedback['failed'][] = $row['url'];
97 $sql = "UPDATE documents SET thumb_url = ? WHERE id = ?";
98 $update = sqlStatement($sql, array($new_file, $row['id']));
100 $feedback['sum_success'] ++
;
101 $feedback['success'][] = $row['url'];
109 * Generate new file and store it in hard disk
111 * @return bool|string
113 private function generate_HD_file($url, $path_depth)
116 $url = preg_replace("|^(.*)://|","",$url);
118 //change full path to current webroot. this is for documents that may have
119 //been moved from a different filesystem and the full path in the database
120 //is not current. this is also for documents that may of been moved to
121 //different patients. Note that the path_depth is used to see how far down
122 //the path to go. For example, originally the path_depth was always 1, which
123 //only allowed things like documents/1/<file>, but now can have more structured
124 //directories. For example a path_depth of 2 can give documents/encounters/1/<file>
126 // NOTE that $from_filename and basename($url) are the same thing
127 $from_all = explode("/",$url);
128 $from_filename = array_pop($from_all);
129 $from_pathname_array = array();
130 for ($i=0;$i<$path_depth;$i++
) {
131 $from_pathname_array[] = array_pop($from_all);
133 $from_pathname_array = array_reverse($from_pathname_array);
134 $from_pathname = implode("/",$from_pathname_array);
136 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
138 if (file_exists($temp_url)) {
144 $path_parts = pathinfo($url);
146 $resource = $this->thumb_obj
->create_thumbnail($url);
151 $thumb_name = $this->get_thumb_name($path_parts['basename']);
152 $full_path = $path_parts['dirname'] . '/' . $thumb_name;
153 $new_thumb_file = $this->thumb_obj
->image_to_file($resource, $full_path);
155 return ($new_thumb_file) ?
'file://' . $full_path : false;
160 * Generate new file and store it in CouchDB
162 * @return bool|string
164 private function generate_couch_file($doc_id, $file_name)
166 if( is_null($this->couch_obj
)) {
167 $this->couch_obj
= new CouchDB();
169 $data = array($GLOBALS['couchdb_dbase'],$doc_id);
170 $resp = $this->couch_obj
->retrieve_doc($data);
172 if(empty($resp->data
)) {
176 $resource = $this->thumb_obj
->create_thumbnail(null,base64_decode($resp->data
));
181 $new_file_content = $this->thumb_obj
->get_string_file($resource);
183 if(!$new_file_content) {
186 $couch_row = get_object_vars($resp);
188 $couch_row['th_data'] = json_encode(base64_encode($new_file_content));
189 $array_update = array_values($couch_row);
190 array_unshift($array_update,$GLOBALS['couchdb_dbase']);
191 $update_couch = $this->couch_obj
->update_doc($array_update);
193 $thumb_name = $this->get_thumb_name($file_name);
198 * Return file name for thumbnail (adding 'th_')
201 private function get_thumb_name($file_name) {
202 return 'th_' . $file_name;
207 * save error in error log
210 private function error_log($url){
212 error_log('Failed to create thumbnail of ' . $url);