PHP实现批量生成App各种尺寸Logo,php文件图片批量上传程序代码

任何个人、组织可自由对本程序进行使用、二次开发等权力,复制代码 代码如下

说明:
uploadx
php批量上传组件遵循开源协议(GPL),任何个人、组织可自由对本程序进行使用、二次开发等权力。
由此也将声明本人不对您个人、组织使用本程序所带来的商业利益及损失有干涉及负责,但请保留版权信息。
也欢迎对uploadx提出保贵的建议及意见,不胜感激。
本程序使用PHP程序编写,能更高效的批量处理PHP开发中的文件上传,图片处理、批量添加图片水印等问题,在使用本程序前请详细阅读使用说明!

使用PHP
GD,使用良好,一键剪裁各种尺寸,打包下载。经常换icon的懂的,美工给你一个1024的logo,你得ps出各种尺寸,于是有了这个东西。

HTML表单页

核心代码

 代码如下

复制代码 代码如下:

<form enctype=”multipart/form-data” action=”uploadx.php”
method=”post”>  
    <input name=”uploadx[]” type=”file”>  
    <input name=”uploadx[]” type=”file”>  
    <input type=”submit” value=”上传”>  
</form>  

<?php
class image {
    /**
     * source image
     *
     * @var string|array
     */
    private $source;
    /**
     * temporay image
     *
     * @var file
     */
    private $image;
    private $ext;
    /**
     * erros
     *
     * @var array
     */
    private $error;
    /**
     * construct
     *
     * @param string|array $source
     */
    public function __construct($source = NULL) {
        if($source != NULL) {
            $this->source($source);
        }
    }
    /**
     * set the source image
     *
     * @param string|array $source
     */
    public function source($source) {
        if(!is_array($source)) {
            $this->source[“name”] = $source;
            $this->source[“tmp_name”] = $source;
            $type = NULL;
            $ext = strtolower(end(explode(“.”,$source)));
            switch($ext) {
                case “jpg”  :
                case “jpeg” : $type = “image/jpeg”; break;
                case “gif”  : $type = “image/gif”; break;
                case “png”  : $type = “image/png”; break;
            }
            $this->source[“type”] = $type;
        } else {
            $this->source = $source;
        }
        $this->destination = $this->source[“name”];
    }
    /**
     * resize the image
     *
     * @param int $width
     * @param int $height
     */
    public function resize($width = NULL,$height = NULL) {
        if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
            list($source_width,$source_height) =
getimagesize($this->source[“tmp_name”]);
            if(($width == NULL) && ($height != NULL)) {
                $width = ($source_width * $height) /
$source_height;
            }
            if(($width != NULL) && ($height == NULL)) {
                $height = ($source_height * $width) /
$source_width;
            }
            if(($width == NULL) && ($height == NULL)) {
                $width = $source_width;
                $height = $source_height;
            }
            switch($this->source[“type”]) {
                case “image/jpeg” : $created =
imagecreatefromjpeg($this->source[“tmp_name”]); break;
                case “image/gif”  : $created =
imagecreatefromgif($this->source[“tmp_name”]);  break;
                case “image/png”  : $created =
imagecreatefrompng($this->source[“tmp_name”]);  break;
            }
            $this->image = imagecreatetruecolor($width,$height);
           
imagecopyresampled($this->image,$created,0,0,0,0,$width,$height,$source_width,$source_height);
        }
    }
    /**
     * add watermark on image
     *
     * @param string $mark
     * @param int $opac
     * @param int $x_pos
     * @param int $y_pos
     */
    public function watermark($mark,$opac,$x_pos,$y_pos) {
        if(file_exists($mark) && ($this->image != “”)) {
            $ext = strtolower(end(explode(“.”,$mark)));
            switch($ext) {
                case “jpg”  :
                case “jpeg” : $watermark = imagecreatefromjpeg($mark);
break;
                case “gif”  : $watermark = imagecreatefromgif($mark); 
break;
                case “png”  : $watermark = imagecreatefrompng($mark); 
break;
            }
            list($watermark_width,$watermark_height) =
getimagesize($mark);
            $source_width = imagesx($this->image);
            $source_height = imagesy($this->image);
            if($x_pos == “top”) $pos  = “t”; else $pos  = “b”;
            if($y_pos == “left”) $pos .= “l”; else $pos .= “r”;
            $dest_x = 0;
            $dest_y = 0;
            switch($pos) {
                case “tr” : $dest_x = $source_width –
$watermark_width; break;
                case “bl” : $dest_y = $source_height –
$watermark_height; break;
                case “br” : $dest_x = $source_width –
$watermark_width; $dest_y = $source_height – $watermark_height;
break;
            }
           
imagecopymerge($this->image,$watermark,$dest_x,$dest_y,0,0,$watermark_width,$watermark_height,$opac);
        }
    }
    /**
     * crop the image
     *
     * @param int $x
     * @param int $y
     * @param int $width
     * @param int $height
     */
    public function crop($x,$y,$width,$height) {
        if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”]) && ($width > 10) &&
($height > 10)) {
            switch($this->source[“type”]) {
                case “image/jpeg” : $created =
imagecreatefromjpeg($this->source[“tmp_name”]); break;
                case “image/gif”  : $created =
imagecreatefromgif($this->source[“tmp_name”]);  break;
                case “image/png”  : $created =
imagecreatefrompng($this->source[“tmp_name”]);  break;
            }          
            $this->image = imagecreatetruecolor($width,$height);
           
imagecopy($this->image,$created,0,0,$x,$y,$width,$height);
        }
    }
    /**
     * create final image file
     *
     * @param string $destination
     * @param int $quality
     */
    public function create($destination,$quality = 100) {
        if($this->image != “”) {
            $extension = substr($destination,-3,3);
            switch($extension) {
                case “gif” : 
                    imagegif($this->image,$destination,$quality);
                    break;
                case “png” :
                    $quality = ceil($quality/10) – 1;
                    imagepng($this->image,$destination,$quality);
                    break;
                default    :
                    imagejpeg($this->image,$destination,$quality);
                    break;
            }
        }
    }
    /**
     * check if extension is valid
     *
     */
    public function validate_extension() {
        if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
            $exts = array(“image/jpeg”, “image/pjpeg”, “image/png”,
“image/x-png”);
            $ext = $this->source[“type”];
            $valid = 0;
            $this->ext = ‘.not_found’;
            if ($ext == $exts[0] || $ext == $exts[1]) {
                $valid = 1;
                $this->ext = ‘.jpg’;
            }
            // if ($ext == $exts[2]) {
            //  $valid = 1;
            //  $this->ext = ‘.gif’;
            // }
            if ($ext == $exts[2] || $ext == $exts[3]) {
                $valid = 1;
                $this->ext = ‘.png’;
            }
            if($valid != 1) {
                $this->error .= “extension”;
            }
        } else {
            $this->error .= “source”;
        }
    }
    /**
     * check if the size is correct
     *
     * @param int $max
     */
    public function validate_size($max) {
        if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
            $max = $max * 1024;
            if($this->source[“size”] >= $max) {
                $this->error .= “size”;
            }
        } else {
            $this->error .= “source”;
        }
    }
    /**
     * check if the dimension is correct
     *
     * @param int $limit_width
     * @param int $limit_height
     */
    public function validate_dimension($limit_width,$limit_height)
{
        if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
            list($source_width,$source_height) =
getimagesize($this->source[“tmp_name”]);
            if(($source_width > $limit_width) || ($source_height
> $limit_height)) {
                $this->error .= “dimension”;
            }
        } else {
            $this->error .= “source”;
        }
    }
    /**
     * get the found errors
     *
     */
    public function error() {
        $error = array();
        if(stristr($this->error,”source”)) $error[] =
“找不到上传文件”;
        if(stristr($this->error,”dimension”)) $error[] =
“上传图片尺寸太大”;
        if(stristr($this->error,”extension”)) $error[] =
“不符合要求的格式”;
        if(stristr($this->error,”size”)) $error[] =
“图片文件太大”;
        return $error;
    }
    public function error_string() {
        $error = “”;
        if(stristr($this->error,”source”)) $error .= “找不到上传文件
/ “;
        if(stristr($this->error,”dimension”)) $error .=
“上传图片尺寸太大 / “;
        if(stristr($this->error,”extension”)) $error .=
“不符合要求的格式 / “;
        if(stristr($this->error,”size”)) $error .= “图片文件太大 /
“;
        if(eregi(” / $”, $error)) {
            $error = substr($error, 0, -3);
        }
        return $error;
    }
    public function ext() {
        return $this->ext;
    }
}

uploadx.php处理页

以上就是本文所述的全部内容了,希望大家能够喜欢。

require (‘./classes/uploadx.class.php’); 
 
$uploadx = new uploadx; 
 
$uploadx->save = ‘./temp’; 
$uploadx->name = ‘auto’; 
$uploadx->mini = ‘200,200,mini’; 
$uploadx->mark = ‘./images/logo.png,0,60’; 
print_r($uploadx->is()); 

您可能感兴趣的文章:

  • PHP图片处理之使用imagecopyresampled函数实现图片缩放例子
  • php使用imagick模块实现图片缩放、裁剪、压缩示例
  • PHP中图片等比缩放的实例
  • php图片的裁剪与缩放生成符合需求的缩略图
  • php实现按指定大小等比缩放生成上传图片缩略图的方法
  • 如何实现php图片等比例缩放
  • php多功能图片处理类分享(php图片缩放类)
  • php
    gd等比例缩放压缩图片函数
  • PHP上传图片进行等比缩放可增加水印功能
  • php缩放图片(根据宽高的等比例缩放)实例介绍
  • php实现图片缩放功能类
  • 使用PHP生成二维码的两种方法(带logo图像)
  • PHP实现图片的等比缩放和Logo水印功能示例

参数说明:

$uploadx->save
上传文件的保存目录
$uploadx->name;
上传文件的命名方式
参数说明
auto=自动随机文件名,
null=原文件名(覆盖),
其他自定义如按时间:date(‘YmdHis’); 
$uploadx->mini;
生成缩略图参数
width = 生成缩略图宽度。
height = 生成缩略图高度。
mini = 生成缩略图文件后缀附加名
默认留空不生成缩略图; 
$uploadx->mark;
上传图片文件添加水印参数:
水印文件,水印位置,水印透明度

位置参数说明:
0 = 随机;
1 = 左上角;
2 = 顶部居中;
3 = 右上角;
4 = 左居中;
5 = 中部居中;
6 = 右居中;
7 = 左下角;
8 = 底部居中;
9 = 右下角;

$uploadx->is(true);
返回已上传文件的数组
is参数说明:
true = 返回所有上传文件
false = 过滤失败文件 
$uploadx->is(); 
返回数据数组格式说明:
name = 上传后已保存的文件名
mini = 生成缩略图的文件名
mark = 添加水印状态,1表示成功,否则失败
code = 错误代码:0表示上传成功
error = 错误信息:上传错误时提示的错误信息
其中生成缩略图和添加水印功能也可单独使用!

以上是对uploadx2.0 进行简单的介绍, 
也欢迎各位能在使用过程中进行改进补充

例子

 代码如下

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″
/>
<title>uploadx demo</title>
</head>

<body>
<form enctype=”multipart/form-data” action=”uploadx.php”
method=”post”>
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <input name=”uploadx[]” type=”file”> <br />
 <br />
 <input type=”submit” value=”上传所选文件”>
</form>
</body>
</html>

<?php

require (‘./classes/uploadx.class.php’);

$uploadx = new uploadx;

$uploadx->save = ‘./temp’;
$uploadx->name = ‘auto’;
$uploadx->mini = ‘200,200,mini’;
$uploadx->mark = ‘./images/logo.png,0,60’;
print_r($uploadx->mini(‘./temp/2.jpg’));

uploadx.class.php

<?php

class uploadx {
  var $form = ‘uploadx’;
  var $save = ‘./’;  
  var $size = ‘1024’;
  var $type =
‘gif,bmp,png,jpg,jpeg,swf,flv,mp3,wma,rar,zip,7z,doc,docx,ppt,pptx,xls,xlsx,txt,pdf’;
  var $name = null;
  var $mini = null;
  var $mark = null;
  var $version = ‘2.0’;

  public function is($type = true){
   foreach ($this->files()
as $key => $val) {
    $file = $mini = null;
    $file = $this->saves($val[‘name’], $val[‘type’],
$val[‘tmp_name’], $val[‘size’], $val[‘error’]);
    $file[‘code’] || $file[‘path’] =
rtrim($this->save,’/’).’/’.$file[‘name’];
    $file[‘code’] || $file[‘mini’] =
$this->mini($file[‘path’]);  
    $file[‘code’] || $file[‘mark’] =
$this->mark($file[‘path’]);    
    $file[‘code’] && $file[‘error’] =
$this->error($file[‘code’]);
    $type ? $files[] = $file :  ($file[‘code’] || $files[] =
$file);
   }
    return isset($files) ? $files : array();
  }
  private function files(){
   if(count($_FILES[$this->form])<1) return array();
   if(is_array($_FILES[$this->form][‘name’])){
    for($i=0; $i<count($_FILES[$this->form][‘name’]); $i++)
{
     if($_FILES[$this->form][‘error’][$i]==4) continue;
     $files[] = array(
      ‘name’=>$_FILES[$this->form][‘name’][$i],
      ‘type’=>$_FILES[$this->form][‘type’][$i],
      ‘tmp_name’=>$_FILES[$this->form][‘tmp_name’][$i],
      ‘error’=>$_FILES[$this->form][‘error’][$i],
      ‘size’=>$_FILES[$this->form][‘size’][$i]);
    } 
   }else{
     $files[] = $_FILES[$this->form];
   }
     return $files;
  }
  private function saves($name, $type, $temp, $size, $error){  
    if($error) return array(‘name’=>$name, ‘code’=>$error);
    $prefix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
    if(!in_array($prefix,
explode(‘,’,
strtolower($this->type)))){
     return array(‘name’=>$name, ‘code’=>’-1′);
    }
    if($size/1024>$this->size){
     return array(‘name’=>$name, ‘code’=>’-2′);
    }
    if(!is_dir($this->save)){
     if(!mkdir($this->save, 0777, TRUE)){
     return array(‘name’=>$name, ‘code’=>’-3′);
     }     
    }
    $filename = $this->name ? ($this->name==’auto’ ? uniqid() :
$this->name) : trim(basename($name,$prefix),’.’);
    $savefile = trim($this->save,’/’).’/’. $filename.’.’.$prefix;
    if(!@move_uploaded_file($temp,$savefile)){
     return array(‘name’=>$name, ‘code’=>’-4′);
    }
     @chmod($savefile,0777); 
    return array(‘name’=>$filename.’.’.$prefix, ‘code’=>0);
  }
  public function mini($file = null){
   if(!$file || !$this->mini) return false;
   if(!is_file($file)) return $this->error(-5,$file);
   list($width, $height, $extends) = explode(‘,’, $this->mini);
   $types = array(‘gif’,’png’,’jpg’,’jpeg’);
   $type = pathinfo($file, PATHINFO_EXTENSION);
   if(!in_array($type, $types)) return $this->error(-6);
   if(!is_file($file)) return $this->error(-5,$file);
   $mini = $extends ? basename($file, $type).$extends.’.’.$type :
trim(basename($file),’.’);
   $image = imagecreatefromstring(file_get_contents($file));
   $imagex = imagesx($image);
   $imagey = imagesy($image);
   $scale = $width / $imagex;
   if($width>$imagex){
    $mini_width = $imagex;
    $mini_height = $imagey;
   }else{
    $mini_width = $width;
    $mini_height = round($scale * $imagey);
   }
   if(function_exists(‘imagecreatetruecolor’) &&
function_exists(‘imagecopyresampled’)){
    $temp = imagecreatetruecolor($mini_width, $mini_height);
    imagecopyresampled($temp,$image,0,0,0,0,$mini_width, $mini_height,
$imagex, $imagey);
   }else{
    $temp = imagecreate($mini_width, $mini_height);
    imagecopyresized($temp,$image,0,0,0,0,$mini_width, $mini_height,
$imagex, $imagey);    
   }
    imagejpeg($temp, rtrim($this->save,’/’).’/’.$mini, 100);
    imagedestroy($temp);
    imagedestroy($image);

   return is_file(rtrim($this->save,’/’).’/’.$mini) ? $mini:
false;
  }
  public function mark($file = null){
     if(!$file || !$this->mark) return false;     
     list($watermark, $position, $opacity) = explode(‘,’,
$this->mark);
     if(!is_file($file) || !is_file($watermark)) return
$this->error(-5,’FILE=’.$file.’||’.’Watermark=’.$watermark);
   $type = pathinfo($file, PATHINFO_EXTENSION);
   $types = array(‘gif’,’png’,’jpg’,’jpeg’);
   if(!in_array($type, $types)) return $this->error(-6,$file);
         $opacity = min($opacity,100);
   $file_data = imagecreatefromstring(file_get_contents($file));
         $file_width = imagesx($file_data);
         $file_height = imagesy($file_data);
         if (in_array(pathinfo($watermark, PATHINFO_EXTENSION),
array(‘gif’,’png’))) {
          $mark_data =
imagecreatefromstring(file_get_contents($watermark));
          $mark_width = imagesx($mark_data);
          $mark_height =  imagesy($mark_data);
          switch($position){
              case 1: $x = 5; $y = 5; break;
              case 2: $x = ($file_width – $mark_width)/2; $y =
$mark_height; break;
              case 3: $x = ($file_width – $mark_width)-5; $y =
$mark_height; break;
              case 4: $x = 5; $y = ($file_height – $mark_height) / 2;
break;
              case 5: $x = ($file_width – $mark_width)/2; $y =
($file_height – $mark_height)/2; break;
              case 6: $x = ($file_width – $mark_width)-5; $y =
($file_height – $mark_height)/2; break;
              case 7: $x = 5; $y = ($file_height – $mark_height) – 5;
break;
              case 8: $x = ($file_width – $mark_width)/2; $y =
($file_height – $mark_height)-5; break;
              case 9: $x = ($file_width – $mark_width)-5; $y =
($file_height – $mark_height)-5; break;
              default: $x = rand(0,($file_width – $mark_width)); $y =
rand(0,($file_height – $mark_height));
          } 
     $temp = imagecreatetruecolor($mark_width, $mark_height); 
        imagecopy($temp, $file_data, 0, 0, $x, $y, $mark_width,
$mark_height); 
        imagecopy($temp, $mark_data, 0, 0, 0, 0, $mark_width,
$mark_height); 
        imagecopymerge($file_data, $temp, $x, $y, 0, 0, $mark_width,
$mark_height, $opacity);          
           imagejpeg($file_data, $file, 100);
           imagedestroy($temp);
           imagedestroy($file_data);
           imagedestroy($mark_data);
           return true;
         }else{
           return $this->error(-6,$watermark);
         } www.111cn.net
  }
  private function error($code = 0, $extends = ”){
   if($code){
   switch ($code) {    
    case 6:  $error = ‘写入临时文件夹失败’; break;
    case 5:  $error = ‘写入系统临时文件夹错误’; break;
    case 4:  $error = ‘没有文件被上传请检查表单’; break;
    case 3:  $error = ‘文件上传出错上传不完整’; break;
    case 2:  $error = ‘文件大小超出表单限制’; break;
    case 1:  $error = ‘文件大小超出系统限制’; break;
    case -1: $error = ‘上传文件类型不合法’; break;
    case -2: $error = ‘上传文件大小超出后台限制’; break;  
    case -3: $error = ‘创建文件保存路径失败’; break; 
    case -4: $error = ‘保存文件失败请检查路径’; break; 
    case -5: $error = ‘读取文件错误’; break; 
    case -6: $error = ‘不支持该操作’; break; 
    default: $error = ‘未知错误’;
   }
    return  ‘[‘.$code.’]:’.$error.$extends;
   }else{
    return false; 
   } 
  }

}