PHP to add watermark and upload image watermark PHP class _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg php class
PHP tutorial for image watermark and upload image watermark PHP class


/*
* Function: PHP image watermark (watermark supports picture or text)
Parameters
* $groundImage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
* $waterPos watermark position, there are 10 kinds of states, 0 is a random position;
* 1 for the top left, 2 for the top center, 3 for the top right;
* 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
* 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
* $waterImage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
* $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
* $textFont Text size, value 1, 2, 3, 4 or 5, default is 5;
* $textColor text color, value is hexadecimal color value, default is #ff0000 (red);
*
* Note: Support GD 2.0,support FreeType, gif Read, GIF Create, JPG, PNG
* $waterImage and $waterText best not to use at the same time, choose one of them, priority to use the $waterImage.
* When the $waterimage is valid, the parameters $waterstring, $stringFont, $stringColor are not effective.
* The watermark image has the same file name as the $groundImage.
* Author: longware @ 2004-11-3 14:15:13
*/
function Imagewatermark ($groundImage, $waterPos =0, $waterImage = "", $waterText = "", $textFont =5, $textColor = "#FF0000 ″)
{
$isWaterImage = FALSE;
$FORMATMSG = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, PNG format. ”;

Read Watermark File
if (!emptyempty ($waterImage) && file_exists ($waterImage))
{
$isWaterImage = TRUE;
$water _info = getimagesize ($waterImage);
$water _w = $water _info[0];//To obtain the width of the watermark picture
$water _h = $water _info[1];//To obtain a high watermark image

Switch ($water _info[2])//Get the format of the watermark picture
{
Case 1: $water _im = Imagecreatefromgif ($waterImage);
Case 2: $water _im = Imagecreatefromjpeg ($waterImage);
Case 3: $water _im = Imagecreatefrompng ($waterImage);
Default:die ($FORMATMSG);
}
}

Read background picture
if (!emptyempty ($groundImage) && file_exists ($groundImage))
{
$ground _info = getimagesize ($groundImage);
$ground _w = $ground _info[0];//Get the width of the background image
$ground _h = $ground _info[1];//get a high background picture

Switch ($ground _info[2])//Get the format of the background picture
{
Case 1: $ground _im = Imagecreatefromgif ($groundImage);
Case 2: $ground _im = Imagecreatefromjpeg ($groundImage);
Case 3: $ground _im = Imagecreatefrompng ($groundImage);
Default:die ($FORMATMSG);
}
}
Else
{
Die ("The picture that needs watermark does not exist!") ”);
}

Watermark Location
if ($isWaterImage)//Picture watermark
{
$w = $water _w;
$h = $water _h;
$label = "Picture";
}
else//text watermark
{
$temp = Imagettfbbox (Ceil ($textFont), 0, "./cour.ttf", $waterText);//Get the range of text that uses TrueType fonts
$w = $temp [2]-$temp [6];
$h = $temp [3]-$temp [7];
Unset ($temp);
$label = "Text Area";
}
if ($ground _w< $w) | | ($ground _h< $h))
{
echo "The length or width of the picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ”;
Return
}
Switch ($waterPos)
{
Case 0://Random
$posX = rand (0, ($ground _w-$w));
$posY = rand (0, ($ground _h-$h));
Break
Case 1://1 for top left
$posX = 0;
$posY = 0;
Break
Case 2://2 centered on the top
$posX = ($ground _w-$w)/2;
$posY = 0;
Break
Case 3://3 for top right
$posX = $ground _w-$w;
$posY = 0;
Break
Case 4://4 for the middle left
$posX = 0;
$posY = ($ground _h-$h)/2;
Break
Case 5://5 centered in the middle
$posX = ($ground _w-$w)/2;
$posY = ($ground _h-$h)/2;
Break
Case 6://6 for the middle right
$posX = $ground _w-$w;
$posY = ($ground _h-$h)/2;
Break
Case 7://7 for Bottom left
$posX = 0;
$posY = $ground _h-$h;
Break
Case 8://8 centered at bottom
$posX = ($ground _w-$w)/2;
$posY = $ground _h-$h;
Break
Case 9://9 for bottom right
$posX = $ground _w-$w;
$posY = $ground _h-$h;
Break
default://Random
$posX = rand (0, ($ground _w-$w));
$posY = rand (0, ($ground _h-$h));
Break
}

To set the color blending mode of an image
Imagealphablending ($ground _im, true);

if ($isWaterImage)//Picture watermark
{
Imagecopy ($ground _im, $water _im, $posX, $posY, 0, 0, $water _w, $water _h);//copy watermark to target file
}
else//text watermark
{
if (!emptyempty ($textColor) && (strlen ($textColor) ==7))
{
$R = Hexdec (substr ($textColor,));
$G = Hexdec (substr ($textColor, 3,2));
$B = Hexdec (substr ($textColor, 5));
}
Else
{
Die ("Watermark text color format is not correct! ”);
}
Imagestring ($ground _im, $textFont, $posX, $posY, $waterText, Imagecolorallocate ($ground _im, $R, $G, $B));
}

Create a picture after a watermark
@unlink ($groundImage);
Switch ($ground _info[2])//Get the format of the background picture
{
Case 1:imagegif ($ground _im, $groundImage);
Case 2:imagejpeg ($ground _im, $groundImage);
Case 3:imagepng ($ground _im, $groundImage);
Default:die ($ERRORMSG);
}

Freeing memory
if (Isset ($water _info)) unset ($water _info);
if (Isset ($water _im)) Imagedestroy ($water _im);
unset ($ground _info);
Imagedestroy ($ground _im);
}
—————————————————————————————
$id =$_request[' id '];
$num = count ($_files[' userfile ' [' name ']);
Print_r ($_files[' userfile ');
Print_r ($_files[' userfile ' [' name ']);

Echo $num;
echo "
”;
if (Isset ($id)) {
for ($i =0; $i < $id; $i + +) {

if (Isset ($_files) &&!emptyempty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
{
$uploadfile = "./". Time (). " _ ". $_files[' UserFile '][name][$i];
echo "
”;
Echo $uploadfile;
if (copy ($_files[' userfile ' [' tmp_name '] [$i], $uploadfile))
{
echo "OK
”;

Text watermark
Imagewatermark ($uploadfile, 5, "", "HTTP://www.lvye.info", 5, "#cccccc");

Image watermark
$waterImage = "logo_ok1.gif";//Watermark Picture Path
Imagewatermark ($uploadfile, 9, $waterImage);

echo "";
}
Else
{
echo "Fail
”;
}
}
}
}

?>

?>

The following code is a picture upload and upload images to add watermark

Parameter description:
$max _file_size: Upload file size limit, unit byte
$destination _folder: Upload file path
$watermark: Whether additional watermark (1 is watermark, the other is not watermark);
Instructions for use:
1. Remove the "Extension=php_gd2.dll" line in the php.ini file, because we need to use the GD library;
2. Change Extension_dir = to the directory where your Php_gd2.dll is located;
3. asp Tutorial ">http://www.knowsky.com/php.asp;
****************************************************/
Upload file Type list
$uptypes =array (
' Image/jpg ',
' Image/jpeg ',
' Image/png ',
' Image/pjpeg ',
' Image/gif ',
' Image/bmp ',
' Image/x-png '
);
$max _file_size=2000000; Upload file size limit, unit byte
$destination _folder= "uploadimg/"; Upload file path
$watermark = 1; Whether additional watermark (1 is watermark, the other is not watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterposition = 1; The watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, and 5 is centered);
$waterstring = "http://www.xplore.cn/"; Watermark String
$waterimg = "Xplore.gif"; Watermark Picture
$imgpreview = 1; Whether to generate a preview map (1 is generated, others are not generated);
$imgpreviewsize =1/2; Thumbnail scale
?>


<title>Zwell Picture Uploading Program</title>




if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name]))
Whether the file exists
{
echo "Picture does not exist www.bkjia.com!";
Exit
}
$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "File too big!";
Exit
}
if (!in_array ($file ["type"], $uptypes))
Check file types
{
echo "file type does not match!". $file ["type"];
Exit
}
if (!file_exists ($destination _folder))
{
mkdir ($destination _folder);
}
$filename = $file ["Tmp_name"];
$image _size = getimagesize ($filename);
$pinfo =pathinfo ($file ["name"]);
$ftype = $pinfo [' extension '];
$destination = $destination _folder.time (). ".". $ftype;
if (file_exists ($destination) && $overwrite! = True)
{
echo "file with the same name already exists";
Exit
}
if (!move_uploaded_file ($filename, $destination))
{
echo "Error moving File";
Exit
}
$pinfo =pathinfo ($destination);
$fname = $pinfo [basename];
echo "has been successfully uploaded
File name: ". $destination _folder. $fname."
";
echo "width:". $image _size[0];
echo "Length:". $image _size[1];
echo "
Size: ". $file [" Size "]." bytes ";
if ($watermark ==1)
{
$iinfo =getimagesize ($destination, $iinfo);
$nimage =imagecreatetruecolor ($image _size[0], $image _size[1]);
$white =imagecolorallocate ($nimage, 255,255,255);
$black =imagecolorallocate ($nimage, 0,0,0);
$red =imagecolorallocate ($nimage, 255,0,0);
Imagefill ($nimage, 0,0, $white);
Switch ($iinfo [2])
{
Case 1:
$simage =imagecreatefromgif ($destination);
Break
Case 2:
$simage =imagecreatefromjpeg ($destination);
Break
Case 3:
$simage =imagecreatefrompng ($destination);
Break
Case 6:
$simage =imagecreatefromwbmp ($destination);
Break
Default
Die ("Unsupported file type");
Exit
}
Imagecopy ($nimage, $simage, 0,0,0,0, $image _size[0], $image _size[1]);
Imagefilledrectangle ($nimage, 1, $image _size[1]-15,80, $image _size[1], $white);
Switch ($watertype)
{
Case 1://Add watermark String
Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black);
Break
Case 2://Add watermark Picture
$simage 1 =imagecreatefromgif ("Xplore.gif");
Imagecopy ($nimage, $simage 1,0,0,0,0,85,15);
Imagedestroy ($simage 1);
Break
}
Switch ($iinfo [2])
{
Case 1:
Imagegif ($nimage, $destination);
Imagejpeg ($nimage, $destination);
Break
Case 2:
Imagejpeg ($nimage, $destination);
Break
Case 3:
Imagepng ($nimage, $destination);
Break
Case 6:
Imagewbmp ($nimage, $destination);
Imagejpeg ($nimage, $destination);
Break
}
Overwrite the original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}
if ($imgpreview ==1)
{
echo "
Picture preview:
";
echo "echo" alt= "Picture preview: R filename:". $destination. " rwww.bkjia.com upload time: ">";
}
}
?>


http://www.bkjia.com/PHPjc/444950.html www.bkjia.com true http://www.bkjia.com/PHPjc/444950.html techarticle PHP Tutorial to add watermark and upload image watermark PHP class/* Function: PHP image watermark (Watermark support image or text) * Parameters: * $groundImage background image, that is, the need to add watermark map ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.