/yashr/drivers/img.php

https://bitbucket.org/inginer/yashr1_1 · PHP · 272 lines · 202 code · 51 blank · 19 comment · 18 complexity · 0a282edacf76c6b414ad7f80dd5aca05 MD5 · raw file

  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: inginer
  5. * Date: 6/1/13
  6. * Time: 2:58 AM
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. namespace Yashr\Drivers;
  10. use Yashr\Classes\Core;
  11. use Yashr\Classes\Driver;
  12. use Yashr\Classes\YashrException;
  13. Core::import('yashr.drivers.db.mysqli');
  14. Core::import('yashr.drivers.session.session');
  15. class Img extends Driver
  16. {
  17. private $id = null;
  18. private $src = null;
  19. private $type = null;
  20. /*
  21. private $format = array(
  22. 'full',
  23. 'medium',
  24. 'min',
  25. );
  26. */
  27. private $dist = null;
  28. public function exec(array $params = array())
  29. {
  30. }
  31. public function get ( $id, $params = array())
  32. {
  33. $def = array(
  34. 'rotate' => 0,
  35. 'format' => 'full',
  36. 'size' => array(),
  37. );
  38. $params = array_merge($def,$params);
  39. switch ($params['format'])
  40. {
  41. case 'medium':
  42. {
  43. $params['size']['width'] = 300;
  44. $params['size']['height'] = 300;
  45. break;
  46. }
  47. case 'min':
  48. {
  49. $params['size']['width'] = 100;
  50. $params['size']['height'] = 100;
  51. break;
  52. }
  53. }
  54. $this->id = $id;
  55. $info = $this->info(array(
  56. 'id' => $id,
  57. ));
  58. if (!$info['public'])
  59. {
  60. if (!$this->session->id)
  61. {
  62. exit;
  63. }
  64. }
  65. $this->src = $_SERVER['DOCUMENT_ROOT'].$info['path'].'/'.$info['title'];
  66. if (!is_file($this->src))
  67. {
  68. return '/img/no-photo.jpg';
  69. //throw new YashrException('File with name ' . $this->src . ' not found');
  70. }
  71. $this->type = substr($info['title'],-3,3);
  72. $this->create();
  73. if ($params['size'])
  74. {
  75. $this->dist = $this->resize($params['size']);
  76. }
  77. if ($params['rotate'])
  78. {
  79. $this->rotate($params['rotate']);
  80. }
  81. $this->img();
  82. }
  83. public function get2 ($id, $params = array())
  84. {
  85. $def = array(
  86. 'rotate' => 0,
  87. 'format' => 'full',
  88. 'size' => array(),
  89. 'data' => array(),
  90. );
  91. $params = array_merge($def,$params);
  92. switch ($params['format'])
  93. {
  94. case 'medium':
  95. {
  96. $params['size']['width'] = 300;
  97. $params['size']['height'] = 300;
  98. break;
  99. }
  100. case 'min':
  101. {
  102. $params['size']['width'] = 100;
  103. $params['size']['height'] = 100;
  104. break;
  105. }
  106. }
  107. $this->id = $id;
  108. $info = $params['data'];
  109. if (!$info['public'])
  110. {
  111. if (!$this->session->id)
  112. {
  113. exit;
  114. }
  115. }
  116. $this->src = $_SERVER['DOCUMENT_ROOT'].$info['path'].'/'.$info['title'];
  117. $file_name=$info['path'].'/'.$params['format'].'_'.$info['title'];
  118. $save_file=$_SERVER['DOCUMENT_ROOT'].$file_name;
  119. if (!file_exists($save_file))
  120. {
  121. if (!is_file($this->src))
  122. {
  123. return '/up/'.$params['format'].'-no-photo.jpg';//$this->src;
  124. }
  125. $this->type = substr($info['title'],-3,3);
  126. $this->create();
  127. if ($params['size'])
  128. {
  129. $this->dist = $this->resize($params['size'],$id);
  130. }
  131. if ($params['rotate'])
  132. {
  133. $this->rotate($params['rotate']);
  134. }
  135. $this->img($save_file);
  136. }
  137. return 'http://'.$info['host'].$file_name;
  138. }
  139. private function info (array $params = array())
  140. {
  141. $def = array(
  142. 'id' => 0,
  143. );
  144. $params = array_merge($def,$params);
  145. $where = '';
  146. if ($params['id'])
  147. {
  148. $where .= ' AND id='.intval($params['id']);
  149. }
  150. return $this->mysqli->fetch("SELECT * FROM file WHERE 1=1 " . $where);
  151. }
  152. private function img ( $save=NULL )
  153. {
  154. header('Content-type: image/'.$this->type);
  155. switch ($this->type)
  156. {
  157. case "jpg":
  158. {
  159. imagejpeg($this->dist,$save,100);
  160. break;
  161. }
  162. case "png":
  163. {
  164. imagepng($this->dist,$save);
  165. break;
  166. }
  167. }
  168. imagedestroy($this->dist);
  169. }
  170. private function create ()
  171. {
  172. switch ($this->type)
  173. {
  174. case "jpg":
  175. {
  176. $this->dist = imagecreatefromjpeg($this->src);
  177. break;
  178. }
  179. case "png":
  180. {
  181. $this->dist = imagecreatefrompng($this->src);
  182. break;
  183. }
  184. }
  185. }
  186. private function rotate ( $degrees=90 )
  187. {
  188. $this->dist = imagerotate($this->dist, $degrees, 0);
  189. }
  190. private function resize (array $params = array())
  191. {
  192. $def = array(
  193. 'width' => 0,
  194. 'height' => 0,
  195. );
  196. $params = array_merge($def,$params);
  197. // задание максимальной ширины и высоты
  198. // тип содержимого
  199. header('Content-Type: image/'.$this->type);
  200. // получение новых размеров
  201. list($width_orig, $height_orig) = getimagesize($this->src);
  202. $ratio_orig = $width_orig/$height_orig;
  203. if ($params['width']/$params['height'] > $ratio_orig)
  204. {
  205. $params['width'] = $params['height']*$ratio_orig;
  206. }
  207. else
  208. {
  209. $params['height'] = $params['width']/$ratio_orig;
  210. }
  211. // ресэмплирование
  212. $tmp_photo = imagecreatetruecolor($params['width'], $params['height']);
  213. if ($this->type=='png')
  214. {
  215. imagealphablending($tmp_photo, false);
  216. imagesavealpha($tmp_photo, true);
  217. }
  218. imagecopyresampled($tmp_photo, $this->dist, 0, 0, 0, 0, $params['width'], $params['height'], $width_orig, $height_orig);
  219. return $tmp_photo;
  220. }
  221. }