<?
require ( $_SERVER [ 'DOCUMENT_ROOT' ]. '/bitrix/modules/main/include/prolog_before.php' );
/**
* При получении массива, описывающего изображение
*/
AddEventHandler( 'main' , 'OnMakeFileArray' , 'MyOnMakeFileArray' );
function MyOnMakeFileArray ( $URL , & $temp_path ) {
if (stripos( $URL , 'http://' )=== 0 || stripos( $URL , 'https://' )=== 0 ) {
$arContext = array (
'http' => array (
'method' => 'GET' ,
'timeout' => '10' ,
'ignore_errors' => false ,
)
);
$Image = file_get_contents( $URL , false , stream_context_create( $arContext ));
$arUrl = parse_url( $URL );
$arPath = pathinfo( $arUrl [ 'path' ]);
mkdir( $_SERVER [ 'DOCUMENT_ROOT' ]. '/upload/http_image_tmp/' ,BX_DIR_PERMISSIONS, true );
$temp_path = $_SERVER [ 'DOCUMENT_ROOT' ]. '/upload/http_image_tmp/' . $arPath [ 'basename' ];
file_put_contents( $temp_path , $Image );
$Size = 0 ;
$Type = '' ;
foreach ( $http_response_header as $strHeader ){
if (preg_match( '#^Content-Length: (\d+)$#i' , $strHeader , $M )) {
$Size = IntVal( $M [ 1 ]);
} if (preg_match( '#^Content-Type: (.*?)$#i' , $strHeader , $M )) {
$Type = $M [ 1 ];
}
}
$arImageSize = getimagesize( $temp_path );
$GLOBALS [ 'http_image_tmp' ] = array (
$temp_path => array (
'URL' => $URL ,
'NAME' => $arPath [ 'basename' ],
'SIZE' => $Size ,
'TYPE' => $Type ,
'WIDTH' => IntVal( $arImageSize [ 0 ]),
'HEIGHT' => IntVal( $arImageSize [ 1 ]),
),
);
return true ;
}
return false ;
}
/**
* При сохранении файла в b_file
*/
AddEventHandler( 'main' , 'OnFileSave' , 'MyOnFileSave' );
function MyOnFileSave (& $arFile , $strFileName , $strSavePath , $bForceMD5 , $bSkipExt , $dirAdd ) {
if ( isset ( $GLOBALS [ 'http_image_tmp' ][ $arFile [ 'tmp_name' ]])) {
$arFileTmp = $GLOBALS [ 'http_image_tmp' ][ $arFile [ 'tmp_name' ]];
$arFile [ 'FILE_NAME' ] = $arFileTmp [ 'URL' ];
$arFile [ 'ORIGINAL_NAME' ] = $arFileTmp [ 'NAME' ];
$arFile [ 'size' ] = $arFileTmp [ 'SIZE' ];
$arFile [ 'type' ] = $arFileTmp [ 'TYPE' ];
$arFile [ 'WIDTH' ] = $arFileTmp [ 'WIDTH' ];
$arFile [ 'HEIGHT' ] = $arFileTmp [ 'HEIGHT' ];
@unlink( $arFile [ 'tmp_name' ]);
return true ;
}
}
/**
* При получении пути для вывода изображения
*/
AddEventHandler( 'main' , 'OnGetFileSRC' , 'MyOnGetFileSRC' );
function MyOnGetFileSRC ( $arFile ) {
if (stripos( $arFile [ 'FILE_NAME' ], 'http://' )=== 0 || stripos( $arFile [ 'FILE_NAME' ], 'https://' )=== 0 ) {
return $arFile [ 'FILE_NAME' ];
}
}
$arNewFile = CFile::MakeFileArray( 'http://www.webdebug.ru/upload/iblock/ec3/appscreen.png' );
$intNewFile = CFile::SaveFile( $arNewFile );
print CFile::ShowImage( $intNewFile );
?>
|