PHP: Upload Image program, add watermark for image

Source: Internet
Author: User
Tags array exit file size http post imagecopy imagejpeg
Program | Upload <?php
$uptypes =array (' image/jpg ',//upload file type list
' 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 watermarks (1 for Watermark, others for no watermark);
$watertype =1;     //Watermark type (1 for text, 2 for picture)
$waterposition =1;    // Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered);
$waterstring = "lengshuye.3322.org"; //watermark string
$waterimg = "Xplore.gif";   //Watermark Picture
$ imgpreview=1;     //Whether to generate a preview diagram (1 is generated, others are not generated);
$imgpreviewsize =1/2;   //thumbnail proportions

<title> Lengshuye.3322.org</title>
<style type= "Text/css"
<!--
Body

  font-size:9pt;
}
Input {
 background-color: #66CCFF;
 border:1px inset #CCCCCC;
}
-->
</style>

<body>
<form enctype= "Multipart/form-data" method= "post" name= "Upform" >
Upload file:
<input name= "upfile" type= "File" >
<input type= "Submit" value= "Upload" ><br>
The types of files allowed to be uploaded are: <?=implode (', ', $uptypes)?>//implode the array element with "," separate
</form>

<?php
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"] ["Tmp_name"])//is_uploaded_file determine if the file was uploaded via an HTTP POST
Is there a file
{
echo "Picture does not exist!";
Exit
}

$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "file is too big!";
Exit
}

if (!in_array ($file [type], $uptypes))//in_array--check that there is a value in the array
Check file type
{
echo "file type does not match!". $file [' type '];
Exit
}

if (!file_exists ($destination _folder))//file_exists--check if the file or directory exists
mkdir ($destination _folder);

$filename = $file ["Tmp_name"];
$image _size = getimagesize ($filename);
$pinfo =pathinfo ($file ["name"]);//pathinfo--Returns information about the file path
$ftype = $pinfo ["extension"];//extension denotes a suffix, for example: gif jpg
$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))//move_uploaded_file-Move uploaded files to a new location
{
echo "Move file Error";
Exit
}

$pinfo =pathinfo ($destination);
$fname = $pinfo ["basename"];
echo "<font color=red> has been successfully uploaded </font><br> FileName: <font color=blue>". $destination _folder. $fname. " </font><br> ";
echo "width:". $image _size[0];
echo "Length:". $image _size[1];
echo "<br> size:". $file ["Size"]. "bytes";

if ($watermark ==1)
{
$iinfo =getimagesize ($destination, $iinfo);
$nimage =imagecreatetruecolor ($image _size[0], $image _size[1]);//imagecreatetruecolor--Create a new true color image
$white =imagecolorallocate ($nimage, 255,255,255);//imagecolorallocate--Assigning color to an image
$black =imagecolorallocate ($nimage, 0,0,0);
$red =imagecolorallocate ($nimage, 255,0,0);
Imagefill ($nimage, 0,0, $white);//imagefill--area fill
Switch ($iinfo [2])
{
Case 1:
$simage =imagecreatefromgif ($destination);//gif
Break
Case 2:
$simage =imagecreatefromjpeg ($destination);//jpg
Break
Case 3:
$simage =imagecreatefrompng ($destination);//png
Break
Case 6:
$simage =imagecreatefromwbmp ($destination);//bmp
Break
Default
Die ("Unsupported file type");
Exit
}

Imagecopy ($nimage, $simage, 0,0,0,0, $image _size[0], $image _size[1]);//imagecopy--part of the copy image
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);//imagedestroy ($simage 1) frees the memory associated with $simage 1, destroying the image
Break
}

Switch ($iinfo [2])
{
Case 1:
Imagegif ($nimage, $destination);
Imagejpeg ($nimage, $destination);//imagejpeg--Output the image to a browser or file in JPEG format
Break
Case 2:
Imagejpeg ($nimage, $destination);
Break
Case 3:
Imagepng ($nimage, $destination);
Break
Case 6:
Imagewbmp ($nimage, $destination);
Imagejpeg ($nimage, $destination);
Break
}

Overwrite original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}

if ($imgpreview ==1)
{
echo "<br> picture preview:<br>";
echo "echo "alt=\" Picture preview: \ r filename: ". $destination." \ r upload time: \ ">";
}
}
?>
</body>


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.