Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /**
  3:  * WsImage
  4:  * Provides simple interface for working with images using GD library.
  5:  *
  6:  * Example usage:
  7:  *
  8:  * <code>
  9:  * $img = new WsImage()
 10:  *
 11:  * // load image from file
 12:  * $img->read('public/img/webiness_white.png');
 13:  *
 14:  * // add wattermark to image
 15:  * $img->addWatermark('copyright (c) me@myaddress.com');
 16:  *
 17:  * // show image thumbnail
 18:  * $img->showThumbnail();
 19:  *
 20:  * // show image
 21:  * $img->show();
 22:  * </code>
 23:  *
 24:  */
 25: class WsImage
 26: {
 27:     /**
 28:      * @var bool $_has_gd_exstension Is GD exstension exist in PHP
 29:      *
 30:      */
 31:     private $_has_gd_extension;
 32: 
 33:     /**
 34:      * @var resource $_image Image
 35:      *
 36:      */
 37:     private $_image;
 38: 
 39:     /**
 40:      * @var string $_imagefile Current image file with full path
 41:      *
 42:      */
 43:     private $_imagefile;
 44: 
 45: 
 46:     public function __construct()
 47:     {
 48:         // check for GD extension
 49:         if (!extension_loaded('gd')) {
 50:             if (!dl('gd.so')) {
 51:                 $this->_has_gd_extension = false;
 52:                 return;
 53:             }
 54:         }
 55: 
 56:         $this->_has_gd_extension = true;
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Read image from file.
 62:      *
 63:      * @param string $file Image file name with path relative to application
 64:      * root directory
 65:      * @return booleane suceess or fail
 66:      *
 67:      */
 68:     public function read($file)
 69:     {
 70:         if (!$this->_has_gd_extension) {
 71:             return false;
 72:         }
 73: 
 74:         // open image file
 75:         $this->_imagefile = WsROOT.'/'.$file;
 76:         $handle = @fopen($this->_imagefile, 'rb');
 77:         if ($handle === false) {
 78:             return false;
 79:         }
 80: 
 81:         // read image
 82:         $data = '';
 83:         while (!feof($handle)) {
 84:             $data .= fread($handle, 4192);
 85:         }
 86:         fclose($handle);
 87: 
 88:         // create image object
 89:         $this->_image = imagecreatefromstring($data);
 90: 
 91:         // free memory
 92:         unset($data, $handle);
 93: 
 94:         if ($this->_image === false) {
 95:             return false;
 96:         } else {
 97:             return true;
 98:         }
 99:     }
100: 
101: 
102:     /**
103:      * Show image in browser
104:      *
105:      */
106:     public function show()
107:     {
108:         if (!$this->_has_gd_extension or $this->_image === false) {
109:             return false;
110:         }
111: 
112:         // temporary image file
113:         $uid = uniqid('wsimg_');
114:         $temp_file = WsROOT.'/runtime/'.$uid.basename($this->_imagefile);
115:         $temp_url = WsSERVER_ROOT.'/runtime/'.$uid.basename($this->_imagefile);
116: 
117:         imagepng($this->_image, $temp_file);
118: 
119:         // show image in browser
120:         echo '<img src="'.$temp_url.'"/>';
121: 
122:         // free memory
123:         unset($temp_url, $temp_file, $uid);
124:     }
125: 
126: 
127:     /**
128:      * Show image thumbnail
129:      *
130:      * @param integer $width Thumbnail width
131:      * @param integer $height Thumbnail height
132:      * @param string $text Thumbnail text
133:      *
134:      */
135:     public function showThumbnail($width = 100, $height = 100, $text = '')
136:     {
137:         if (!$this->_has_gd_extension or $this->_image === false) {
138:             return false;
139:         }
140: 
141:         list($orig_width, $orig_height) = getimagesize($this->_imagefile);
142:         $thumb_image = imagecreatetruecolor($width, $height);
143: 
144:         imagecopyresampled($thumb_image, $this->_image, 0, 0, 0, 0,
145:             $width, $height, $orig_width, $orig_height
146:         );
147: 
148:         ob_start();
149:         imagepng($thumb_image);
150:         $i =  ob_get_clean();
151:         imagedestroy($thumb_image);
152: 
153:         // temporary image file
154:         $uid = uniqid('wsimg_');
155:         $temp_file = WsROOT.'/runtime/'.$uid.basename($this->_imagefile);
156:         $temp_url = WsSERVER_ROOT.'/runtime/'.$uid.basename($this->_imagefile);
157: 
158:         imagepng($this->_image, $temp_file);
159: 
160:         // id of popup window
161:         $img_id = uniqid('thumb_'.round(rand()));
162:         // show thumbnail
163:         //echo '<div id="'.$img_id.'" style="display: inline-block;">';
164:         echo '<img id="'.$img_id.'" class="img_thumb" src="data:image/png;base64,'
165:             .base64_encode($i).'"/>';
166:         if (trim($text) != '') {
167:             echo '<p style="align: center;">'.$text.'</p>';
168:         }
169:         //echo '</div>';
170: 
171:         echo '<script type="text/javascript">'.PHP_EOL;
172:         echo 'var '.$img_id.'=document.getElementById("'.$img_id.'");'.PHP_EOL;
173:         echo $img_id.'.onclick = function() {'.PHP_EOL;
174:         echo 'window.ws_thumb_show = WsViewThumbnail("'
175:             .$temp_url.'");'.PHP_EOL;
176:         echo '}'.PHP_EOL;
177:         echo '</script>'.PHP_EOL;
178: 
179:         // free memory
180:         unset($i, $thumb_image,
181:             $orig_height, $orig_width, $temp_url, $temp_file
182:         );
183:     }
184: 
185: 
186:     /**
187:      * Add watermark text to image
188:      *
189:      * @param string $text Watermark text
190:      * @param integer $x Verical offset for text
191:      * @param integer $y Horizontal offset for text
192:      * @param string $font Full path, relative to server root, of font file
193:      * @param integer $size Font size
194:      *
195:      */
196:     public function addWatermark(
197:         $text, $x = 8, $y = 8 , $font = null, $size = 11
198:     )
199:     {
200:         if (!$this->_has_gd_extension or $this->_image === false) {
201:             return false;
202:         }
203: 
204:         // no empty string
205:         if (trim($text) === '') {
206:             return false;
207:         }
208: 
209:         if ($font == null) {
210:             $font = WsROOT.'/public/fonts/FreeSans.ttf';
211:         } else {
212:             $font = WsROOT.'/'.$font;
213:         }
214: 
215:         # calculate maximum height of a character
216:         $bbox = imagettfbbox($size, 0, $font, 'ky');
217:         $y -= $bbox[5];
218: 
219:         $black = imagecolorallocate($this->_image, 0, 0, 0);
220:         $white = imagecolorallocate($this->_image, 255, 255, 255);
221:         imagettftext($this->_image, $size, 0, $x+1, $y+1, $black, $font, $text);
222:         imagettftext($this->_image, $size, 0, $x, $y+1, $black, $font, $text);
223:         imagettftext($this->_image, $size, 0, $x, $y, $white, $font, $text);
224: 
225:         unset($white, $black, $bbox);
226:     }
227: 
228: 
229:     /**
230:      * Get image width
231:      *
232:      * @return integer $width Image width
233:      *
234:      */
235:     public function getWidth()
236:     {
237:         if (!$this->_has_gd_extension or $this->_image === false) {
238:             return false;
239:         }
240: 
241:         $width = imagesx($this->_image);
242: 
243:         return $width;
244:     }
245: 
246: 
247:     /**
248:      * Get image height
249:      *
250:      * @return integer $height Image height
251:      *
252:      */
253:     public function getHeight()
254:     {
255:         if (!$this->_has_gd_extension or $this->_image === false) {
256:             return false;
257:         }
258: 
259:         $height = imagesy($this->_image);
260: 
261:         return $height;
262:     }
263: }
264: 
API documentation generated by ApiGen