/engine/functions/subfunctions/image_functions.php
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
- <?php
- global $coners_alpha_coords;
- $coners_alpha_coords=array();
- /**
- * Resize image
- *
- * @param Image object $image
- * @param int $new_x.New width
- * @param int $new_y. New height
- * @param bool $cropping. Cropping flag
- * @param bool $round_edges. Need to round edges
- * @return Image object
- */
- function &image_resize($image,$new_x,$new_y,$cropping=false,$round_edges=true,$type='jpg')
- {
- $fff=$new_y;
- $new_image=false;
- $x_size=imagesx($image);
- $y_size=imagesy($image);
- $koeff_x=$x_size/$new_x;
- $koeff_y=$y_size/$new_y;
- $koeff_min=min($koeff_x,$koeff_y);
- $koeff_max=max($koeff_x,$koeff_y);
- if(!$cropping && $koeff_max<=1) {
- if($round_edges)image_effect_round_edges($image,$type);
- return $image;
- }
- if(!$cropping)
- {
- $otstup_x=0;
- $otstup_y=0;
- }
- else
- {
- $otstup_x=($koeff_x>$koeff_y)?round(((($x_size/$koeff_min)-$new_x)*$koeff_min)/2):0;
- if($otstup_x<0)$otstup_x=0;
- $otstup_y=($koeff_x>$koeff_y)?0:round(((($y_size/$koeff_min)-$new_y)*$koeff_min)/2);
- if($otstup_y<0)$otstup_y=0;
- }
-
- if(!$cropping)
- {
- //вычисляем новый размер учитывая пропорции оригинальной картинки
- $new_x=ceil($x_size/$koeff_max);
- $new_y=ceil($y_size/$koeff_max);
- $new_image=imagecreatetruecolor($new_x,$new_y);
- if(!imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_x, $new_y, $x_size, $y_size))
- {
- trigger_error('Не удалось сделать ресайз изображения.',E_USER_ERROR);
- $new_image=false;
- }
- }
- else
- {
- $new_image=imagecreatetruecolor($new_x,$new_y);
- if($koeff_min>1)
- {
- 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))
- {
- trigger_error('Не удалось сделать ресайз изображения.',E_USER_WARNING);
- $new_image=false;
- }
- }
- else
- {
- 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))
- {
- trigger_error('Не удалось сделать CROP изображения.',E_USER_WARNING);
- $new_image=false;
- }
- }
- }
-
- if($round_edges)image_effect_round_edges($new_image,$type);
-
- return $new_image;
- }
- /**
- * Round corner for image
- *
- * @param GD obejct $image
- */
- function image_effect_round_edges(&$image,$type)
- {
- global $coners_alpha_coords;
- $width=imagesx($image);
- $height=imagesy($image);
-
- if($type=='png'){
- imageAlphaBlending($image, false);
- imageSaveAlpha($image, true);
- }
-
- if(count($coners_alpha_coords)==0){
- $corner_tl=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_top_left.png');
- $corner_tr=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_top_right.png');
- $corner_bl=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_bottom_left.png');
- $corner_br=imagecreatefrompng(__APP_PATH.'/design/images/avatar_corner_bottom_right.png');
- $coners_alpha_coords['t1']['image']=&$corner_tl;
- $coners_alpha_coords['t1']['width']=imagesx($corner_tl);
- $coners_alpha_coords['t1']['height']=imagesy($corner_tl);
- $coners_alpha_coords['t1']['start_width']=0;
- $coners_alpha_coords['t1']['start_height']=0;
- $coners_alpha_coords['t1']['alpha']=array();
-
- $coners_alpha_coords['tr']['image']=&$corner_tr;
- $coners_alpha_coords['tr']['width']=imagesx($corner_tr);
- $coners_alpha_coords['tr']['height']=imagesy($corner_tr);
- $coners_alpha_coords['tr']['start_width']=($width-$coners_alpha_coords['tr']['width']);
- $coners_alpha_coords['tr']['start_height']=0;
- $coners_alpha_coords['tr']['alpha']=array();
-
- $coners_alpha_coords['b1']['image']=&$corner_bl;
- $coners_alpha_coords['b1']['width']=imagesx($corner_bl);
- $coners_alpha_coords['b1']['height']=imagesy($corner_bl);
- $coners_alpha_coords['b1']['start_width']=0;
- $coners_alpha_coords['b1']['start_height']=($height-$coners_alpha_coords['b1']['height']);
- $coners_alpha_coords['b1']['alpha']=array();
-
- $coners_alpha_coords['br']['image']=&$corner_br;
- $coners_alpha_coords['br']['width']=imagesx($corner_br);
- $coners_alpha_coords['br']['height']=imagesy($corner_br);
- $coners_alpha_coords['br']['start_width']=($width-$coners_alpha_coords['br']['width']);
- $coners_alpha_coords['br']['start_height']=($height-$coners_alpha_coords['br']['height']);
- $coners_alpha_coords['br']['alpha']=array();
-
-
- foreach($coners_alpha_coords as $key=>&$value){
- for($x=0;$x<$value['width'];$x++){
- for($y=0;$y<$value['height'];$y++){
- //получаем прозрачность в уголке
- $color_index = @imagecolorat($value['image'], $x, $y);
- $color_tran = imagecolorsforindex($value['image'], $color_index);
- $alpha=$color_tran['alpha'];
- if($alpha<127) $coners_alpha_coords[$key]['alpha'][$x][$y]=(127-$alpha);
- }
- }
- }unset($value);
- }else{
- $coners_alpha_coords['tr']['start_width']=($width-$coners_alpha_coords['tr']['width']);
- $coners_alpha_coords['b1']['start_height']=($height-$coners_alpha_coords['b1']['height']);
- $coners_alpha_coords['br']['start_width']=($width-$coners_alpha_coords['br']['width']);
- $coners_alpha_coords['br']['start_height']=($height-$coners_alpha_coords['br']['height']);
- }
-
-
-
- if($type=='png'){
- foreach($coners_alpha_coords as $key=>&$value){
- foreach($value['alpha'] as $x=>$value2){
- foreach($value2 as $y=>$alpha){
- $color_index = @imagecolorat($image, $value['start_width']+$x, $value['start_height']+$y);
- $color_tran = imagecolorsforindex($image, $color_index);
-
- $new_color=imagecolorallocatealpha($image,$color_tran['red'],$color_tran['green'],$color_tran['blue'],$alpha);
- imagesetpixel($image,$value['start_width']+$x,$value['start_height']+$y,$new_color);
- }
- }
- }unset($pixels);
- }else {
- foreach($coners_alpha_coords as $key=>&$value){
- imagecopyresampled($image,$value['image'],$value['start_width'],$value['start_height'],0,0,$value['width'],$value['height'],$value['width'],$value['height']);
- }unset($value);
- }
- }
- /**
- * Make IPTC teg
- *
- * @param int $rec. Hren ego znaet zachem
- * @param string $data. Name of IPTC tag
- * @param string $value. Value of IPTC tag
- * @return string. Binary data of IPTC tag
- */
- function iptc_make_tag($rec, $data, $value)
- {
- $length = strlen($value);
- $retval = chr(0x1C) . chr($rec) . chr($data);
- if($length < 0x8000)
- {
- $retval .= chr($length >> 8) . chr($length & 0xFF);
- }
- else
- {
- $retval .= chr(0x80) .
- chr(0x04) .
- chr(($length >> 24) & 0xFF) .
- chr(($length >> 16) & 0xFF) .
- chr(($length >> 8) & 0xFF) .
- chr($length & 0xFF);
- }
- return $retval . $value;
- }
- /**
- * Resize all image in storage by originals
- *
- * @return unknown
- */
- function resize_images_in_storage()
- {
- die();
- $report=array();
- $objects_count=0;
- $users_count=0;
- $photos_objects_count=0;
- $photos_users_count=0;
- global $avatar_types;
- global $image_types;
-
- include_once(__ENGINE_PATH.'/functions/subfunctions/file_functions.php');
-
- $report[]="Процедура начата в ".date("H:i:s");
-
- $objects=array();
-
- $avatars_dir['objects'] =__MEDIA_STORAGE_PATH."/avatars/objects/";
- $avatars_dir['users'] =__MEDIA_STORAGE_PATH."/avatars/users/";
- $photos_dir['objects'] =__MEDIA_STORAGE_PATH."/photos/objects/";
- $photos_dir['users'] =__MEDIA_STORAGE_PATH."/photos/users/";
-
- $avatars_res['objects'] =opendir(__MEDIA_STORAGE_PATH."/avatars/objects/");
- $avatars_res['users'] =opendir(__MEDIA_STORAGE_PATH."/avatars/users/");
- $photos_res['objects'] =opendir(__MEDIA_STORAGE_PATH."/photos/objects/");
- $photos_res['users'] =opendir(__MEDIA_STORAGE_PATH."/photos/users/");
-
-
- /*while (false !== ($object_type_dir = readdir($avatars_res['objects']))) {
-
- if ($object_type_dir==".." || $object_type_dir=="." || !is_dir($avatars_dir['objects'].$object_type_dir))continue;
- $object_type_res=opendir($avatars_dir['objects'].$object_type_dir);
-
- while (false !== ($object_dir = readdir($object_type_res))) {
- if ($object_dir==".." || $object_dir=="." || !is_dir($avatars_dir['objects'].$object_type_dir.'/'.$object_dir))continue;
-
- $object_dir=$avatars_dir['objects'].$object_type_dir.'/'.$object_dir;
- $objects_count++;
-
- if(!file_exists($object_dir."/1/")){$report[]="Отсутствует папка {$object_dir}/1/";continue;}
- if(!file_exists($object_dir."/6/")){$report[]="Отсутствует папка {$object_dir}/6/";continue;}
-
- $object_type1_res=opendir($object_dir.'/1');
- $object_type6_res=opendir($object_dir.'/6');
-
- $avatars_info=array();
- $type1=array();
- while (false !== ($file = readdir($object_type1_res)))
- {
-
- if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
- $type1[]=$file;
-
- //получаем описание аватаров
- $info=array();
- $size=getimagesize($object_dir."/1/".$file,$info);
- $file=substr($file,0,-4);
- if(isset($info["APP13"])){
- $iptc = iptcparse ($info["APP13"]);
- $avatars_info[$file]=(string)$iptc["2#005"][0];
- }else $avatars_info[$file]='';
- }
- $type6=array();
- while (false !== ($file = readdir($object_type6_res)))
- {
- if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
- $type6[]=$file;
- }
-
- if(count(array_diff($type1,$type6))){$report[]="Количество аватаров и оригиналов не совпадает '{$object_dir}'.";continue;}
- //echo $object_dir.'/6';
-
- foreach($type6 as $image){
-
- set_time_limit(5);
- $img=imagecreatefromjpeg($object_dir.'/6/'.$image);
- if(!$img)$report[]="Не удалось открыть картинку ".$object_dir.'/6/'.$image;
-
- foreach($avatar_types as $key=>$value)
- {
- if($key == 6)continue;
- if(!file_exists($object_dir.'/'.$key)){
- umask(0);
- if(!@mkdir($object_dir.'/'.$key,0777)){
- $report[]="Не удалось создать папку ".$object_dir.'/'.$key;
- continue;
- }
- }
-
- set_time_limit(5);
-
- $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
- if($new_image) {
- if($value['type']=='png'){
- if(!@imagepng($new_image,$object_dir.'/'.$key.'/'.$image)){
- $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
- }
- }else {
- if(!@imagejpeg($new_image,$object_dir.'/'.$key.'/'.$image,85)){
- $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
- }
- }
- }
- if($key == 0)
- {
- $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
- $content = iptcembed($iptc_new, $object_dir.'/0/'.$image, 0);
- $fp = @fopen($object_dir.'/0/'.$image, "w");
- if($fp){
- fwrite($fp, $content);
- fclose($fp);
- }else $report[]='Не удалось установить IPTC теги для '.$object_dir.'/0/'.$image;
- }
- }
- }
- }
- }
- /////////////////////////////
- //аватары пользователей
- while (false !== ($object_dir = readdir($avatars_res['users']))) {
-
- if ($object_dir==".." || $object_dir=="." || !is_dir($avatars_dir['users'].$object_dir))continue;
-
- $object_dir=$avatars_dir['users'].$object_type_dir.'/'.$object_dir;
- $users_count++;
-
- if(!file_exists($object_dir."/6/")){$report[]="Отсутствует папка {$object_dir}/6/";continue;}
-
- $object_type6_res=opendir($object_dir.'/6');
-
- $type6=array();
- while (false !== ($file = readdir($object_type6_res)))
- {
- if ($file==".." || $file=="." || $file=="Thumbs.db")continue;
- $type6[]=$file;
- }
- if(count($type6)==0)continue;
-
- foreach($type6 as $image){
-
- set_time_limit(5);
- $img=imagecreatefromjpeg($object_dir.'/6/'.$image);
- if(!$img)$report[]="Не удалось открыть картинку ".$object_dir.'/6/'.$image;
-
- foreach($avatar_types as $key=>$value)
- {
- if($key == 6)break;
- if(!file_exists($object_dir.'/'.$key)){
- umask(0);
- if(!@mkdir($object_dir.'/'.$key,0777)){
- $report[]="Не удалось создать папку ".$object_dir.'/'.$key;
- continue;
- }
- }
- set_time_limit(5);
-
- $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
-
- if($new_image) {
- if($value['type']=='png'){
- if(!@imagepng($new_image,$object_dir.'/'.$key.'/'.$image)){
- $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
- }
- }else {
- if(!@imagejpeg($new_image,$object_dir.'/'.$key.'/'.$image,85)){
- $report[]='Не удалось создать изображение '.$object_dir.'/'.$key.'/'.$image; continue;
- }
- }
- }
- }
- }
- }*/
-
- $count=0;
- ////////////////////////////////
- //фотоальбомы объектов
- /* while (false !== ($object_type_dir = readdir($photos_res['objects']))) {
-
- if ($object_type_dir==".." || $object_type_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir))continue;
- $object_type_res=opendir($photos_dir['objects'].$object_type_dir);
- if($count > 5)die();
- while (false !== ($object_dir = readdir($object_type_res))) {
-
- if ($object_dir==".." || $object_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir.'/'.$object_dir))continue;
- $object_album_res=opendir($photos_dir['objects'].$object_type_dir.'/'.$object_dir);
- if($count > 5)die();
- while (false !== ($object_album_dir = readdir($object_album_res)) ) {
- $count++;
- if($count > 5)die();
- if ($object_album_dir==".." || $object_album_dir=="." || !is_dir($photos_dir['objects'].$object_type_dir.'/'.$object_dir.'/'.$object_album_dir))continue;
- $object_album_dir=$photos_dir['objects'].$object_type_dir.'/'.$object_dir.'/'.$object_album_dir;
-
- $photos_objects_count++;
-
- if(!file_exists($object_album_dir."/2/")){$report[]="Отсутствует папка {$object_album_dir}/2/";continue;}
-
- $object_type6_res=opendir($object_album_dir.'/2/');
-
- $avatars_info=array();
- $avatars_order=array();
- $avatars_private=array();
- $type6=array();
- while (false !== ($file = readdir($object_type6_res)))
- {
-
- if ($file==".." || $file=="." || $file=="Thumbs.db" || !is_file($object_album_dir.'/'.$file))continue;
- $type6[]=$file;
-
- //получаем описание аватаров
- $info=array();
- $size=getimagesize($object_album_dir."/2/".$file,$info);
- $file=substr($file,0,-4);
- print_r($info);echo "<br/>";
- if(isset($info["APP13"])){
- $iptc = iptcparse ($info["APP13"]);
- $avatars_info[$file]=(string)$iptc["2#005"][0];
- $avatars_order[$file]=$iptc["2#010"][0];
- $avatars_private[$file]=$iptc["2#015"][0];
- }else {
- $avatars_info[$file]='';
- $avatars_order[$file]=100;
- $avatars_private[$file]=0;
- }
- }
-
- foreach ($image_types as $key=>&$value){
- if($key==1) continue;
- dir_remove($object_album_dir."/".$key,true);
- }unset($value);
-
-
- foreach($type6 as $image){
-
- set_time_limit(5);
- $img=imagecreatefromjpeg($object_album_dir.'/'.$image);
- if(!$img)$report[]="Не удалось открыть картинку ".$object_album_dir.'/'.$image;
-
- foreach($image_types as $key=>&$value)
- {
- if($key == 1)continue;
- if(!file_exists($object_album_dir.'/'.$key))mkdir($object_album_dir.'/'.$key,0777);
- set_time_limit(5);
-
- $new_image=&image_resize($img,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
- if($new_image) {
- if($value['type']=='png'){
- if(!@imagepng($new_image,$object_album_dir.'/'.$key.'/'.$image)){
- $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
- }
- }else {
- if(!@imagejpeg($new_image,$object_album_dir.'/'.$key.'/'.$image,85)){
- $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
- }
- }
- }
- if($key==3 || $key==4){
- $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
- $iptc_new.=iptc_make_tag(2,'010',$avatars_order[substr($image,0,-4)]);
- $iptc_new.=iptc_make_tag(2,'015',$avatars_private[substr($image,0,-4)]);
- $content = iptcembed($iptc_new, $object_album_dir.'/'.$key.'/'.$image, 0);
- $fp = @fopen($object_album_dir.'/'.$key.'/'.$image, "w");
- if($fp){
- fwrite($fp, $content);
- fclose($fp);
- }else $report[]='Не удалось установить IPTC теги для '.$object_album_dir.'/'.$key.'/'.$image;
- }
-
- }unset($value);
- }
- }
- }
- }*/
-
- ////////////////////////////////
- //фотоальбомы пользователей
- while (false !== ($object_dir = readdir($photos_res['users']))) {
-
- if ($object_dir==".." || $object_dir=="." || !is_dir($photos_dir['users'].$object_dir))continue;
- $object_album_res=opendir($photos_dir['users'].$object_dir);
-
- while (false !== ($object_album_dir = readdir($object_album_res))) {
- $count++;
- //if($count > 10)die();
- if ($object_album_dir==".." || $object_album_dir=="." || !is_dir($photos_dir['users'].$object_dir.'/'.$object_album_dir))continue;
- $object_album_dir=$photos_dir['users'].$object_dir.'/'.$object_album_dir;
- $photos_users_count++;
-
- if(!file_exists($object_album_dir."/2/")){$report[]="Отсутствует папка {$object_album_dir}/2/";continue;}
- $object_type6_res=opendir($object_album_dir.'/2/');
- $avatars_info=array();
- $avatars_order=array();
- $avatars_private=array();
- $type6=array();
- while (false !== ($file = readdir($object_type6_res)))
- {
- if ($file==".." || $file=="." || $file=="Thumbs.db" || !is_file($object_album_dir.'/'.$file))continue;
- $type6[]=$file;
-
- //получаем описание аватаров
- $info=array();
- $size=getimagesize($object_album_dir."/3/".$file,$info);
- $file=substr($file,0,-4);
- //print_r($info);echo "<br/>";
- //print_r(iptcparse ($info["APP13"])).' <br/>';
- if(isset($info["APP13"])){
- $iptc = iptcparse ($info["APP13"]);
- $avatars_info[$file]=(string)$iptc["2#005"][0];
- $avatars_order[$file]=$iptc["2#010"][0];
- $avatars_private[$file]=$iptc["2#015"][0];
- }else {
- $avatars_info[$file]='';
- $avatars_order[$file]=100;
- $avatars_private[$file]=0;
- }
- }
-
-
- foreach ($image_types as $key=>&$value){
- if($key==1) continue;
- dir_remove($object_album_dir."/".$key,true);
- }unset($value);
-
-
- foreach($type6 as $image){
-
- set_time_limit(5);
- $img=imagecreatefromjpeg($object_album_dir.'/'.$image);
- if(!$img)$report[]="Не удалось открыть картинку ".$object_album_dir.'/'.$image;
-
- foreach($image_types as $key=>&$value)
- {
- if($key == 1)continue;
-
- set_time_limit(5);
- if(!file_exists($object_album_dir.'/'.$key))mkdir($object_album_dir.'/'.$key,0777);
- if (App::isSmpz()) {
- $new_image=&image_resize($image,$value['x'],$value['y'],$value['crop'],$value['round_edges'],$value['type']);
- } else {
- $new_image=&image_resize($image,$value['x'],$value['y'],$value['crop'],false,$value['type']);
- }
- if($new_image) {
- if($value['type']=='png'){
- if(!@imagepng($new_image,$object_album_dir.'/'.$key.'/'.$image)){
- $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
- }
- }else {
- if(!@imagejpeg($new_image,$object_album_dir.'/'.$key.'/'.$image,85)){
- $report[]='Не удалось создать изображение '.$object_album_dir.'/'.$key.'/'.$image; continue;
- }
- }
- }
-
- if($key==3){
- $iptc_new=iptc_make_tag(2,'005',$avatars_info[substr($image,0,-4)]);
- $iptc_new.=iptc_make_tag(2,'010',$avatars_order[substr($image,0,-4)]);
- $iptc_new.=iptc_make_tag(2,'015',$avatars_private[substr($image,0,-4)]);
- $content = iptcembed($iptc_new, $object_album_dir.'/'.$key.'/'.$image, 0);
- $fp = @fopen($object_album_dir.'/'.$key.'/'.$image, "w");
- if($fp){
- fwrite($fp, $content);
- fclose($fp);
- }else $report[]='Не удалось установить IPTC теги для '.$object_album_dir.'/'.$key.'/'.$image;
- }
- }unset($value);
- }
- }
- }
-
- $report[]="Процедура окончена в ".date("h:i:s");
- $report[]="Затронуто объектов: {$objects_count}";
- $report[]="Затронуто пользователей: {$users_count}";
- $report[]="Затронуто фотоальбомов пользователей: {$photos_users_count}";
- $report[]="Затронуто фотоальбомов объектов: {$photos_objects_count}";
- return $report;
- }
- function imagecreatefrombmp($filename)
- {
- $tmp_name = tempnam(__MEDIA_STORAGE_PATH."/avatars/temp", "GD");
- if(bmp2gd($filename, $tmp_name)) {
- $img = imagecreatefromgd($tmp_name);
- unlink($tmp_name);
- return $img;
- }
- return false;
- }
- function bmp2gd($src, $dest = false)
- {
-
- if(!($src_f = fopen($src, "rb")))
- {
- return false;
- }
-
-
- if(!($dest_f = fopen($dest, "wb")))
- {
- return false;
- }
-
-
- $header = unpack("vtype/Vsize/v2reserved/Voffset", fread( $src_f, 14));
-
-
- $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",
- fread($src_f, 40));
-
-
- extract($info);
- extract($header);
-
-
- if($type != 0x4D42)
- {
- return false;
- }
-
-
- $palette_size = $offset - 54;
- $ncolor = $palette_size / 4;
- $gd_header = "";
-
-
- $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
- $gd_header .= pack("n2", $width, $height);
- $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
- if($palette_size) {
- $gd_header .= pack("n", $ncolor);
- }
-
- $gd_header .= "\xFF\xFF\xFF\xFF";
-
-
- fwrite($dest_f, $gd_header);
-
-
- if($palette_size)
- {
-
- $palette = fread($src_f, $palette_size);
-
- $gd_palette = "";
- $j = 0;
-
- while($j < $palette_size)
- {
- $b = $palette{$j++};
- $g = $palette{$j++};
- $r = $palette{$j++};
- $a = $palette{$j++};
-
- $gd_palette .= "$r$g$b$a";
- }
-
- $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
-
- fwrite($dest_f, $gd_palette);
- }
-
-
- $scan_line_size = (($bits * $width) + 7) >> 3;
- $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
-
-
- for($i = 0, $l = $height - 1; $i < $height; $i++, $l--)
- {
-
- fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
- $scan_line = fread($src_f, $scan_line_size);
- if($bits == 24)
- {
- $gd_scan_line = "";
- $j = 0;
- while($j < $scan_line_size)
- {
- $b = $scan_line{$j++};
- $g = $scan_line{$j++};
- $r = $scan_line{$j++};
- $gd_scan_line .= "\x00$r$g$b";
- }
- }
- elseif($bits == 8)
- {
- $gd_scan_line = $scan_line;
- }
- elseif($bits == 4)
- {
- $gd_scan_line = "";
- $j = 0;
- while($j < $scan_line_size)
- {
- $byte = ord($scan_line{$j++});
- $p1 = chr($byte >> 4);
- $p2 = chr($byte & 0x0F);
- $gd_scan_line .= "$p1$p2";
- }
- $gd_scan_line = substr($gd_scan_line, 0, $width);
- }
- elseif($bits == 1)
- {
- $gd_scan_line = "";
- $j = 0;
- while($j < $scan_line_size)
- {
- $byte = ord($scan_line{$j++});
- $p1 = chr((int) (($byte & 0x80) != 0));
- $p2 = chr((int) (($byte & 0x40) != 0));
- $p3 = chr((int) (($byte & 0x20) != 0));
- $p4 = chr((int) (($byte & 0x10) != 0));
- $p5 = chr((int) (($byte & 0x08) != 0));
- $p6 = chr((int) (($byte & 0x04) != 0));
- $p7 = chr((int) (($byte & 0x02) != 0));
- $p8 = chr((int) (($byte & 0x01) != 0));
- $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
- }
-
- $gd_scan_line = substr($gd_scan_line, 0, $width);
- }
-
- fwrite($dest_f, $gd_scan_line);
- }
-
- fclose($src_f);
-
- fclose($dest_f);
-
- return true;
- }
- /* Создание watermarka (водяной знак) */
- function add_watermark($simple_image,$water_image,$result_image)
- {
- $size_water = getimagesize($water_image);
- $size_image = getimagesize($simple_image);
- // свойство водяного знака
- $water['width'] = $size_water['0'];
- $water['height'] = $size_water['1'];
- $water['padding'] = 17; // отступ водяного знака
-
- if( $size_image['0'] < ($water['width'] * 2.5) || $size_image['1'] < ($water['height'] * 2.5) ) {
- return;
- }
- // Проверка MIME
- if( $size_water['mime'] != 'image/png' ) {
- return;
- }
- $im_water = imagecreatefrompng($water_image);
- imagealphablending($im_water, false);
- imagesavealpha($im_water, true);
- $im_simple = imagecreatefromjpeg($simple_image);
- $final_x = $size_image['0'] - $water['width'] - $water['padding'];
- $final_y = $size_image['1'] - $water['height'] - $water['padding'];
-
- // Добавление водяного знака в изображение
- imagecopy($im_simple, $im_water, $final_x, $final_y, 0, 0, $water['width'], $water['height']);
- // Сохранение итоговой картинки
- imagejpeg($im_simple, $result_image, 100);
-
- imagedestroy($im_water);
- imagedestroy($im_simple);
- }
- /* Вырезание участка изображения (crop) */
- function image_crop($image_source, $x1, $y1, $w, $h)
- {
- $image = $image_source;
- if( preg_match('|^/avatars/users/.*|', $image) && !preg_match('|^/avatars/objects/.*|', $image) )
- {
- $uid = preg_replace('|.*/([^_]+)_.*|','$1',$image);
- $key = preg_replace('|.*/[^_]+_(\d+).*|','$1',$image);
- $type = preg_replace('|.*/[^_]+_\d+_(\d+).*|','$1',$image);
- $file = '1.jpg';
- $ext = preg_replace('|.*\.(.*)|','$1',$file);;
- $path_start = '/avatars/users/'.$uid;
- $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$type.'/'.$file;
- }
- else if( preg_match('|^/avatars/\d-.*|', $image) || preg_match('|^/avatars/[\d\w]+.*|', $image) ) // при загрузке картинок
- {
- $image = preg_replace('/(.*)\?.*$/','$1',$image);
- $type = preg_replace('/.*avatars\/(temp|objects)\/([^-]+)-.*/','$2',$image);
- $pid = preg_replace('|.*avatars/[^-]+-([\d]+).*|','$1',$image);
- $oid = preg_replace('|.*avatars/([^/]+).*|','$1',$image);
- $key = preg_replace('/.*[\d]+_([\d]+)(\..*)/','$1', $image);
- $file = preg_replace('/.*([\d]+)_[\d]+(\..*)/','$1$2', $image);
- $ext = preg_replace('|.*\.(.*)|','$1',$file);
- if( preg_match('|^/avatars/objects/.*|', $image) ) {
- $path_start = '/avatars/objects/'.$type.'/'.$pid;
- } else {
- $path_start = '/avatars/temp/'.$oid;
- }
- $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$key.'/'.$file;
- }
- else if( preg_match('|^/avatars/objects/.*|', $image) )
- {
- $image = preg_replace('/(.*)\?.*$/','$1',$image);
- $type = preg_replace('|.*objects/([^-]+)-.*|','$1',$image);
- $pid = preg_replace('|.*objects/[^-]+-([^_]+)_.*|','$1',$image);
- $key = preg_replace('/.*[\d]+_([\d]+)(\..*)/','$1', $image);
- $file = preg_replace('/.*([\d]+)_[\d]+(\..*)/','$1$2', $image);
- $ext = preg_replace('|.*\.(.*)|','$1',$file);
- $path_start = '/avatars/objects';
- $path_full = __MEDIA_STORAGE_PATH.$path_start.'/'.$type.'/'.$pid.'/'.$key.'/'.$file;
- }
-
- if( $ext == 'jpg' )
- {
- // начало создания миниатюры из большой картинки (10)
- $im = imagecreatefromjpeg( $path_full );
-
- $im_size = getimagesize( $path_full );
- $sto_percent_x1 = $im_size['0'];
- $sto_percent_y1 = $im_size['1'];
- if($sto_percent_x1 > $sto_percent_y1){
- $koefW = 400;
- $koefH = round(($koefW/$sto_percent_x1)*$sto_percent_y1);
- }
- else{
- $koefH = 400;
- $koefW = round(($koefH/$sto_percent_y1)*$sto_percent_x1);
- }
- $precents_preview_x1 = $x1 / $koefW;
- $precents_preview_y1 = $y1 / $koefH;
- $precents_preview_w = $sto_percent_x1 * ($w / $koefW);
- $precents_preview_h = $sto_percent_y1 * ($h / $koefH);
-
-
- $x1_fimage = round( $sto_percent_x1 * $precents_preview_x1 ); // координата x1
- $y1_fimage = round( $sto_percent_y1 * $precents_preview_y1 ); // координата y1
- // создание миниатюры
- $dest = imagecreatetruecolor($precents_preview_w, $precents_preview_h);
- imagecopy($dest, $im, 0, 0, $x1_fimage, $y1_fimage, $precents_preview_w, $precents_preview_h);
- include_once(__ENGINE_PATH.'/functions/subfunctions/common.arrays.php');
-
- global $avatar_types;
- global $image_types;
- $i = 0;
- foreach( $avatar_types as $key => $avatar )
- {
- if( !$avatar['crop'] ) {
- $i++;
- continue;
- }
- $path_save = __MEDIA_STORAGE_PATH.$path_start.'/';
- $path_save .= $key.'/'.$file;
- if (App::isSmpz()) {
- $im_final = image_resize($dest, $avatar['x'], $avatar['y']);
- } else {
- $im_final = image_resize($dest, $avatar['x'], $avatar['y'], false, false);
- }
- if( !imagejpeg($im_final, $path_save, 100) )
- {
- return 0;
- }
- $i++;
- }
-
- imagedestroy($dest);
- imagedestroy($im);
- imagedestroy($im_final);
- return 1;
- }
- }