zoukankan      html  css  js  c++  java
  • PHP上传图片至阿里云

    <?php 
    header("Content-type: text/html; charset=utf-8");
    header('Access-Control-Allow-Origin:*');  
    require_once '/aliyun-oss-php-sdk-master/autoload.php';
    use OSSOssClient;
    use OSSCoreOssException;
    $accessKeyId = "*********"; ;
    $accessKeySecret = "************";
    $endpoint = "oss-cn-shanghai.aliyuncs.com";
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->setTimeout(3600);
    $ossClient->setConnectTimeout(5); 
    function create_guid() {
       $charid = strtoupper(md5(uniqid(mt_rand(), true)));
       $uuid = 
        substr($charid, 0, 8)
       .substr($charid, 8, 4)
       .substr($charid,12, 4)
       .substr($charid,16, 4);
       return $uuid;
      }
    function WriteLog($info)
    {
      $file  = 'F:wampserversetupwampwwwlog.txt';
      file_put_contents($file, $info,FILE_APPEND);
    }
    function test_input($data)
     {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    function Outputsmallimage($tmp_path, $dst_w, $file_path, $file_name)
      {
        $arr=getimagesize($tmp_path);
        $src_w=$arr[0];
        $src_h=$arr[1];
        $type=$arr[2];
        switch($type){
          case 1:$src_im=imagecreatefromgif($tmp_path);break;
          case 2:$src_im=imagecreatefromjpeg($tmp_path);break;
          case 3:$src_im=imagecreatefrompng($tmp_path);break;
          default:UtlsSvc::showMsg('不支持该图片类型','/coinproduct/index/');
        }
        if ($dst_w == 500) {
          $dst_h = 385;
        } elseif ($dst_w == 300) {
          $dst_h = 225;
        } elseif ($dst_w == 100) {
          $dst_h = 75;
        }
        $dst_im=imagecreatetruecolor($dst_w,$dst_h);
        imagecopyresized($dst_im,$src_im,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
        imagejpeg($dst_im, $file_path.'/'.$file_name);
      }
    class Backinform
    {
    public $kindsmall ;
    public $kindbig;
    public $kind;
    }
    class CommentImage
    {
     private $servername;
     private $username ;
     private $password;
     private $dbname ;
     private $sql;
     private $conn;
     public function CommentImage()
     { 
       $this->servername="cnpsimout.mysql.rds.aliyuncs.com:****";
       $this->username="******";
       $this->password="******";
       $this->dbname="******";
       $this->conn = new mysqli($this->servername,$this->username,$this->password,$this->dbname);
      if ($this->conn->connect_error)
        {
             die("Connection failed: " . $conn->connect_error);
             WriteLog("Connection failed: " . $conn->connect_error);
        } 
     }
     public function Insert_image($resourceid,$mediaid,$linktype,$linkid,$resourcetype,$image,$image2)
     {
      $this->sql = "INSERT INTO resources (resourceid,mediaid,linktype,linkid,resourcetype,smallurl,bigurl)
               VALUES ('$resourceid','$mediaid','$linktype','$linkid','$resourcetype','$image','$image2')";
          if ($this->conn->query($this->sql) === TRUE) 
          {
               WriteLog(mysqli_error());
              return "OK";
    
          } 
          else 
          {
             WriteLog(mysqli_error());
             return "NO";
          }
        $str=mysql_error($this->conn);
        WriteLog($str);
      }
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST')  
    {  
        WriteLog("
    上传开始时间:".date('y-m-d h:i:s',time())."
    ");
        $backinfo=new Backinform();
        $backinfo->kindbig="";
        $backinfo->kindsmall="";
        $backinfo->kind="";
        $uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');  
        $destination_folder="uploadimg/"; //上传文件路径 
        $file = $_FILES["upfile"];  
        $filename=$file["tmp_name"];  
        $image_size = getimagesize($filename);  
        $pinfo=pathinfo($file["name"]);  
        $ftype=$pinfo['extension'];  
        $imagebig=create_guid().".".$ftype;
        $imagesmall=create_guid().".".$ftype;
        $bucket = "w376161501";
        $object="Ionic/Imagebig/".$imagebig; 
    try{
         $ossClient->uploadFile($bucket,$object,$filename); 
         $backinfo->kindbig="OK";
       } 
       catch (OssException $e) {
        WriteLog("上传原图失败信息".$e->getMessage());
        $backinfo->kindbig="NO";
       } 
        Outputsmallimage($filename, 100,$destination_folder, $imagesmall);  
        $object2="Ionic/Imagesmall/".$imagesmall; 
        $path=$destination_folder.$imagesmall;
     try{
        $ossClient->uploadFile($bucket,$object2,$path); 
          $backinfo->kindsmall="OK";
        } 
        catch (OssException $e) {
        WriteLog("上传压缩图失败信息".$e->getMessage());
        $backinfo->kindsmall="NO";
        } 
        unlink($path); 
        if($backinfo->kindsmall=="OK"&&$backinfo->kindbig=="OK")
        {
          $insertimage=new CommentImage();
          $resourceid=create_guid();
          $mediaid="DB5483ADCADC3ADC1E5CD2";
          $linkid=test_input($_POST["id"]);
          $linktype=test_input($_POST["eventtype"]);
          WriteLog("连接ID:".$linkid."--连接类型:".$linktype."
    ");
          $resourcetype="图片";
          if($insertimage->Insert_image($resourceid,$mediaid,$linktype,$linkid,$resourcetype,$object2,$object)=="OK")
          {
             $backinfo->kind="OK";
             $json = json_encode($backinfo);
             echo "$json";
             WriteLog("资源ID:".$resourceid."--成功上传截止:".date('y-m-d h:i:s',time())."
    ");
           }
           else
           {
            WriteLog("
    数据添加失败");
           }
        }
    }
    ?>

    demo下载地址http://download.csdn.net/detail/w76161501/9663266

  • 相关阅读:
    Codeforces 1255B Fridge Lockers
    Codeforces 1255A Changing Volume
    Codeforces 1255A Changing Volume
    leetcode 112. 路径总和
    leetcode 129. 求根到叶子节点数字之和
    leetcode 404. 左叶子之和
    leetcode 104. 二叉树的最大深度
    leetcode 235. 二叉搜索树的最近公共祖先
    450. Delete Node in a BST
    树的c++实现--建立一棵树
  • 原文地址:https://www.cnblogs.com/wangboke/p/5996390.html
Copyright © 2011-2022 走看看