Obsah článku
Možná vás už někdy napadlo dát si do svých obrázků podpis nebo vodoznak, ale nechtělo se vám, protože bylo obrázků prostě příliš a bylo by to moc práce. Tak nějak jsem uvažoval i já a tak jsem zasedl a chvilku vyťukával na klávesnici funkci jménem „image_join();“, která tuto práci dělá za mne.
<?php
/*
+----------------------------------------------------------------------+
| Image_join is free to use PHP script. You can support development |
| via PayPal or check updates on page http://blog.petrknap.cz/c66.html |
+----------------------------------------------------------------------+
/*-------------------------------------------------------------------------------------------------
About
* updated: 2012-02-20
* author: Petr Knap
* e-mail: kontakt@petrknap.cz
* homepage: http://petrknap.cz/
How does it work?
The script inserts the image $front into the image $back
and sends the final image to output.
Inputs
* back - path to background image
* front - path to foreground image
* position - position foreground image on background image (switch)
tl - top left
tc - top center
tr - top right
cc - center center
bl - bottom left
bc - bottom center
br - bottom right (default)
* target - output (default null)
* $TCimage - true color background image in associative array (default null)
image => true color image (as resource)
width => width of this image (as int)
height => height of this image (as int)
If $target is not null the output is file defined in $target.
REMEMBER to use readfile($target) after call image_join for
send saved file to output.
Example
<img src="img.php?file=desktop.jpg" alt="My desktop" />
Where file img.php contains:
<?php
require_once('./image_join.php');
$sign = './data/signature.png';
$e404 = './data/e404.png';
if(!file_exists($_GET['file']))
{
header("Content-Type: image/png");
readfile($e404);
}
else image_join($_GET['file'], $sign);
?>
Bugs & Errors
The script has no known bugs.
Have you found a bug? Report it via e-mail!
-------------------------------------------------------------------------------------------------*/
function image_join($back, $front, $position = "br", $target = null, $TCimage = null)
{
//-------------------------------------------------------------------------------------------------
// background image
if(is_array($TCimage)) // true color image
{
$b_w = $TCimage['width'];
$b_h = $TCimage['height'];
}
else // from file
{
$b_i = getimagesize($back);
$b_w = $b_i[0];
$b_h = $b_i[1];
$b_t = $b_i[2];
}
//-------------------------------------------------------------------------------------------------
// foreground image
$f_i = getimagesize($front);
$f_w = $f_i[0];
$f_h = $f_i[1];
$f_t = $f_i[2];
//-------------------------------------------------------------------------------------------------
// new image
$new_image = imagecreatetruecolor($b_w, $b_h);
$background = imagecolorallocate($new_image,229,229,229);
imagecolortransparent($new_image, $background);
imagefilledrectangle($new_image,0,0,$b_w,$b_h,$background);
//-------------------------------------------------------------------------------------------------
// loading background image
if(is_array($TCimage)) // true color image
$image =& $TCimage['image'];
else switch ( $b_t ) // from file
{
case '1':
$image = imagecreatefromgif($back); //GIF
break;
case '2':
$image = imagecreatefromjpeg($back); //JPeG
break;
default:
$image = imagecreatefrompng($back); // PNG
break;
}
// copy $image to $new_image
imagecopy($new_image, $image, 0, 0, 0, 0, $b_w, $b_h);
//-------------------------------------------------------------------------------------------------
// loading foreground image
switch ( $f_t )
{
case '1':
$image = imagecreatefromgif($front); //GIF
break;
case '2':
$image = imagecreatefromjpeg($front); //JPeG
break;
default:
$image = imagecreatefrompng($front); // PNG
break;
}
// positions
switch ( $position )
{
case "tl":
$f_x = 0; // left
$f_y = 0; // top
break;
case "tc":
$f_x = $b_w/2 - $f_w/2; // center
$f_y = 0; // top
break;
case "tr":
$f_x = $b_w - $f_w; // right
$f_y = 0; // top
break;
case "cc":
$f_x = $b_w/2 - $f_w/2; // center
$f_y = $b_h/2 - $f_h/2; // center
break;
case "bl":
$f_x = 0; // left
$f_y = $b_h - $f_h; // bottom
break;
case "bc":
$f_x = $b_w/2 - $f_w/2; // center
$f_y = $b_h - $f_h; // bottom
break;
default: // br
$f_x = $b_w - $f_w; // right
$f_y = $b_h - $f_h; // bottom
break;
}
// copy $image to $new_image
imagecopy($new_image, $image, $f_x, $f_y, 0, 0, $f_w, $f_h);
//-------------------------------------------------------------------------------------------------
// output
switch ( $b_t )
{
case '1': // GIF
if($target == null)
{
header("Content-Type: image/gif");
imagegif($new_image);
}
else imagegif($new_image, $target);
break;
case '3': // PNG
if($target == null)
{
header("Content-Type: image/png");
imagepng($new_image);
}
else imagepng($new_image, $target);
break;
default: // JPeG
if($target == null)
header("Content-Type: image/jpeg");
imagejpeg($new_image, $target, 95); // quality is set at 95%, can be changed
break;
}
// free memory
imagedestroy($new_image);
imagedestroy($image);
}
//-------------------------------------------------------------------------------------------------
?>
Po vyvolání funkce, např.: „image_join('back.png', 'signature.png');“, je vrácen obrázek s rozměry obrázku „back.png“ a v jeho dolním pravém rohu je umístěn obrázek „signature.png“ - samozřejmě tady na weblogu je funkce doplněná o cachování, kontrolu existence souborů a podobné věci, ale snažil jsem se, aby byla tato publikovaná verze co nejjasnější a dobře pochopitelná.