PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/__include/sesivacka.php

https://gitlab.com/error414/fileshit
PHP | 444 lines | 185 code | 131 blank | 128 comment | 24 complexity | 493bf7289bf42cb8700ed5befdc2c180 MD5 | raw file
  1. <?php
  2. /**
  3. * zmensovani obrazku a jine upravy (zadne velke)
  4. *
  5. * Copyright (c) 2006, Petr Cada <error414@error414.com>
  6. * Web: http://www.error414.com/
  7. *
  8. */
  9. /**
  10. * sesivacka - znemseni obrazku png, gif, jpg pokud jsou podporovanz v gd2 knihovne
  11. *
  12. * <code>
  13. * $ses = new sesivacka();
  14. * $ses->tmpDir = 'tmp/';
  15. * $ses->doIt('img/obloha.jpg');
  16. * </code>
  17. */
  18. class sesivacka
  19. {
  20. /**
  21. * adresar pro ukladani vzgenerovanych obrazku
  22. * @var string
  23. */
  24. public $tmpDir = 'tmp/';
  25. /**
  26. * obrazek ketry znensujeme, musi
  27. * byt pouzita cela cesta
  28. *
  29. * adr/jinyAdr/jemeno.png
  30. *
  31. *@var string
  32. */
  33. public $img;
  34. /**
  35. * seznam podporovanych formatu obrazku
  36. *
  37. *@var array
  38. */
  39. private $supportImgType = array(
  40. 'image/gif',
  41. 'image/jpeg',
  42. 'image/png');
  43. /**
  44. * informace o obrazku
  45. * stejene pole jako vraci getimagesize()
  46. *
  47. *@var array
  48. */
  49. public $imageInfo = array();
  50. /**
  51. * pozadovana sirka zmenseneho obrazku
  52. *
  53. *@var int
  54. */
  55. public $newImageWidth = 200;
  56. /**
  57. * vyska zmenseneho obrazku
  58. *
  59. *@var int
  60. */
  61. private $newImageHeigh;
  62. /**
  63. * jmeno zmenseneho obrazku
  64. *
  65. *@var int
  66. */
  67. private $tmpNamePatch;
  68. /**
  69. * efekty obrayku
  70. * cerna - bila; defaultne vypnuto
  71. *
  72. *@var bool
  73. */
  74. public $imageEfektGreyScale = false;
  75. /**
  76. * povoleni vytvoreni zvetseneho obrazku
  77. *
  78. *@var bool
  79. */
  80. public $enableResizeOverSize = false;
  81. public function __construct()
  82. {
  83. if (version_compare(PHP_VERSION , '5.0.3', '<')){
  84. die('sesivacka potrebuje PHP ve verzi 5.0.3 nebo novejsi');
  85. }
  86. $this->checkModule();
  87. }
  88. /**
  89. * kontrola existence rizsireni pro praci s obrazky
  90. * neuspech ukoncuje script
  91. */
  92. private function checkModule(){
  93. if(!extension_loaded('gd')){
  94. die('Neni nacteno rozsireni pro praci s obrazky GD');
  95. }
  96. }
  97. /**
  98. * zapracovani obrazku
  99. * neuspech ukoncuje script
  100. */
  101. public function doIt($img = ''){
  102. if(empty($img)){
  103. die($this->error('musite zadat obrazek ke zpracovani'));
  104. }
  105. $imageType = $this->isImg($img);
  106. switch($imageType)
  107. {
  108. case $this->supportImgType[0]:
  109. return $this->resizeGif($img);
  110. break;
  111. case $this->supportImgType[1]:
  112. return $this->resizeJPG($img);
  113. break;
  114. case $this->supportImgType[2]:
  115. return $this->resizePNG($img);
  116. break;
  117. default:
  118. die($this->error('Tento typ obrazku neni podporovan -- chyba 10'));
  119. }
  120. }
  121. /**
  122. * kontrola existence obrazku a zpravneho mime
  123. * vraci mime typ obrazku
  124. *
  125. * return string
  126. */
  127. private function isImg($img){
  128. if(!@file_exists($img)){
  129. die($this->error('Nebyl nalezen soubor s obrazkem'));
  130. }
  131. $this->imageInfo = getimagesize($img);
  132. if(!in_array($this->imageInfo['mime'], $this->supportImgType)){
  133. die($this->error('Tento typ obrazku neni podporovan'));
  134. }
  135. return $this->imageInfo['mime'];
  136. }
  137. /**
  138. * zmenseni obrazku PNG
  139. * vraci adresu vygenerovaneho obrazku
  140. *
  141. * return string
  142. */
  143. private function resizePNG($img){
  144. if(!$this->isSuport(IMG_PNG)){
  145. die('Knihovna GD nepodporuje obrazky type PNG');
  146. }
  147. if($this->resizenExist($img)){
  148. return $this->resizenExist($img);
  149. }else{
  150. $fp = imagecreatefrompng($img);
  151. $fx = imagecreatetruecolor ($this->newImageWidth,$this->newImageHeight);
  152. imagecopyresized ($fx,$fp,0,0,0,0,$this->newImageWidth, $this->newImageHeight, $this->imageInfo[0],$this->imageInfo[1]);
  153. if($this->imageEfektGreyScale)
  154. $this->greyScale($fx);
  155. imagePNG ($fx, $this->tmpDir . $this->tmpNamePatch);
  156. imagedestroy($fx);
  157. imagedestroy($fp);
  158. return $this->tmpDir . $this->tmpNamePatch;
  159. }
  160. }
  161. /**
  162. * zmenseni obrazku JPG
  163. * vraci adresu vygenerovaneho obrazku
  164. *
  165. * return string
  166. */
  167. private function resizeJPG($img){
  168. if(!$this->isSuport(IMG_JPEG)){
  169. die($this->error('Knihovna GD nepodporuje obrazky type JPG'));
  170. }
  171. if($this->resizenExist($img)){
  172. return $this->resizenExist($img);
  173. }else{
  174. $fp = imagecreatefromjpeg($img);
  175. $fx = imagecreatetruecolor ($this->newImageWidth,$this->newImageHeight);
  176. imagecopyresized ($fx,$fp,0,0,0,0,$this->newImageWidth, $this->newImageHeight, $this->imageInfo[0],$this->imageInfo[1]);
  177. if($this->imageEfektGreyScale)
  178. $this->greyScale($fx);
  179. imagejpeg ($fx, $this->tmpDir . $this->tmpNamePatch);
  180. imagedestroy($fx);
  181. imagedestroy($fp);
  182. return $this->tmpDir . $this->tmpNamePatch;
  183. }
  184. }
  185. /**
  186. * zmenseni obrazku GIF
  187. * vraci adresu vygenerovaneho obrazku
  188. *
  189. * return string
  190. */
  191. private function resizeGIF($img){
  192. if(!$this->isSuport(IMG_GIF)){
  193. die('Knihovna GD nepodporuje obrazky type JPG');
  194. }
  195. if($this->resizenExist($img)){
  196. return $this->resizenExist($img);
  197. }else{
  198. $fp = imagecreatefromgif($img);
  199. $fx = imagecreatetruecolor ($this->newImageWidth,$this->newImageHeight);
  200. imagecopyresampled ($fx,$fp,0,0,0,0,$this->newImageWidth, $this->newImageHeight, $this->imageInfo[0],$this->imageInfo[1]);
  201. if($this->imageEfektGreyScale)
  202. $this->greyScale($fx);
  203. imagegif ($fx, $this->tmpDir . $this->tmpNamePatch);
  204. imagedestroy($fx);
  205. imagedestroy($fp);
  206. return $this->tmpDir . $this->tmpNamePatch;
  207. }
  208. }
  209. /**
  210. * zjisteni jestli je typ obrazku podporovan
  211. *
  212. * return bools
  213. */
  214. public function isSuport($type = '')
  215. {
  216. if(imagetypes() & $type){
  217. return true;
  218. }else{
  219. return false;
  220. }
  221. }
  222. /**
  223. * rozparsovani adresy obrazku
  224. * varci se pole ve stejnem formatu jako funkce pathinfo()
  225. *
  226. * return array
  227. */
  228. public function parseImgPatch($img)
  229. {
  230. $path_parts = pathinfo($img);
  231. return $path_parts;
  232. }
  233. /**
  234. * vypocet vysky obrazku
  235. *
  236. * return int
  237. */
  238. public function newImgSize()
  239. {
  240. if($this->imageInfo[1] === 0){
  241. die($this->error('chyba pri vypoctu velikosti obrazku'));
  242. }
  243. $this->newImageHeight = round($this->newImageWidth / ($this->imageInfo[0]/$this->imageInfo[1]));
  244. return $this->newImageHeight;
  245. }
  246. /**
  247. * zjisteni jestli zmenseny obrazek uz neexistuje
  248. *
  249. * return mixed
  250. */
  251. private function resizenExist($img)
  252. {
  253. $patchInfoImg = $this->parseImgPatch($img);
  254. $this->newImgSize();
  255. if($this->newImageWidth > $this->imageInfo[0] and $this->enableResizeOverSize == 0){;
  256. return $img;
  257. }elseif($this->newImageWidth == $this->imageInfo[0] and $this->imageEfektGreyScale == 0){
  258. return $img;
  259. }else{
  260. $this->tmpNamePatch = '~' . $patchInfoImg['basename']
  261. . $this->newImageWidth . 'X' . $this->newImageHeight
  262. . '[' . $this->imageEfektGreyScale . ',0,0,0,0,0,0]' . '.' . $patchInfoImg['extension'];
  263. if(@file_exists($this->tmpDir . $this->tmpNamePatch)){
  264. return $this->tmpDir . $this->tmpNamePatch;
  265. }else{
  266. return false;
  267. }
  268. }
  269. }
  270. /**
  271. * zmeni paletu obrazku na greyscale
  272. *
  273. * return void
  274. */
  275. private function greyScale($fx, $dither=1) {
  276. if (!($t = imagecolorstotal($fx))) {
  277. $t = 256;
  278. imagetruecolortopalette($fx, $dither, $t);
  279. }
  280. for ($c = 0; $c < $t; $c++) {
  281. $col = imagecolorsforindex($fx, $c);
  282. $min = min($col['red'],$col['green'],$col['blue']);
  283. $max = max($col['red'],$col['green'],$col['blue']);
  284. $i = ($max+$min)/2;
  285. imagecolorset($fx, $c, $i, $i, $i);
  286. }
  287. }
  288. /**
  289. * vytvoreni chyboveho hlaseni do obrazku
  290. *
  291. * return void
  292. */
  293. private function error($text) {
  294. if(!$this->isSuport(IMG_JPG)){
  295. die('Knihovna GD nepodporuje obrazky type PNG');
  296. }
  297. $im = imagecreatetruecolor(400, 100);
  298. $bg = imagecolorallocate($im, 255, 255, 255);
  299. $textcolor = imagecolorallocate($im, 225, 225, 225);
  300. imagestring($im, 5, 10, 45, $text, $textcolor);
  301. imagejpeg($im, $this->tmpDir . 'error.jpeg');
  302. die ('<img src="' . $this->tmpDir . 'error.jpeg" >');
  303. }
  304. public function initExtension()
  305. {
  306. $extension['picture'] = array('gif', 'png', 'jpg');
  307. $extension['multi'] = array('hmtl','htm', 'php', 'php3', 'hta');
  308. $extension['js'] = array('js');
  309. $extension['css'] = array('css');
  310. $extension['text'] = array('txt');
  311. }
  312. }
  313. ?>