Here is a little piece of code that will allow you to apply a png watermark over the top of a jpg or png that has been uploaded by PHPRunner.
The watermark needs to be a png file, of any resolution (i used 1080x864, and will completely cover the image so leave the areas transparent where you wish to have the picture show through.
After adding the code below you can lay a watermark over an image by referencing the function
watermarkImage($inputPhotoName, $outputPhotoName);//pass the same file name if you want to overwrite the origional
At the bottom of your phpfunction.php file add the following 2 Functions
--------------------------------------------------------------------
function watermarkImage($SourceFile, $DestinationFile){
$disp_width_max=150; // used when displaying watermark choices
$disp_height_max=80; // used when displaying watermark choices
$edgePadding=15; // used when placing the watermark near an edge
$quality=100; // used when generating the final image
$watermark='images/watermark.png';
// be sure that the other options we need have some kind of value
$savtype='jpeg';
$vposition='center';
$hposition='center';
$wmsize='1';
// file upload success
$size=getimagesize($SourceFile);
if($size[2]==2 || $size[2]==3){
// it was a JPEG or PNG image, so we're OK so far
$original=$SourceFile;
$targetname=date('YmdHis').''.
// if you change this regex, be sure to change it in generated-images.php:26
pregreplace('`[^a-z0-9-.]`i','',$SourceFile);
$target=$DestinationFile;
$wmTarget=$watermark.'.tmp';
$origInfo = getimagesize($original);
$origWidth = $origInfo[0];
$origHeight = $origInfo[1];
$waterMarkInfo = getimagesize($watermark);
$waterMarkWidth = $waterMarkInfo[0];
$waterMarkHeight = $waterMarkInfo[1];
// watermark sizing info
if($wmsize=='larger'){
$placementX=0;
$placementY=0;
$hposition='center';
$vposition='center';
$waterMarkDestWidth=$waterMarkWidth;
$waterMarkDestHeight=$waterMarkHeight;
// both of the watermark dimensions need to be 5% more than the original image...
// adjust width first.
if($waterMarkWidth > $origWidth1.05 && $waterMarkHeight > $origHeight1.05){
// both are already larger than the original by at least 5%...
// we need to make the watermark smaller for this one.
// where is the largest difference?
$wdiff=$waterMarkDestWidth - $origWidth;
$hdiff=$waterMarkDestHeight - $origHeight;
if($wdiff > $hdiff){
// the width has the largest difference - get percentage
$sizer=($wdiff/$waterMarkDestWidth)-0.05;
}//end if ($wdiff > $hdiff){
else{
$sizer=($hdiff/$waterMarkDestHeight)-0.05;
}//end else
$waterMarkDestWidth-=$waterMarkDestWidth $sizer;
$waterMarkDestHeight-=$waterMarkDestHeight $sizer;
}//end if($waterMarkWidth > $origWidth1.05 && $waterMarkHeight > $origHeight1.05)
else{
// the watermark will need to be enlarged for this one
// where is the largest difference?
$wdiff=$origWidth - $waterMarkDestWidth;
$hdiff=$origHeight - $waterMarkDestHeight;
if($wdiff > $hdiff){
// the width has the largest difference - get percentage
$sizer=($wdiff/$waterMarkDestWidth)+0.05;
}//end if
else{
$sizer=($hdiff/$waterMarkDestHeight)+0.05;
}//end else
$waterMarkDestWidth+=$waterMarkDestWidth $sizer;
$waterMarkDestHeight+=$waterMarkDestHeight $sizer;
}//end else
}//end if($wmsize=='larger'){
else{
$waterMarkDestWidth=round($origWidth floatval($wmsize));
$waterMarkDestHeight=round($origHeight floatval($wmsize));
if($wmsize==1){
$waterMarkDestWidth-=2$edgePadding;
$waterMarkDestHeight-=2$edgePadding;
}//end if
}//end else
// OK, we have what size we want the watermark to be, time to scale the watermark image
resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);
// get the size info for this watermark.
$wmInfo=getimagesize($wmTarget);
$waterMarkDestWidth=$wmInfo[0];
$waterMarkDestHeight=$wmInfo[1];
$differenceX = $origWidth - $waterMarkDestWidth;
$differenceY = $origHeight - $waterMarkDestHeight;
$placementX = round($differenceX / 2);
$placementY = round($differenceY / 2);
if($size[2]==3)
$resultImage = imagecreatefrompng($original);
else
$resultImage = imagecreatefromjpeg($original);
imagealphablending($resultImage, TRUE);
$finalWaterMarkImage = imagecreatefrompng($wmTarget);
$finalWaterMarkWidth = imagesx($finalWaterMarkImage);
$finalWaterMarkHeight = imagesy($finalWaterMarkImage);
imagecopy($resultImage,
$finalWaterMarkImage,
$placementX,
$placementY,
0,
0,
$finalWaterMarkWidth,
$finalWaterMarkHeight
);
if($size[2]==3){
imagealphablending($resultImage,FALSE);
imagesavealpha($resultImage,TRUE);
imagepng($resultImage,$target,$quality);
}//end if
else{
imagejpeg($resultImage,$target,$quality);
}//end else
imagedestroy($resultImage);
imagedestroy($finalWaterMarkImage);
}
}//end function watermarkImage
function resize_png_image($img,$newWidth,$newHeight,$target){
$srcImage=imagecreatefrompng($img);
if($srcImage==''){
return FALSE;
}
$srcWidth=imagesx($srcImage);
$srcHeight=imagesy($srcImage);
$percentage=(double)$newWidth/$srcWidth;
$destHeight=round($srcHeight$percentage)+1;
$destWidth=round($srcWidth$percentage)+1;
if($destHeight > $newHeight){
// if the width produces a height bigger than we want, calculate based on height
$percentage=(double)$newHeight/$srcHeight;
$destHeight=round($srcHeight$percentage)+1;
$destWidth=round($srcWidth$percentage)+1;
}
$destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
if(!imagealphablending($destImage,FALSE)){
return FALSE;
}
if(!imagesavealpha($destImage,TRUE)){
return FALSE;
}
if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){
return FALSE;
}
if(!imagepng($destImage,$target)){
return FALSE;
}
imagedestroy($destImage);
imagedestroy($srcImage);
return TRUE;
}//end function resize_png_image
?>
--------------------------------------------------------------------
Modified version of
/
KOIVI GD Image Watermarker for PHP Copyright (C) 2004 Justin Koivisto
Version 2.0
Last Modified: 12/9/2004
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Full license agreement notice can be found in the LICENSE file contained
within this distribution package.
Justin Koivisto
justin.koivisto@gmail.com
http://www.koivi.com
/