I spend more then two hour to find a function that can fill a pattern or file as background instead of color. but i can't find. so i develop the following function. i though this function will save time who need it...
Function will get four parameter
1. Main Image Identifier
2. Pattern Image Identifier
3. Final Image Width
4. Final Image Height
If you set final image width or height is less then main image width or height then you may get wrong result
<?php
function fill_with_patternfile($p_main_im, $p_patternfile_im, $p_width, $p_height){
$pimiX=$p_patternfile_im;
$pw=imagesx($pimiX);
$ph=imagesy($pimiX);
$targetImageIdentifier=imagecreatetruecolor($p_width,$p_height);
if($pw<$p_width && $ph<$p_height){
for($pX=0;$pX<$p_width;$pX+=$pw){
for($pY=0;$pY<$p_height;$pY+=$ph){
imagecopy($targetImageIdentifier,$pimiX,$pX,$pY,0,0,$pw,$ph);
}
}
}else imagecopy($targetImageIdentifier,$pimiX,0,0,0,0,$pw,$ph);
$w=imagesx($p_main_im);
$h=imagesy($p_main_im);
$nX=0;
if($w<$p_width) $nX=intval(($p_width-$w)/2);
$nY=0;
if($h<$p_height) $nY=intval(($p_height-$h)/2);
imagecopy($targetImageIdentifier,$p_main_im,$nX,$nY,0,0,$w,$h);
return $targetImageIdentifier;
}
$pattern_im=imagecreatefromjpeg('logo.jpg');
$main_im=imagecreatefromjpeg('r2.jpg');
$final=fill_with_patternfile($main_im, $pattern_im, 500, 500);
header('Content-type: image/jpeg');
imagejpeg($final);
imagedestroy($final);
imagedestroy($main_im);
imagedestroy($pattern_im);
exit();
?>