多图片上传程序代码,PHP中多张图片合成一张图

作者: 金沙澳门官网  发布:2019-09-26

今天分享一段PHP中多张图片合成一张图片的代码实现,我们可以在图片合成,水印,九宫格等需求下使用到类似这样的图片合成效果,下面一起来看看例子吧。

文件上传例子

例子

 代码如下

 代码如下

<?php
header('content-type:text/html;charset=utf-8');
require('uploadFile.php');

$imgs = array();
$imgs[0] = '1.jpg';
$imgs[1] = '2.jpg';
$imgs[2] = '3.jpg';
$imgs[3] = '4.jpg';
$target = 'emp.jpg';//背景图片
$target_img = Imagecreatefromjpeg($target);
$source= array();
foreach ($imgs as $k=>$v){
    $source[$k]['source'] = Imagecreatefromjpeg($v);
    $source[$k]['size'] = getimagesize($v);
}
//imagecopy ($target_img,$source[0]['source'],2,2,0,0,$source[0]['size'][0],$source[0]['size'][1]);
//imagecopy ($target_img,$source[1]['source'],250,2,0,0,$source[1]['size'][0],$source[1]['size'][1]);
$num1=0;
$num=3; //控制列数,一行几列,0为1以此类推。
$tmp=2;
$tmpy=2; //图片之间的间距
for ($i=0; $i<4; $i++){
    imagecopy($target_img,$source[$i]['source'],$tmp,$tmpy,0,0,$source[$i]['size'][0],$source[$i]['size'][1]);
    $tmp = $tmp+$source[$i]['size'][0];
    $tmp = $tmp+5;
    if($i==$num){
        $tmpy = $tmpy+$source[$i]['size'][1];
        $tmpy = $tmpy+5;
        $tmp=2;
        $num=$num+3;
    }
}
Imagejpeg($target_img,'pin.jpg');
?>

if(isset($_POST['submit'])){
$uploads = $_FILES['file'];
$num_file = count($uploads['name']);

<img src="pin.jpg">

$up = new UploadFile($uploads,'uploads',1024);
$num = $up->upload();

if($num == $num_file ){
echo '全部文件上传成功';
exit;
}else{
echo $num,'个文件上传成功<br/>';
echo $up->showErrorInfo();
exit;
}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ";
<html xmlns=" 1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form action="uup.php" method="post" enctype="multipart/form-data">
<p><input name="file[]" type="file" /></p>
<p><input name="file[]" type="file" /></p>
<p><input name="file[]" type="file" /></p>
<p><input name="file[]" type="file" /></p>
<input name="submit" type="submit" />
</form>
</body>
</html>

文件上传类代码

 代码如下

<?php
/*------------*/
class UploadFile
{
var $user_post_file  = array();
var $save_file_path  = '';
var $max_file_size   = '';
var $allow_type      = array('gif','jpg','png','zip','rar','txt','doc','pdf');
var $final_file_path = '';
var $save_info       = array();
var $error_info      = array();

/**
*构造函数,用于初始化信息。
*
*@param Array $file
*@param String $path
*@param Integer $size
*@param Array $type
*/
function __construct($file,$path,$size = 2097152,$type='')
{
$this->user_post_file   = $file;
$this->save_file_path   = $path;
$this->max_file_size    = $size;
if(!$type=''){
$this->allow_type[] = $type;
}
}

/**
*
*
*@access public
*@return int
*/
function upload()
{
for($i=0;$i<count($this->user_post_file['name']);$i++)
{
if($this->user_post_file['error'][$i] == 0){//上传文件状态正常
//获取当前文件名,临时文件名,大小,类型,扩展名
$name     = $this->user_post_file['name'][$i];
$tmp_name = $this->user_post_file['tmp_name'][$i];
$size     = $this->user_post_file['size'][$i];
$type     = $this->user_post_file['type'][$i];
$ext_name = $this->getExtName($name);

//文件大小
if(!$this->checkSize($size)){
$this->error_info[] = '您上传的文件:'.$name.'太大';
continue;
}
//扩展名
if(!$this->checkType($ext_name)){
$this->error_info[] = '您上传的文件:'.$name.'不合法';
continue;
}
//非法上传
if(!is_uploaded_file($tmp_name)){
$this->error_info[] = '您上传的文件:'.$name.'属于非法提交';
continue;
}

//
$basename = $this->getBaseName($name,".".$ext_name);
$final_filename = $basename.'-'.time().'-'.rand(1,10000).'.'.$ext_name;
$this->final_file_path = $this->save_file_path.'/'.$final_filename;

if(!move_uploaded_file($tmp_name,$this->final_file_path)){
$this->error_info = $this->user_post_file['error'][$i];
continue;
}

//
$this->save_info[] = array(
"name" => $name,
"ext_name" => $ext_name,
          "type" => $type,
                            "size" => $size,
"final_filename" => $final_filename,
                            "path" => $this->final_file_path
);
}
}

return count($this->save_info);
}

/*
 *检查用户上传文件的大小时候合法
 *
 *@param Integer $size
 *@access private
 *@return boolean
 */
function checkSize($size)
{
if($size > $this->max_file_size){
return FALSE;
}

return TRUE;
}

/*
 *检查用户上传文件的类型是否合法
 *
 *@access private
 *@return boolean
 */
function checkType($extension)
{
foreach($this->allow_type as $type){
if(strcasecmp($extension,$type) == 0){
return TRUE;
}
}

return FALSE;
}

/*
 *获取文件的扩展名
 *
 *@param string $filename
 *@access private
 *@return string
 */
function getExtName($filename)
{
$p = pathinfo($filename);

return $p['extension'];
}

/*
 *获取文件名(不包括扩展名)
 *
 *@param string $filename
 *@param string $type
 *@access private
 *@return boolean
 */
function getBaseName($filename,$ext_name)
{
$basename = basename($filename,$ext_name);

return $basename;
}

/*
 *
 *
 *
 */
function showErrorInfo()
{
if(count($this->error_info) != 0){
//echo 'error...<br/>';
foreach($this->error_info as $k=>$v){
echo ($k+1),':',$v,'<br/>';
}
}
}

function getSaveInfo()
{
return $this->save_info;
}
}

//$upload = new UploadFile('','');
//$upload = new UploadFile();
//$upload->showErrorInfo();

?>

本文由金沙澳门官网送注册58发布于金沙澳门官网,转载请注明出处:多图片上传程序代码,PHP中多张图片合成一张图

关键词: