PHP Multi-image upload and check add Watermark source code

Source: Internet
Author: User
Tags imagecopy imagejpeg

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;
* Enter description here ...
*
* @param upload file size limit, unit byte $max _file_size
* @param upload file path $destination _folder
* @param whether Watermark is added (1 is watermark, others are watermark) $watermark
* @param watermark Type (1 for text, 2 for picture) $watertype
* @param 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) $waterposition
* @param watermark String $waterstring
* @param watermark Picture $waterimg
* @param whether to generate a preview map (1 is generated, others are not generated) $imgpreview
* @param thumbnail scale $imgpreviewsize
*/
Public Function Picnewsadd ($max _file_size, $destination _folder, $watermark, $watertype, $waterposition, $waterstring, $waterimg, $imgpreview, $imgpreviewsize)
{
Upload file Type list
$uptypes =array (
' Image/jpg ',
' Image/jpeg ',
' Image/png ',
' Image/pjpeg ',
' Image/gif ',
' Image/bmp ',
' Image/x-png '
);
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["File0" [' Tmp_name ']))
Whether the file exists
{
echo "Picture does not exist!";
Exit
}

$file = $_files["File0"];

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
}
Create a folder if the folder does not exist
if (!file_exists ($destination _folder))
{
mkdir ($destination _folder);
}
$filename = $file ["Tmp_name"]; Filename
$image _size = getimagesize ($filename); File size
$pinfo =pathinfo ($file ["name"]); Path
$ftype = $pinfo [' extension ']; Extended Name
$destination = $destination _folder.time (). " 0. ". $ftype; Target Name = folder + Time +.+ type
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); Re-fetch the new path
$fname = $pinfo [' basename ']; Win Meiko, Name
echo "<font Color=red> has 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)
Whether additional watermark (1 is watermark, the other is not watermark);
{

$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,100, $image _size[1], $black);
Switch ($watertype)
Watermark Type (1 for text, 2 for picture)
{

Case 1://Add watermark String
Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $white);
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)
Whether to generate a preview map (1 is generated, others are not generated);
{
echo "<br> picture preview:<br>";
echo "echo "alt=\" Picture preview: \ r file name: ". $destination." \ r upload time: \ ">";
}
}
$ffname = ' http://'. $_server[' remote_addr ']. ' /jfinance/uploadpic/'. $fname;
return $ffname;
}
Call the following $fname= $picnews->picnewsadd (1048576, ' ... /.. /uploadpic/', 1,1,1, ' www.tute.edu.cn ', ', ', ', ');

PHP Multi-image upload and check add Watermark source code

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.