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

/engine/functions/subfunctions/image_functions.php

https://github.com/Pankrashingo/cms-landing
PHP | 905 lines | 489 code | 126 blank | 290 comment | 92 complexity | 0cc9f1de8db471b01a6595479bfd3e0a MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. global $coners_alpha_coords;
  3. $coners_alpha_coords=array();
  4. /**
  5. * Resize image
  6. *
  7. * @param Image object $image
  8. * @param int $new_x.New width
  9. * @param int $new_y. New height
  10. * @param bool $cropping. Cropping flag
  11. * @param bool $round_edges. Need to round edges
  12. * @return Image object
  13. */
  14. function &image_resize($image,$new_x,$new_y,$cropping=false,$round_edges=true,$type='jpg')
  15. {
  16. $fff=$new_y;
  17. $new_image=false;
  18. $x_size=imagesx($image);
  19. $y_size=imagesy($image);
  20. $koeff_x=$x_size/$new_x;
  21. $koeff_y=$y_size/$new_y;
  22. $koeff_min=min($koeff_x,$koeff_y);
  23. $koeff_max=max($koeff_x,$koeff_y);
  24. if(!$cropping && $koeff_max<=1) {
  25. if($round_edges)image_effect_round_edges($image,$type);
  26. return $image;
  27. }
  28. if(!$cropping)
  29. {
  30. $otstup_x=0;
  31. $otstup_y=0;
  32. }
  33. else
  34. {
  35. $otstup_x=($koeff_x>$koeff_y)?round(((($x_size/$koeff_min)-$new_x)*$koeff_min)/2):0;
  36. if($otstup_x<0)$otstup_x=0;
  37. $otstup_y=($koeff_x>$koeff_y)?0:round(((($y_size/$koeff_min)-$new_y)*$koeff_min)/2);
  38. if($otstup_y<0)$otstup_y=0;
  39. }
  40. if(!$cropping)
  41. {
  42. //вычисляем новый размер учитывая пропорции оригинальной картинки
  43. $new_x=ceil($x_size/$koeff_max);
  44. $new_y=ceil($y_size/$koeff_max);
  45. $new_image=imagecreatetruecolor($new_x,$new_y);
  46. if(!imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_x, $new_y, $x_size, $y_size))
  47. {
  48. trigger_error('Не удалось сделать ресайз изображения.',E_USER_ERROR);
  49. $new_image=false;
  50. }
  51. }
  52. else
  53. {
  54. $new_image=imagecreatetruecolor($new_x,$new_y);
  55. if($koeff_min>1)
  56. {
  57. if(!imagecopyresampled($new_image, $image, 0, 0, $otstup_x, $otstup_y, $new_x, $new_y, $x_size-$otstup_x*2, $y_size-$otstup_y*2))
  58. {
  59. trigger_error('Не удалось сделать ресайз изображения.',E_USER_WARNING);
  60. $new_image=false;
  61. }
  62. }
  63. else
  64. {
  65. if(!imagecopyresampled($new_image,$image,0,0,$otstup_x,$otstup_y,$new_x,$new_y,$x_size-$otstup_x*2,$y_size-$otstup_y*2))
  66. {
  67. trigger_error('Не удалось сделать CROP изображения.',E_USER_WARNING);
  68. $new_image=false;
  69. }
  70. }
  71. }
  72. if($round_edges)image_effect_round_edges($new_image,$type);
  73. return $new_image;
  74. }
  75. /**
  76. * Round corner for image
  77. *
  78. * @param GD obejct $image
  79. */
  80. function image_effect_round_edges(&$image,$type)
  81. {
  82. global $coners_alpha_coords;
  83. $width=imagesx($image);
  84. $height=imagesy($image);
  85. if($type=='png'){
  86. imageAlphaBlending($image, false);
  87. imageSaveAlpha($image, true);
  88. }
  89. if(count($coners_alpha_coords)==0){
  90. $corner_tl=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_top_left.png');
  91. $corner_tr=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_top_right.png');
  92. $corner_bl=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_bottom_left.png');
  93. $corner_br=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_bottom_right.png');
  94. $coners_alpha_coords['t1']['image']=&$corner_tl;
  95. $coners_alpha_coords['t1']['width']=imagesx($corner_tl);
  96. $coners_alpha_coords['t1']['height']=imagesy($corner_tl);
  97. $coners_alpha_coords['t1']['start_width']=0;
  98. $coners_alpha_coords['t1']['start_height']=0;
  99. $coners_alpha_coords['t1']['alpha']=array();
  100. $coners_alpha_coords['tr']['image']=&$corner_tr;
  101. $coners_alpha_coords['tr']['width']=imagesx($corner_tr);
  102. $coners_alpha_coords['tr']['height']=imagesy($corner_tr);
  103. $coners_alpha_coords['tr']['start_width']=($width-$coners_alpha_coords['tr']['width']);
  104. $coners_alpha_coords['tr']['start_height']=0;
  105. $coners_alpha_coords['tr']['alpha']=array();
  106. $coners_alpha_coords['b1']['image']=&$corner_bl;
  107. $coners_alpha_coords['b1']['width']=imagesx($corner_bl);
  108. $coners_alpha_coords['b1']['height']=imagesy($corner_bl);
  109. $coners_alpha_coords['b1']['start_width']=0;
  110. $coners_alpha_coords['b1']['start_height']=($height-$coners_alpha_coords['b1']['height']);
  111. $coners_alpha_coords['b1']['alpha']=array();
  112. $coners_alpha_coords['br']['image']=&$corner_br;
  113. $coners_alpha_coords['br']['width']=imagesx($corner_br);
  114. $coners_alpha_coords['br']['height']=imagesy($corner_br);
  115. $coners_alpha_coords['br']['start_width']=($width-$coners_alpha_coords['br']['width']);
  116. $coners_alpha_coords['br']['start_height']=($height-$coners_alpha_coords['br']['height']);
  117. $coners_alpha_coords['br']['alpha']=array();
  118. foreach($coners_alpha_coords as $key=>&$value){
  119. for($x=0;$x<$value['width'];$x++){
  120. for($y=0;$y<$value['height'];$y++){
  121. //получаем прозрачность в уголке
  122. $color_index = @imagecolorat($value['image'], $x, $y);
  123. $color_tran = imagecolorsforindex($value['image'], $color_index);
  124. $alpha=$color_tran['alpha'];
  125. if($alpha<127) $coners_alpha_coords[$key]['alpha'][$x][$y]=(127-$alpha);
  126. }
  127. }
  128. }unset($value);
  129. }else{
  130. $coners_alpha_coords['tr']['start_width']=($width-$coners_alpha_coords['tr']['width']);
  131. $coners_alpha_coords['b1']['start_height']=($height-$coners_alpha_coords['b1']['height']);
  132. $coners_alpha_coords['br']['start_width']=($width-$coners_alpha_coords['br']['width']);
  133. $coners_alpha_coords['br']['start_height']=($height-$coners_alpha_coords['br']['height']);
  134. }
  135. if($type=='png'){
  136. foreach($coners_alpha_coords as $key=>&$value){
  137. foreach($value['alpha'] as $x=>$value2){
  138. foreach($value2 as $y=>$alpha){
  139. $color_index = @imagecolorat($image, $value['start_width']+$x, $value['start_height']+$y);
  140. $color_tran = imagecolorsforindex($image, $color_index);
  141. $new_color=imagecolorallocatealpha($image,$color_tran['red'],$color_tran['green'],$color_tran['blue'],$alpha);
  142. imagesetpixel($image,$value['start_width']+$x,$value['start_height']+$y,$new_color);
  143. }
  144. }
  145. }unset($pixels);
  146. }else {
  147. foreach($coners_alpha_coords as $key=>&$value){
  148. imagecopyresampled($image,$value['image'],$value['start_width'],$value['start_height'],0,0,$value['width'],$value['height'],$value['width'],$value['height']);
  149. }unset($value);
  150. }
  151. }
  152. /**
  153. * Make IPTC teg
  154. *
  155. * @param int $rec. Hren ego znaet zachem
  156. * @param string $data. Name of IPTC tag
  157. * @param string $value. Value of IPTC tag
  158. * @return string. Binary data of IPTC tag
  159. */
  160. function iptc_make_tag($rec, $data, $value)
  161. {
  162. $length = strlen($value);
  163. $retval = chr(0x1C) . chr($rec) . chr($data);
  164. if($length < 0x8000)
  165. {
  166. $retval .= chr($length >> 8) . chr($length & 0xFF);
  167. }
  168. else
  169. {
  170. $retval .= chr(0x80) .
  171. chr(0x04) .
  172. chr(($length >> 24) & 0xFF) .
  173. chr(($length >> 16) & 0xFF) .
  174. chr(($length >> 8) & 0xFF) .
  175. chr($length & 0xFF);
  176. }
  177. return $retval . $value;
  178. }
  179. /**
  180. * Resize all image in storage by originals
  181. *
  182. * @return unknown
  183. */
  184. function resize_images_in_storage()
  185. {
  186. die();
  187. $report=array();
  188. $objects_count=0;
  189. $users_count=0;
  190. $photos_objects_count=0;
  191. $photos_users_count=0;
  192. global $avatar_types;
  193. global $image_types;
  194. include_once(__ENGINE_PATH.'/functions/subfunctions/file_functions.php');
  195. $report[]="Процедура начата в ".date("H:i:s");
  196. $objects=array();
  197. $avatars_dir['objects'] =__MEDIA_STORAGE_PATH."/avatars/objects/";
  198. $avatars_dir['users'] =__MEDIA_STORAGE_PATH."/avatars/users/";
  199. $photos_dir['objects'] =__MEDIA_STORAGE_PATH."/photos/objects/";
  200. $photos_dir['users'] =__MEDIA_STORAGE_PATH."/photos/users/";
  201. $avatars_res['objects'] =opendir(__MEDIA_STORAGE_PATH."/avatars/objects/");
  202. $avatars_res['users'] =opendir(__MEDIA_STORAGE_PATH."/avatars/users/");
  203. $photos_res['objects'] =opendir(__MEDIA_STORAGE_PATH."/photos/objects/");
  204. $photos_res['users'] =opendir(__MEDIA_STORAGE_PATH."/photos/users/");
  205. /*while (false !== ($object_type_dir = readdir($avatars_res['objects']))) {
  206. if ($object_type_dir==".." || $object_type_dir=="." || !is_dir($avatars_dir['objects'].$object_type_dir))continue;
  207. $object_type_res=opendir($avatars_dir['objects'].$object_type_dir);
  208. while (false !== ($object_dir = readdir($object_type_res))) {
  209. if ($object_dir==".." || $object_dir=="." || !is_dir($avatars_dir['objects'].$object_type_dir.'/'.$object_dir))continue;
  210. $object_dir=$avatars_dir['objects'].$object_type_dir.'/'.$object_dir;
  211. $objects_count++;
  212. if(!file_exists($object_dir."/1/")){$report[]="Отсутствует папка {$object_dir}/1/";continue;}
  213. if(!file_exists($object_dir."/6/")){$report[]="Отсутствует папка {$object_dir}/6/";continue;}
  214. $object_type1_res=opendir($object_dir.'/1');
  215. $object_type6_res=opendir($object_dir.'/6');
  216. $avatars_info=array();
  217. $type1=array();
  218. while (false !== ($file = readdir($object_type1_res)))
  219. {
  220. if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
  221. $type1[]=$file;
  222. //получаем описание аватаров
  223. $info=array();
  224. $size=getimagesize($object_dir."/1/".$file,$info);
  225. $file=substr($file,0,-4);
  226. if(isset($info["APP13"])){
  227. $iptc = iptcparse ($info["APP13"]);
  228. $avatars_info[$file]=(string)$iptc["2#005"][0];
  229. }else $avatars_info[$file]='';
  230. }
  231. $type6=array();
  232. while (false !== ($file = readdir($object_type6_res)))
  233. {
  234. if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
  235. $type6[]=$file;
  236. }
  237. if(count(array_diff($type1,$type6))){$report[]="Количество аватаров и оригиналов не совпадает '{$object_dir}'.";continue;}
  238. //echo $object_dir.'/6';
  239. foreach($type6 as $image){
  240. set_time_limit(5);
  241. $img=imagecreatefromjpeg($object_dir.'/6/'.$image);
  242. if(!$img)$report[]="Не удалось открыть картинку ".$object_dir.'/6/'.$image;
  243. foreach($avatar_types as $key=>$value)
  244. {
  245. if($key == 6)continue;
  246. if(!file_exists($object_dir.'/'.$key)){
  247. umask(0);
  248. if(!@mkdir($object_dir.'/'.$key,0777)){
  249. $report[]="Не удалось создать папку ".$object_dir.'/'.$key;
  250. continue;
  251. }
  252. }
  253. set_time_limit(5);
  254. $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
  255. if($new_image) {
  256. if($value['type']=='png'){
  257. if(!@imagepng($new_image,$object_dir.'/'.$key.'/'.$image)){
  258. $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
  259. }
  260. }else {
  261. if(!@imagejpeg($new_image,$object_dir.'/'.$key.'/'.$image,85)){
  262. $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
  263. }
  264. }
  265. }
  266. if($key == 0)
  267. {
  268. $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
  269. $content = iptcembed($iptc_new, $object_dir.'/0/'.$image, 0);
  270. $fp = @fopen($object_dir.'/0/'.$image, "w");
  271. if($fp){
  272. fwrite($fp, $content);
  273. fclose($fp);
  274. }else $report[]='Не удалось установить IPTC теги для '.$object_dir.'/0/'.$image;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. /////////////////////////////
  281. //аватары пользователей
  282. while (false !== ($object_dir = readdir($avatars_res['users']))) {
  283. if ($object_dir==".." || $object_dir=="." || !is_dir($avatars_dir['users'].$object_dir))continue;
  284. $object_dir=$avatars_dir['users'].$object_type_dir.'/'.$object_dir;
  285. $users_count++;
  286. if(!file_exists($object_dir."/6/")){$report[]="Отсутствует папка {$object_dir}/6/";continue;}
  287. $object_type6_res=opendir($object_dir.'/6');
  288. $type6=array();
  289. while (false !== ($file = readdir($object_type6_res)))
  290. {
  291. if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
  292. $type6[]=$file;
  293. }
  294. if(count($type6)==0)continue;
  295. foreach($type6 as $image){
  296. set_time_limit(5);
  297. $img=imagecreatefromjpeg($object_dir.'/6/'.$image);
  298. if(!$img)$report[]="Не удалось открыть картинку ".$object_dir.'/6/'.$image;
  299. foreach($avatar_types as $key=>$value)
  300. {
  301. if($key == 6)break;
  302. if(!file_exists($object_dir.'/'.$key)){
  303. umask(0);
  304. if(!@mkdir($object_dir.'/'.$key,0777)){
  305. $report[]="Не удалось создать папку ".$object_dir.'/'.$key;
  306. continue;
  307. }
  308. }
  309. set_time_limit(5);
  310. $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
  311. if($new_image) {
  312. if($value['type']=='png'){
  313. if(!@imagepng($new_image,$object_dir.'/'.$key.'/'.$image)){
  314. $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
  315. }
  316. }else {
  317. if(!@imagejpeg($new_image,$object_dir.'/'.$key.'/'.$image,85)){
  318. $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }*/
  325. $count=0;
  326. ////////////////////////////////
  327. //фотоальбомы объектов
  328. /* while (false !== ($object_type_dir = readdir($photos_res['objects']))) {
  329. if ($object_type_dir==".." || $object_type_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir))continue;
  330. $object_type_res=opendir($photos_dir['objects'].$object_type_dir);
  331. if($count > 5)die();
  332. while (false !== ($object_dir = readdir($object_type_res))) {
  333. if ($object_dir==".." || $object_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir.'/'.$object_dir))continue;
  334. $object_album_res=opendir($photos_dir['objects'].$object_type_dir.'/'.$object_dir);
  335. if($count > 5)die();
  336. while (false !== ($object_album_dir = readdir($object_album_res)) ) {
  337. $count++;
  338. if($count > 5)die();
  339. if ($object_album_dir==".." || $object_album_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir.'/'.$object_dir.'/'.$object_album_dir))continue;
  340. $object_album_dir=$photos_dir['objects'].$object_type_dir.'/'.$object_dir.'/'.$object_album_dir;
  341. $photos_objects_count++;
  342. if(!file_exists($object_album_dir."/2/")){$report[]="Отсутствует папка {$object_album_dir}/2/";continue;}
  343. $object_type6_res=opendir($object_album_dir.'/2/');
  344. $avatars_info=array();
  345. $avatars_order=array();
  346. $avatars_private=array();
  347. $type6=array();
  348. while (false !== ($file = readdir($object_type6_res)))
  349. {
  350. if ($file==".." || $file=="." || $file=="Thumbs.db" || !is_file($object_album_dir.'/'.$file))continue;
  351. $type6[]=$file;
  352. //получаем описание аватаров
  353. $info=array();
  354. $size=getimagesize($object_album_dir."/2/".$file,$info);
  355. $file=substr($file,0,-4);
  356. print_r($info);echo "<br/>";
  357. if(isset($info["APP13"])){
  358. $iptc = iptcparse ($info["APP13"]);
  359. $avatars_info[$file]=(string)$iptc["2#005"][0];
  360. $avatars_order[$file]=$iptc["2#010"][0];
  361. $avatars_private[$file]=$iptc["2#015"][0];
  362. }else {
  363. $avatars_info[$file]='';
  364. $avatars_order[$file]=100;
  365. $avatars_private[$file]=0;
  366. }
  367. }
  368. foreach ($image_types as $key=>&$value){
  369. if($key==1) continue;
  370. dir_remove($object_album_dir."/".$key,true);
  371. }unset($value);
  372. foreach($type6 as $image){
  373. set_time_limit(5);
  374. $img=imagecreatefromjpeg($object_album_dir.'/'.$image);
  375. if(!$img)$report[]="Не удалось открыть картинку ".$object_album_dir.'/'.$image;
  376. foreach($image_types as $key=>&$value)
  377. {
  378. if($key == 1)continue;
  379. if(!file_exists($object_album_dir.'/'.$key))mkdir($object_album_dir.'/'.$key,0777);
  380. set_time_limit(5);
  381. $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
  382. if($new_image) {
  383. if($value['type']=='png'){
  384. if(!@imagepng($new_image,$object_album_dir.'/'.$key.'/'.$image)){
  385. $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
  386. }
  387. }else {
  388. if(!@imagejpeg($new_image,$object_album_dir.'/'.$key.'/'.$image,85)){
  389. $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
  390. }
  391. }
  392. }
  393. if($key==3 || $key==4){
  394. $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
  395. $iptc_new.=iptc_make_tag(2,'010',$avatars_order[substr($image,0,-4)]);
  396. $iptc_new.=iptc_make_tag(2,'015',$avatars_private[substr($image,0,-4)]);
  397. $content = iptcembed($iptc_new, $object_album_dir.'/'.$key.'/'.$image, 0);
  398. $fp = @fopen($object_album_dir.'/'.$key.'/'.$image, "w");
  399. if($fp){
  400. fwrite($fp, $content);
  401. fclose($fp);
  402. }else $report[]='Не удалось установить IPTC теги для '.$object_album_dir.'/'.$key.'/'.$image;
  403. }
  404. }unset($value);
  405. }
  406. }
  407. }
  408. }*/
  409. ////////////////////////////////
  410. //фотоальбомы пользователей
  411. while (false !== ($object_dir = readdir($photos_res['users']))) {
  412. if ($object_dir==".." || $object_dir=="." || !is_dir($photos_dir['users'].$object_dir))continue;
  413. $object_album_res=opendir($photos_dir['users'].$object_dir);
  414. while (false !== ($object_album_dir = readdir($object_album_res))) {
  415. $count++;
  416. //if($count > 10)die();
  417. if ($object_album_dir==".." || $object_album_dir=="." || !is_dir($photos_dir['users'].$object_dir.'/'.$object_album_dir))continue;
  418. $object_album_dir=$photos_dir['users'].$object_dir.'/'.$object_album_dir;
  419. $photos_users_count++;
  420. if(!file_exists($object_album_dir."/2/")){$report[]="Отсутствует папка {$object_album_dir}/2/";continue;}
  421. $object_type6_res=opendir($object_album_dir.'/2/');
  422. $avatars_info=array();
  423. $avatars_order=array();
  424. $avatars_private=array();
  425. $type6=array();
  426. while (false !== ($file = readdir($object_type6_res)))
  427. {
  428. if ($file==".." || $file=="." || $file=="Thumbs.db" || !is_file($object_album_dir.'/'.$file))continue;
  429. $type6[]=$file;
  430. //получаем описание аватаров
  431. $info=array();
  432. $size=getimagesize($object_album_dir."/3/".$file,$info);
  433. $file=substr($file,0,-4);
  434. //print_r($info);echo "<br/>";
  435. //print_r(iptcparse ($info["APP13"])).' <br/>';
  436. if(isset($info["APP13"])){
  437. $iptc = iptcparse ($info["APP13"]);
  438. $avatars_info[$file]=(string)$iptc["2#005"][0];
  439. $avatars_order[$file]=$iptc["2#010"][0];
  440. $avatars_private[$file]=$iptc["2#015"][0];
  441. }else {
  442. $avatars_info[$file]='';
  443. $avatars_order[$file]=100;
  444. $avatars_private[$file]=0;
  445. }
  446. }
  447. foreach ($image_types as $key=>&$value){
  448. if($key==1) continue;
  449. dir_remove($object_album_dir."/".$key,true);
  450. }unset($value);
  451. foreach($type6 as $image){
  452. set_time_limit(5);
  453. $img=imagecreatefromjpeg($object_album_dir.'/'.$image);
  454. if(!$img)$report[]="Не удалось открыть картинку ".$object_album_dir.'/'.$image;
  455. foreach($image_types as $key=>&$value)
  456. {
  457. if($key == 1)continue;
  458. set_time_limit(5);
  459. if(!file_exists($object_album_dir.'/'.$key))mkdir($object_album_dir.'/'.$key,0777);
  460. if (App::isSmpz()) {
  461. $new_image=&image_resize($image,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
  462. } else {
  463. $new_image=&image_resize($image,$value['x'],$value['y'],$value['crop'],false,$value['type']);
  464. }
  465. if($new_image) {
  466. if($value['type']=='png'){
  467. if(!@imagepng($new_image,$object_album_dir.'/'.$key.'/'.$image)){
  468. $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
  469. }
  470. }else {
  471. if(!@imagejpeg($new_image,$object_album_dir.'/'.$key.'/'.$image,85)){
  472. $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
  473. }
  474. }
  475. }
  476. if($key==3){
  477. $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
  478. $iptc_new.=iptc_make_tag(2,'010',$avatars_order[substr($image,0,-4)]);
  479. $iptc_new.=iptc_make_tag(2,'015',$avatars_private[substr($image,0,-4)]);
  480. $content = iptcembed($iptc_new, $object_album_dir.'/'.$key.'/'.$image, 0);
  481. $fp = @fopen($object_album_dir.'/'.$key.'/'.$image, "w");
  482. if($fp){
  483. fwrite($fp, $content);
  484. fclose($fp);
  485. }else $report[]='Не удалось установить IPTC теги для '.$object_album_dir.'/'.$key.'/'.$image;
  486. }
  487. }unset($value);
  488. }
  489. }
  490. }
  491. $report[]="Процедура окончена в ".date("h:i:s");
  492. $report[]="Затронуто объектов: {$objects_count}";
  493. $report[]="Затронуто пользователей: {$users_count}";
  494. $report[]="Затронуто фотоальбомов пользователей: {$photos_users_count}";
  495. $report[]="Затронуто фотоальбомов объектов: {$photos_objects_count}";
  496. return $report;
  497. }
  498. function imagecreatefrombmp($filename)
  499. {
  500. $tmp_name = tempnam(__MEDIA_STORAGE_PATH."/avatars/temp", "GD");
  501. if(bmp2gd($filename, $tmp_name)) {
  502. $img = imagecreatefromgd($tmp_name);
  503. unlink($tmp_name);
  504. return $img;
  505. }
  506. return false;
  507. }
  508. function bmp2gd($src, $dest = false)
  509. {
  510. if(!($src_f = fopen($src, "rb")))
  511. {
  512. return false;
  513. }
  514. if(!($dest_f = fopen($dest, "wb")))
  515. {
  516. return false;
  517. }
  518. $header = unpack("vtype/Vsize/v2reserved/Voffset", fread( $src_f, 14));
  519. $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",
  520. fread($src_f, 40));
  521. extract($info);
  522. extract($header);
  523. if($type != 0x4D42)
  524. {
  525. return false;
  526. }
  527. $palette_size = $offset - 54;
  528. $ncolor = $palette_size / 4;
  529. $gd_header = "";
  530. $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
  531. $gd_header .= pack("n2", $width, $height);
  532. $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
  533. if($palette_size) {
  534. $gd_header .= pack("n", $ncolor);
  535. }
  536. $gd_header .= "\xFF\xFF\xFF\xFF";
  537. fwrite($dest_f, $gd_header);
  538. if($palette_size)
  539. {
  540. $palette = fread($src_f, $palette_size);
  541. $gd_palette = "";
  542. $j = 0;
  543. while($j < $palette_size)
  544. {
  545. $b = $palette{$j++};
  546. $g = $palette{$j++};
  547. $r = $palette{$j++};
  548. $a = $palette{$j++};
  549. $gd_palette .= "$r$g$b$a";
  550. }
  551. $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
  552. fwrite($dest_f, $gd_palette);
  553. }
  554. $scan_line_size = (($bits * $width) + 7) >> 3;
  555. $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
  556. for($i = 0, $l = $height - 1; $i < $height; $i++, $l--)
  557. {
  558. fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
  559. $scan_line = fread($src_f, $scan_line_size);
  560. if($bits == 24)
  561. {
  562. $gd_scan_line = "";
  563. $j = 0;
  564. while($j < $scan_line_size)
  565. {
  566. $b = $scan_line{$j++};
  567. $g = $scan_line{$j++};
  568. $r = $scan_line{$j++};
  569. $gd_scan_line .= "\x00$r$g$b";
  570. }
  571. }
  572. elseif($bits == 8)
  573. {
  574. $gd_scan_line = $scan_line;
  575. }
  576. elseif($bits == 4)
  577. {
  578. $gd_scan_line = "";
  579. $j = 0;
  580. while($j < $scan_line_size)
  581. {
  582. $byte = ord($scan_line{$j++});
  583. $p1 = chr($byte >> 4);
  584. $p2 = chr($byte & 0x0F);
  585. $gd_scan_line .= "$p1$p2";
  586. }
  587. $gd_scan_line = substr($gd_scan_line, 0, $width);
  588. }
  589. elseif($bits == 1)
  590. {
  591. $gd_scan_line = "";
  592. $j = 0;
  593. while($j < $scan_line_size)
  594. {
  595. $byte = ord($scan_line{$j++});
  596. $p1 = chr((int) (($byte & 0x80) != 0));
  597. $p2 = chr((int) (($byte & 0x40) != 0));
  598. $p3 = chr((int) (($byte & 0x20) != 0));
  599. $p4 = chr((int) (($byte & 0x10) != 0));
  600. $p5 = chr((int) (($byte & 0x08) != 0));
  601. $p6 = chr((int) (($byte & 0x04) != 0));
  602. $p7 = chr((int) (($byte & 0x02) != 0));
  603. $p8 = chr((int) (($byte & 0x01) != 0));
  604. $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
  605. }
  606. $gd_scan_line = substr($gd_scan_line, 0, $width);
  607. }
  608. fwrite($dest_f, $gd_scan_line);
  609. }
  610. fclose($src_f);
  611. fclose($dest_f);
  612. return true;
  613. }
  614. /* Создание watermarka (водяной знак) */
  615. function add_watermark($simple_image,$water_image,$result_image)
  616. {
  617. $size_water = getimagesize($water_image);
  618. $size_image = getimagesize($simple_image);
  619. // свойство водяного знака
  620. $water['width'] = $size_water['0'];
  621. $water['height'] = $size_water['1'];
  622. $water['padding'] = 17; // отступ водяного знака
  623. if( $size_image['0'] < ($water['width'] * 2.5) || $size_image['1'] < ($water['height'] * 2.5) ) {
  624. return;
  625. }
  626. // Проверка MIME
  627. if( $size_water['mime'] != 'image/png' ) {
  628. return;
  629. }
  630. $im_water = imagecreatefrompng($water_image);
  631. imagealphablending($im_water, false);
  632. imagesavealpha($im_water, true);
  633. $im_simple = imagecreatefromjpeg($simple_image);
  634. $final_x = $size_image['0'] - $water['width'] - $water['padding'];
  635. $final_y = $size_image['1'] - $water['height'] - $water['padding'];
  636. // Добавление водяного знака в изображение
  637. imagecopy($im_simple, $im_water, $final_x, $final_y, 0, 0, $water['width'], $water['height']);
  638. // Сохранение итоговой картинки
  639. imagejpeg($im_simple, $result_image, 100);
  640. imagedestroy($im_water);
  641. imagedestroy($im_simple);
  642. }
  643. /* Вырезание участка изображения (crop) */
  644. function image_crop($image_source, $x1, $y1, $w, $h)
  645. {
  646. $image = $image_source;
  647. if( preg_match('|^/avatars/users/.*|', $image) && !preg_match('|^/avatars/objects/.*|', $image) )
  648. {
  649. $uid = preg_replace('|.*/([^_]+)_.*|','$1',$image);
  650. $key = preg_replace('|.*/[^_]+_(\d+).*|','$1',$image);
  651. $type = preg_replace('|.*/[^_]+_\d+_(\d+).*|','$1',$image);
  652. $file = '1.jpg';
  653. $ext = preg_replace('|.*\.(.*)|','$1',$file);;
  654. $path_start = '/avatars/users/'.$uid;
  655. $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$type.'/'.$file;
  656. }
  657. else if( preg_match('|^/avatars/\d-.*|', $image) || preg_match('|^/avatars/[\d\w]+.*|', $image) ) // при загрузке картинок
  658. {
  659. $image = preg_replace('/(.*)\?.*$/','$1',$image);
  660. $type = preg_replace('/.*avatars\/(temp|objects)\/([^-]+)-.*/','$2',$image);
  661. $pid = preg_replace('|.*avatars/[^-]+-([\d]+).*|','$1',$image);
  662. $oid = preg_replace('|.*avatars/([^/]+).*|','$1',$image);
  663. $key = preg_replace('/.*[\d]+_([\d]+)(\..*)/','$1', $image);
  664. $file = preg_replace('/.*([\d]+)_[\d]+(\..*)/','$1$2', $image);
  665. $ext = preg_replace('|.*\.(.*)|','$1',$file);
  666. if( preg_match('|^/avatars/objects/.*|', $image) ) {
  667. $path_start = '/avatars/objects/'.$type.'/'.$pid;
  668. } else {
  669. $path_start = '/avatars/temp/'.$oid;
  670. }
  671. $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$key.'/'.$file;
  672. }
  673. else if( preg_match('|^/avatars/objects/.*|', $image) )
  674. {
  675. $image = preg_replace('/(.*)\?.*$/','$1',$image);
  676. $type = preg_replace('|.*objects/([^-]+)-.*|','$1',$image);
  677. $pid = preg_replace('|.*objects/[^-]+-([^_]+)_.*|','$1',$image);
  678. $key = preg_replace('/.*[\d]+_([\d]+)(\..*)/','$1', $image);
  679. $file = preg_replace('/.*([\d]+)_[\d]+(\..*)/','$1$2', $image);
  680. $ext = preg_replace('|.*\.(.*)|','$1',$file);
  681. $path_start = '/avatars/objects';
  682. $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$type.'/'.$pid.'/'.$key.'/'.$file;
  683. }
  684. if( $ext == 'jpg' )
  685. {
  686. // начало создания миниатюры из большой картинки (10)
  687. $im = imagecreatefromjpeg( $path_full );
  688. $im_size = getimagesize( $path_full );
  689. $sto_percent_x1 = $im_size['0'];
  690. $sto_percent_y1 = $im_size['1'];
  691. if($sto_percent_x1 > $sto_percent_y1){
  692. $koefW = 400;
  693. $koefH = round(($koefW/$sto_percent_x1)*$sto_percent_y1);
  694. }
  695. else{
  696. $koefH = 400;
  697. $koefW = round(($koefH/$sto_percent_y1)*$sto_percent_x1);
  698. }
  699. $precents_preview_x1 = $x1 / $koefW;
  700. $precents_preview_y1 = $y1 / $koefH;
  701. $precents_preview_w = $sto_percent_x1 * ($w / $koefW);
  702. $precents_preview_h = $sto_percent_y1 * ($h / $koefH);
  703. $x1_fimage = round( $sto_percent_x1 * $precents_preview_x1 ); // координата x1
  704. $y1_fimage = round( $sto_percent_y1 * $precents_preview_y1 ); // координата y1
  705. // создание миниатюры
  706. $dest = imagecreatetruecolor($precents_preview_w, $precents_preview_h);
  707. imagecopy($dest, $im, 0, 0, $x1_fimage, $y1_fimage, $precents_preview_w, $precents_preview_h);
  708. include_once(__ENGINE_PATH.'/functions/subfunctions/common.arrays.php');
  709. global $avatar_types;
  710. global $image_types;
  711. $i = 0;
  712. foreach( $avatar_types as $key => $avatar )
  713. {
  714. if( !$avatar['crop'] ) {
  715. $i++;
  716. continue;
  717. }
  718. $path_save = __MEDIA_STORAGE_PATH.$path_start.'/';
  719. $path_save .= $key.'/'.$file;
  720. if (App::isSmpz()) {
  721. $im_final = image_resize($dest, $avatar['x'], $avatar['y']);
  722. } else {
  723. $im_final = image_resize($dest, $avatar['x'], $avatar['y'], false, false);
  724. }
  725. if( !imagejpeg($im_final, $path_save, 100) )
  726. {
  727. return 0;
  728. }
  729. $i++;
  730. }
  731. imagedestroy($dest);
  732. imagedestroy($im);
  733. imagedestroy($im_final);
  734. return 1;
  735. }
  736. }