<?
  ##########################################
## Автор: -=ШАХТЕР=-                        #                                                                                                                                                                                      ## Сайт: http://masteram.us                 #                 															                      ## ICQ  :  46-92-90                         #                 														 		           ## Версия 4.0 Лицензионная                  #
## Данная версия скрипта является ПЛАТНОЙ,  #
## вы НЕ ИМЕЕТЕ ПРАВА распрострянять данный #
##  скрипт или какие-либо части его кода... #
  #########################################
require'../config.php';


header('Content-type: image/jpeg');

$W = check(intval($_GET['W']));
$H = check(intval($_GET['H']));
$file = check(intval($_GET['file']));

$file_info = mysql_fetch_row($db->sql_query('SELECT TRIM(`path`) FROM `fo_files` WHERE `id` = '.$file.' LIMIT 1'));
$theme = urldecode(htmlspecialchars($file_info[0]));
$type = $file_info[1];



if(!$W and !$H)
{
$size = explode('*',$set['prev_size']);
$W = round(intval($size[0])); // ширина картинки
$H = round(intval($size[1])); // высота картинки
}

if($W AND $H)
{
$g_preview_image_w = $H;
$g_preview_image_h = $H;
}
else
{
$g_preview_image_w = 100; // ширина картинки
$g_preview_image_h = 100; // высота картинки
}

if($type == '.nth'){
include 'pclzip.lib.php';

$nth = &new PclZip($theme);
$content = $nth->extract(PCLZIP_OPT_BY_NAME,'theme_descriptor.xml',PCLZIP_OPT_EXTRACT_AS_STRING);

$teg = simplexml_load_string($content[0]['content'])->wallpaper['src'] or $teg = simplexml_load_string($content[0]['content'])->wallpaper['main_display_graphics'];
$image = $nth->extract(PCLZIP_OPT_BY_NAME, trim($teg), PCLZIP_OPT_EXTRACT_AS_STRING);

$im = array_reverse(explode('.',$teg));
$im = 'imageCreateFrom'.str_ireplace('jpg','jpeg',trim($im[0]));

file_put_contents($name,$image[0]['content']);
$f = $im($name);

$h = imagesy($f);
$w = imagesx($f);

$ratio = $w/$h;
if($g_preview_image_w/$g_preview_image_h > $ratio){
$g_preview_image_w = $g_preview_image_h*$ratio;
}
else{
$g_preview_image_h = $g_preview_image_w/$ratio;
}


$new = imagecreatetruecolor($g_preview_image_w, $g_preview_image_h);
imagecopyresampled($new, $f, 0, 0, 0, 0, $g_preview_image_w, $g_preview_image_h, $w, $h);

imageGif($new, $name, 100);
}
elseif($type == '.thm'){
include 'tar.php';

$thm = &new Archive_Tar($theme);
$deskside_file = $thm->extractInString('Theme.xml');
$load = simplexml_load_string($deskside_file)->Standby_image['Source'] or $load = simplexml_load_string($deskside_file)->Desktop_image['Source'] or $load = simplexml_load_string($deskside_file)->Desktop_image['Source'];
$image = $thm->extractInString(trim($load));

$im = array_reverse(explode('.',$load));
$im = 'imageCreateFrom'.str_ireplace('jpg','jpeg',trim($im[0]));

file_put_contents($name,$image);
$f = $im($name);

$h = imagesy($f);
$w = imagesx($f);

$ratio = $w/$h;
if($g_preview_image_w/$g_preview_image_h > $ratio){
$g_preview_image_w = $g_preview_image_h*$ratio;
}
else{
$g_preview_image_h = $g_preview_image_w/$ratio;
}


$new = imagecreatetruecolor($g_preview_image_w, $g_preview_image_h);
imagecopyresampled($new, $f, 0, 0, 0, 0, $g_preview_image_w, $g_preview_image_h, $w, $h);

imageGif($new, $name, 100);
}


header('Location: '.$name, true, 301);
?>