0

I want to create 600px png overlay pattern using 4px png. This is only repeating x axis.

$srcfile = '4px.png';
$outfile = 'overlay.png';
list($src_w,$src_h,$src_type) = getimagesize($srcfile);

$out_w = 600;
$out_h = 600;

$src = imagecreatefrompng($srcfile);
$out = imagecreatetruecolor($out_w, $out_h);

$curr_x = 0;
while($curr_x < $out_w){
    $curr_y = 0;
    while($curr_y < $out_h){
       imagecopy($out, $src, $curr_y, 0, 0, 0, $src_w, $src_h);
       $curr_y += $src_h;
      }
    $curr_x += $src_w;
}

imagepng($out, $outfile, 100);
imagedestroy($src);
imagedestroy($out);

Working x-repeat as follows

$curr_x = 0;
while($curr_x < $out_w){
    imagecopy($out, $src, $curr_x, 0, 0, 0, $src_w, $src_h);
    $curr_x += $src_w;
}

How Can I Y-repat the above code?

troshan
  • 134
  • 1
  • 11

1 Answers1

0

I think, you should use two loops - for x and y separately

$curr_x = 0;
while($curr_x < $out_w) {
    $curr_y = 0;
    while($curr_y < $out_h){
       imagecopy($out, $src, $curr_x, $curr_y, 0, 0, $src_w, $src_h);
       $curr_y += $src_h;
      }
    $curr_x += $src_w;
    }
splash58
  • 26,043
  • 3
  • 22
  • 34
  • Thank you, since the comment section doesn't support code, I have edited my question with your solution. I'm stuck with repeating through y-axis. – troshan Nov 19 '17 at 14:43
  • I've executed the code from my answer on php 5.6, it produces png with success. This is full code - https://eval.in/903133 – splash58 Nov 19 '17 at 19:45
  • Why it fills with black background rather transparent? – troshan Nov 20 '17 at 04:48
  • *imagecreatetruecolor() returns an image identifier representing a black image of the specified size.* - http://php.net/manual/en/function.imagecreatetruecolor.php – splash58 Nov 20 '17 at 11:57
  • Add `imagecolortransparent($out, imagecolorallocate($out, 0, 0, 0));` after creaing image to set backgrownd transparent – splash58 Nov 20 '17 at 12:25