Create overlay pattern png file using repeat-x repeat-y method

161 views Asked by At

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?

1

There are 1 answers

5
splash58 On

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;
    }