XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
image.php
Go to the documentation of this file.
1 <?php
2 // $Id: image.php 1020 2012-09-04 16:15:09Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
15 class RMImage extends RMObject
16 {
21  private $selected_size = 0;
26  private $sizes = array();
27 
28  public function __construct($id=null){
29  $this->db = XoopsDatabaseFactory::getDatabaseConnection();
30  $this->_dbtable = $this->db->prefix("rmc_images");
31  $this->setNew();
32  $this->initVarsFromTable();
33  if ($id==null){
34  return;
35  }
36 
37  if ($this->loadValues($id)){
38  $this->unsetNew();
39  }
40  }
41 
42  public function id(){
43  return $this->getVar('id_img');
44  }
45 
51  public function load_from_params($params){
52 
53  if($params=='') return false;
54  $p = explode(":", $params);
55 
56  if(intval($p[0])<=0) return false;
57 
58  if($this->loadValues(intval($p[0]))) $this->unsetNew();
59  $this->selected_size = intval($p[1]);
60 
61  $p[2] = $p[2]!='' ? urldecode($p[2]) : '';
62  $p[3] = $p[3]!='' ? urldecode($p[3]) : '';
63 
64  return $p[2];
65 
66  }
67 
72  public function get_sizes_data(){
73  if(empty($this->sizes)){
74  $cat = new RMImageCategory($this->getVar('cat'));
75  if($cat->isNew()) return false;
76  // Get sizes
77  $this->sizes = $cat->getVar('sizes');
78  }
79 
80  return $this->sizes;
81  }
82 
88  public function url($size=-1){
89 
90  if($size<0 && $this->selected_size>0)
91  $size = $this->selected_size;
92 
93  if($this->isNew()) return false;
94 
95  $this->get_sizes_data();
96 
97  $url = XOOPS_UPLOAD_URL.'/'.date('Y', $this->getVar('date')).'/'.date('m',$this->getVar('date')).'/';
98  if($size>=count($this->sizes)){
99  $url .= $this->getVar('file');
100  return $url;
101  }
102 
103  $info = pathinfo($this->getVar('file'));
104 
105  $url .= 'sizes/'.$info['filename'].'_'.$this->sizes[$size]['width'].'x'.(isset($this->sizes[$size]['height'])?$this->sizes[$size]['height']:'');
106  $url .= '.'.$info['extension'];
107  return $url;
108 
109  }
110 
111  public function get_smallest(){
112 
113  if($this->isNew()) return false;
114 
115  $this->get_sizes_data();
116  $ps = 0; // Previous size
117  $small = 0;
118 
119  foreach($this->sizes as $k => $size){
120  $ps = $ps==0?$size['width']:$ps;
121  if($size['width']<$ps){
122  $ps = $size['width'];
123  $small = $k;
124  }
125 
126  }
127 
128  return $this->url($small);
129 
130  }
131 
136  public function get_all_versions(){
137 
138  if($this->isNew()) return false;
139 
140  $this->get_sizes_data();
141  $ret = array();
142  foreach($this->sizes as $k => $size){
143  $ret[$size['name']] = $this->url($k);
144  }
145 
146  return $ret;
147  }
148 
149  public function get_version($name){
150 
151  if($this->isNew()) return false;
152 
153  $this->get_sizes_data();
154  $ret = array();
155  foreach($this->sizes as $k => $size){
156  if($size['name'] == $name)
157  return $this->url($k);
158  }
159 
160  return '';
161 
162  }
163 
164  public function getOriginal(){
165  $url = XOOPS_UPLOAD_URL.'/'.date('Y', $this->getVar('date')).'/'.date('m',$this->getVar('date')).'/';
166  $url .= $this->getVar('file');
167  return $url;
168  }
169 
170  public function save(){
171 
172  if ($this->isNew()){
173  return $this->saveToTable();
174  } else {
175  return $this->updateTable();
176  }
177 
178  }
179 
180  public function delete(){
181  $path = XOOPS_UPLOAD_PATH.'/'.date('Y', $this->getVar('date')).'/'.date('m',$this->getVar('date')).'/';
182  $sizes = $this->get_sizes_data();
183 
184  $info = pathinfo($this->getVar('file'));
185  foreach($sizes as $size){
186  unlink($path.'sizes/'.$info['filename'].'_'.$this->sizes[$size]['width'].'x'.(isset($this->sizes[$size]['height'])?$this->sizes[$size]['height']:'').'.'.$info['extension']);
187  }
188  unlink($path.$this->getVar('file'));
189 
190  return $this->deleteFromTable();
191  }
192 
193 }