N
N
newaitix2018-02-09 12:26:38
PHP
newaitix, 2018-02-09 12:26:38

How to wrap repetitive code in a function?

$prc=3899;
$prefix1='88Qn|Q*wcIa5';
$prefix2='66{Rvi#wy?r?#1';
$width=200;
$typ='grp';
$cur='uk';
$rrc=3906;
$hdt=2;

$code=urlencode(base64_encode(json_encode([
  'encode'	=>	$prefix1.$prc.$prefix2,
  'width'		=>	$width,
  'typ'		=>	$typ,
  'cur'		=>	$cur,
  'rrc'		=>	$rrc,
  'hdt'		=>	$hdt
])));

echo"<img src='text_to_img.php?w=$code'>";

text_to_img.php
<?php
$encod_array=$_GET['w'];
$decode_array=json_decode(base64_decode(urldecode($encod_array)));

$width=$decode_array->{'width'};
$encode=$decode_array->{'encode'};
$type=$decode_array->{'typ'};
$cur=$decode_array->{'cur'};
$rrc=$decode_array->{'rrc'};
$height=$decode_array->{'hdt'};

$prefix1='88Qn|Q*wcIa5';
$prefix2='66{Rvi#wy?r?#1';
$font_file='arial.ttf';
$lan=array(
    'uk'=>' грн.',
    'ru'=>' грн.',
    'en'=>' hrn.'
  );

if(
  isset($width)&&!empty($width)&&
  isset($encode)&&!empty($encode)&&
  isset($type)&&!empty($type)&&
  isset($cur)&&!empty($cur)&&
  isset($height)&&!empty($height)
){
  $p1_test=strripos($encode,$prefix1);
  $p2_test=strripos($encode,$prefix2);

  if($p1_test===false||$p2_test===false)exit;
  if(gettype($lan[$cur])==NULL)exit;
  if($type!='grp'&&$type!='txt')exit;
  if($width==0)exit;

  $p1_split=str_replace($prefix1,"",$encode);
  $p2_split=str_replace($prefix2,"",$p1_split);

  $price_smash=strrev(implode(' ',str_split(strrev($p2_split),3)));
  $rrc_smash=strrev(implode(' ',str_split(strrev($rrc),3)));

  $price_ready=$price_smash.$lan[$cur];
  $rrc_ready=$rrc_smash.$lan[$cur];

  $price_ln=strlen($p2_split);

function test(){
  $w_1_symbol=13.9;
  $image=imagecreatetruecolor($width,26);
  $background=imagecolorallocate($image,255,255,255);
  $color=imagecolorallocate($image,57,181,74);
  imagefilledrectangle($image,0,0,299,99,$background);
  imagefttext($image,17,0,($width-$price_ln*$w_1_symbol-42)/2,21,$color,$font_file,$price_ready);
  return $image;
}

  if($height==2){
    if($type=='grp'){
      $image=test();
    }
    if($type=='txt'){
      $w_1_symbol=7;
      $image=imagecreatetruecolor($width,15);
      $background=imagecolorallocate($image,255,255,255);
      $color=imagecolorallocate($image,0,0,0);
      imagefilledrectangle($image,0,0,299,99,$background);
      imagefttext($image,9,0,($width-$price_ln*$w_1_symbol-27)/2,12,$color,$font_file,$price_ready);
    }
  }
  if($height==3){
    if($type=='grp'){
      $w_1_symbol=13.9;
      $image=imagecreatetruecolor($width,47);
      $background=imagecolorallocate($image,255,255,255);
      $color=imagecolorallocate($image,57,181,74);
      imagefilledrectangle($image,0,0,299,99,$background);
      imagefttext($image,17,0,($width-$price_ln*$w_1_symbol-42)/2,40,$color,$font_file,$price_ready);
    }
  }
  if($height==4&&isset($rrc)&&!empty($rrc)){
    if($type=='grp'){
      $w_1_symbol=13.9;
      $image=imagecreatetruecolor($width,47);
      $background=imagecolorallocate($image,255,255,255);
      $color=imagecolorallocate($image,57,181,74);
      imagefilledrectangle($image,0,0,299,99,$background);
      imagefttext($image,17,0,($width-$price_ln*$w_1_symbol-42)/2,40,$color,$font_file,$price_ready);
      if($rrc){
        $w_1_symbol=7;
        $color=imagecolorallocate($image,153,0,0);
        imagefttext($image,10,0,($width-$price_ln*$w_1_symbol-27)/2,14,$color,$font_file,$rrc_ready);
        $rtrgtfre='_';
        $gerfkerng='';
        for($i=0;(strlen($rrc)*7+27)/7>$i;$i++)
          $gerfkerng=$gerfkerng.$rtrgtfre;
        $gerfkerng=$gerfkerng.$rtrgtfre;
        imagefttext($image,9,0,($width-$price_ln*$w_1_symbol-27)/2-3,8,$color,$font_file,$gerfkerng);
      }
    }
  }
  header('Content-Type:image/png');
  imagepng($image);
  imagedestroy($image);
}
?>

$image=test(); is called
Why does not it work ? How to write correctly so as not to produce repetitive code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DieZz, 2018-02-09
@newaitix

The problem is in scope. Check out php.net/manual/en/language.variables.scope.php
Simplified, your function doesn't see variables $width, $image, etc. These variables must be passed as function arguments.

function someFunction($width)
{
    return ++$width;
}

or expand scope to global
function someFunction()
{
    global $width;
    // do something with $width
}

which is basically not very good. The option with function arguments is preferable, but in your case there will be a lot of them, which is also not good

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question