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 OSS\OssClient;
use OSS\Core\OssException;
$accessKeyId = "*********"; ;
$accessKeySecret = "************";
$endpoint = "oss-cn-shanghai.aliyuncs.com";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->setTimeout();
$ossClient->setConnectTimeout();
function create_guid() {
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$uuid =
substr($charid, , )
.substr($charid, , )
.substr($charid,, )
.substr($charid,, );
return $uuid;
}
function WriteLog($info)
{
$file = 'F:\wampserver\setup\wamp\www\log.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[];
$src_h=$arr[];
$type=$arr[];
switch($type){
case :$src_im=imagecreatefromgif($tmp_path);break;
case :$src_im=imagecreatefromjpeg($tmp_path);break;
case :$src_im=imagecreatefrompng($tmp_path);break;
default:UtlsSvc::showMsg('不支持该图片类型','/coinproduct/index/');
}
if ($dst_w == ) {
$dst_h = ;
} elseif ($dst_w == ) {
$dst_h = ;
} elseif ($dst_w == ) {
$dst_h = ;
}
$dst_im=imagecreatetruecolor($dst_w,$dst_h);
imagecopyresized($dst_im,$src_im,,,,,$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("\n上传开始时间:".date('y-m-d h:i:s',time())."\n");
$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, ,$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."\n");
$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())."\n");
}
else
{
WriteLog("\n数据添加失败");
}
}
}
?>
demo下载地址http://download.csdn.net/detail/w76161501/9663266
PHP上传图片至阿里云的更多相关文章
- 上传图片到阿里云OSS和获取上传图片的外网url的步骤
啥都不说 直接上代码 1.html: <form action="/bcis/api/headImgUpload.json" method="post" ...
- swift3.0 从相册选取或者拍照上传图片至阿里云OSS
导入相应的库 import Photos import AliyunOSSiOS 选取照片需要继承 UIImagePickerControllerDelegate,UINavigationContro ...
- multipartUpload上传图片到阿里云
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue 上传图片到阿里云(前端直传:不推荐)
为何要这样做:减轻后端数据库压力(个人觉得于前端没啥用,谁返回来都行) 代码部分: <template> <div class="upLoad"> < ...
- Java中使用RestFul接口上传图片到阿里云OSS服务器
1.接口方法 import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springfr ...
- 压缩并上传图片到阿里云(jfinal)
/** * 获取上传文件 * * @param r * @Return: com.oreilly.servlet.multipart.FilePart */public static FilePart ...
- vue中上传图片至阿里云oss
1.开通阿里云的oss服务这些这里就不多做介绍了 2.登入阿里云的后台管理系统创建一个Bucket 3.在后台管理系统中进入访问控制 4.点击用户管理->新建用户->填写相关信息,就生成了 ...
- 上传图片到阿里云oss
阿里云地址 登录阿里云管理控制台,创建对象存储oss private static final String endpoint = "http://oss-cn-shanghai.aliyu ...
- TP框架上传图片至阿里云oss
首先安装阿里云oss扩展: composer require aliyuncs/oss-sdk-php 如果这个安装不上可以直接下载SDK的包: 链接:https://pan.baidu.com/s/ ...
随机推荐
- 分享一个自己写的基于JQuery的一个Web背景切换的Demo
这个效果主要有两个特点: 1. 背景切换的渐变 2. 背景大小自适应 3. 背景自适应保持比例同时, 相对居中 js源码: (function ($) { $.fn.bgChange = functi ...
- angular controller之间通信方式
对于日常开发中,难免会有controller之间通信需求.对于controller之间通信有两种方式可以做到. 用 Angular 进行开发,基本上都会遇到 Controller 之间通信的问题,本文 ...
- No DEFAULT or UI configuration directive found!
虚拟机安装OracleLinux出错: No DEFAULT or UI configuration directive found! 解决方法:我使用第一条就成功了 在报错信息后面的boot命令行输 ...
- ios 文字图标
如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...
- .Net中如何使用MySql连接池
提供一份官方的译文.翻译也挺辛苦的!! 6.4 Using Connector/Net with Connection Pooling 6.4在Connector/Net中使用连接池 The Conn ...
- SQLServer服务器数据库之间的数据操作(完整版)
分类: 数据库开发技术 ---------------------------------------------------------------------------------- -- Au ...
- js new Date()
1.Date 对象用于处理日期和时间.创建 Date 对象的语法:var myDate=new Date()Date 对象会自动把当前日期和时间保存为其初始值.2.参数形式有以下5种: new Dat ...
- JQuery动态表格
功能实现:点击添加按钮,表格增加一行并给其name属性赋予的值,方便获取 点击删除,自动删除这一行 JQuery中定义一个count变量 var count = 1; function add() { ...
- Leetcode 100 Same Tree python
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- SQL中 and or优先级问题(转)
刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK_LIBRARY where LIBRARY_ID=1 or LIB_ID=1 and STATUS=3 ...