<?php

/**
 * @author bleakas
 * @copyright 2012
 */
 $textl='Создание демотиватора';
 define('_IN_JOHNCMS', 1); 
require('../incfiles/core.php');

 function err($err,$tf) {
   
    echo functions::display_error($err);
    require('../incfiles/end.php'); exit();
    
 }
 if (isset($_POST['submit'])) {
 $lines=ceil(intval($_COOKIE['lines']));
 $spaceln=ceil(intval($_COOKIE['spln']));
 $px=ceil(intval($_COOKIE['px']));
 $mas=array('times','tahoma','arial','verdana');
 $url=trim($_POST['url']);
$font3=trim($_POST['font']);
$font4=trim($_POST['font1']);
 $str=trim($_POST['str']);
 $str1=trim($_POST['str1']);
 if (strlen($str)>100 or strlen($str1)>100 or !in_array($font3,$mas) or  !in_array($font4,$mas)) {
    require('../incfiles/head.php');
    err('Неверная величина текста!!',1);
 }
 $font3=$font3.'.ttf';
 $font4=$font4.'.ttf';
 
if (!isset($_POST['str']) or empty($_POST['str'])) $st=0;
 if (!isset($_POST['str1']) or empty($_POST['str1'])) $st1=0;

 function is_url($url) {
  return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
  }
  if ($url!=='-') {
    if (!is_url($url)) {
        require('../incfiles/head.php');
        err('Неверный URL!!!!');}
    file_put_contents('i.tmp',file_get_contents($url));
    
  }
  else {
    if (!isset($_FILES['fail']) or empty($_FILES['fail']['tmp_name']) or !isset($_FILES['fail']['tmp_name']) or $_FILES['fail']['size']<3 or $_FILES['fail']['size']>= 1500000) {
        require('../incfiles/head.php');
        err('Плохое изображение!!!!',1);
    }
     if ((move_uploaded_file($_FILES["fail"]["tmp_name"], "i.tmp")) == true) {
        
        }
        else {
            require('../incfiles/head.php');
            err('your loh',1);
  }}
  if (filesize('i.tmp')>= 1500000) {
    require('../incfiles/head.php');
    unlink('i.tmp');
    err('Большой размер картинки',1);
  }
  $arrr=exif_imagetype('i.tmp');
            if (!$arrr) {
                require('../incfiles/head.php');
                unlink('i.tmp');
                err('bad image!',1);
                
            }
            switch ($arrr) {
                case 1: $formfail='gif'; break;
                case 2: $formfail='jpeg'; break;
                break;
                case 3: $formfail='png'; break;
                default: 
                require('../incfiles/head.php'); unlink('i.tmp'); err('Неверный формат!!!');
            }
            $ttr='$img=imagecreatefrom'.$formfail.'("i.tmp");';
            eval($ttr);
            if (!$img) {
                unlink('i.tmp');
                require('../incfiles/head.php');
                err('invalid img');
            }
            unlink('i.tmp');

$size=43;


$size2=16;

$sx=imagesx($img);
$sy=imagesy($img);
$cx=$sx+142;
$cy=$sy+191;
$img1=imagecreatetruecolor($cx,$cy);
imagecopy($img1,$img,73,43,0,0,$sx,$sy);
$bl=imagecolorallocate($img1,0,0,0);
$wh=imagecolorallocate($img1,255,255,255);
imagerectangle($img1,66,36,$sx+79,$sy+49,$wh);
imagerectangle($img1,67,37,$sx+78,$sy+48,$wh);
imagerectangle($img1,68,38,$sx+77,$sy+47,$wh);
$box=imagettfbbox($size,0,$font3,$str);
$box1=imagettfbbox($size2,0,$font4,$str1);
 $wt1 = $box[2]-$box[0];
 $ht1 = $box[1]-$box[7];
 $wt12 = $box1[2]-$box1[0];
 $ht12 = $box1[1]-$box1[7];
imagettftext($img1,$size,0, ($cx- $wt1) / 2,$ht1+$cy-130,$wh,$font3,$str);
imagettftext($img1,$size2,0, ($cx- $wt12) / 2,$ht12+$cy-60,$wh,$font4,$str1);

imagettftext($img1,10,0,13,$cy-6,$wh,'arial.ttf',$_SERVER['HTTP_HOST']);
header('Content-Type: image/png');
imagepng($img1);
imagedestroy($img);
imagedestroy($img1);
}
else {
    $textl='Создание демотиватора';
    require('../incfiles/head.php');
    echo "<form action='' method='post' enctype='multipart/form-data'>
        
            
        Заголовок:
        <br />
        <input type='text' name='str' size='10' maxlength='100'/> <br />
        Описание:<br />
         <input type='text' name='str1' size='10' maxlength='100'/>
        <br />Шрифт заголовка<br />
        <select name='font'><option value='times' >times</option><option value='arial'>arial</option>
        <option value='tahoma'>tahoma</option><option value='verdana'>verdana</option></select>
        <br />Шрифт описания<br />
        <select name='font1'><option value='arial' >arial</option><option value='times'>times</option>
        <option value='tahoma'>tahoma</option><option value='verdana'>verdana</option></select>
        <br />
        
       
        <br />Фото: <br />
        
            
            URL адресс<br />
            
            
            <input type='text' name='url' value='-' size='10' maxlength='100'/><br />
            <input type='file' name='fail' /><br /><input type='submit' name='submit' value='CREATE'/>
            </form>
            <br/>";
            require('../incfiles/end.php');
    
    
    
    
    
}

?>