note 69865 added to function.imagefill
| From: | razonklnbd at yahoo dot com | Date: | Sun, 24 Sep 2006 10:32:15 +0000 |
| Subject: | note 69865 added to function.imagefill | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117490@lists.php.net to get a copy of this message | ||
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;
}
// If you want to use a gif or png file as
// pattern file you need to change function below :)
$pattern_im=imagecreatefromjpeg('logo.jpg');
// If you want to use a gif or png file as
// main file you need to change function below :)
$main_im=imagecreatefromjpeg('r2.jpg');
// call the function width 500 and height 500
// if your width and height is less then main images
// width and height then you can't understand any change!
$final=fill_with_patternfile($main_im, $pattern_im, 500, 500);
// view the image and destroy all instance
header('Content-type: image/jpeg');
imagejpeg($final);
imagedestroy($final);
imagedestroy($main_im);
imagedestroy($pattern_im);
exit();
?>
----
Server IP: 66.163.161.117
Probable Submitter: 202.59.136.8
----
Manual Page -- http://www.php.net/manual/en/function.imagefill.php
Edit -- https://master.php.net/note/edit/69865
Del: integrated -- https://master.php.net/note/delete/69865/integrated
Del: useless -- https://master.php.net/note/delete/69865/useless
Del: bad code -- https://master.php.net/note/delete/69865/bad+code
Del: spam -- https://master.php.net/note/delete/69865/spam
Del: non-english -- https://master.php.net/note/delete/69865/non-english
Del: in docs -- https://master.php.net/note/delete/69865/in+docs
Del: other reasons-- https://master.php.net/note/delete/69865
Reject -- https://master.php.net/note/reject/69865
Search -- https://master.php.net/manage/user-notes.php