Home > Backend Development > PHP Tutorial > PHP multiple file upload (example of adding watermark to pictures)_PHP tutorial

PHP multiple file upload (example of adding watermark to pictures)_PHP tutorial

WBOY
Release: 2016-07-20 11:08:22
Original
835 people have browsed it

PHP multiple file upload (example of adding watermark to pictures)

php tutorial Multiple file upload (example of adding watermark to pictures)





php Multiple file upload (example of adding watermark to pictures)


if($_post['sub' ]=="www")
{
$waterimg="water.png";
$ftype=array('image/jpg','image/jpeg','imgage/png',' image/pjpeg','image/gif');//File types allowed to be uploaded
$files=$_files['files'];
$fnum=count($files['name']); //Get the number of uploaded files
for($i=0;$i<$fnum;$i++)
{

if($files['name'][$i]! =''&&is_uploaded_file($files['tmp_name'][$i]))
{

if(in_array($files['type'][$i],$ftype))// Determine whether the file is an allowed type
{

$fname[$i]='upfile/'.rand(0,10000).time().substr($files['name'] [ $i],strrpos($files['name'][$i],'.'));//Automatic naming
move_uploaded_file($files['tmp_name'][$i],$fname[$i ]);
echo '
File uploaded successfully! ';

}
else
{
echo '
Not allowed file type! ';
exit;
}
}
else
{
echo '
The file does not exist! ';
exit;
}
watermark($fname[$i],$waterimg);
}

$string=implode('|',$fname);
echo $string;
}

?>


 



Upload another one
/**
* Add watermark to the image
* @param string $desimg The target image parameter format is ./images/pic.jpg
* @param string $waterimg The watermark image parameter format is the same as above, and the watermark image is in png format. Background transparent
* @param int positon Watermark position 1: Top left 2: Top right 3: Center 4: Bottom left 5: Bottom right
* @param bool $saveas Whether to save as, default value false , indicating overwriting the original image
* @param int $alpha The opacity of the watermark image
* @return string $savepath The path of the new image
* **/
function watermark($ desimg,$waterimg,$positon=1,$saveas=false,$alpha=30)
{
//Get the basic information of the target image
$temp=pathinfo($desimg);
$name=$temp["basename"];//File name
$path=$temp["dirname"];//Folder where the file is located
$extension=$temp["extension"]; //File extension
if($saveas)
{
//Need to save as
$name=rtrim($name,".$extension")."_2.";// Rename
$savepath=$path."/".$name.$extension;
}
else
{
//Overwrite the original image if you don’t need to save it as $savepath=$path."/".$name;
}
$info=getimageinfo($desimg);//Get the information of the target image
$info2=getimageinfo($waterimg);// Get the information of the watermark image
$desimg=create($desimg);//Create from the original image
$waterimg=create($waterimg);//Create from the watermark image
//Position 1: Top Left
if($positon==1)
{
$x=0;
$y=0;
}
//Position 2: Top right
if($positon==2)
{
$x=$info[0]-$info2[0];
$y=0;
}
//Position 3: Centered
if($positon==3)
{
$x=($info[0]-$info2[0])/2;
$y=($info[1] -$info2[1])/2;
}
//地位4:底部居左
if($positon==4)
{
$x=0;
$y=$info[1]-$info2[1];
}
//地位5:底部居右
if($positon==5)
{
$x=$info[0]-$info2[0];
$y=$info[1]-$info2[1];
}
imagecopymerge($desimg,$waterimg,$x,$y,0,0,$info2[0],$info2[1],$alpha);
imagejpeg($desimg,$savepath);
imagedestroy($desimg);
imagedestroy($waterimg);
return $savepath;
}
/**
* Get image information, width, height, image/type
* @param string $src image path
* @return array
* **/
function getimageinfo($src)
{
return getimagesize($src);
}
/**
* Create an image and return the resource type
* @param string $src Image path
* @return resource $im Return the resource type
* **/
function create($src)
{
$info=getimageinfo($src);
switch ($info[2])
{
case 1:
$im=imagecreatefromgif($src);
break;
case 2:
$im=imagecreatefromjpeg($src);
break;
case 3:
$im=imagecreatefrompng($src);
break;
}
return $im;
}
 ?>



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444896.htmlTechArticlephp 多个文件上传(给图片加水印实例) php教程 多个文件上传(给图片加水印实例) !doctype html public -//w3c//dtd xhtml 1.0 transitional//en http://www.w3.org...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template