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/ ...
随机推荐
- [python语法巩固][leetcode326][Power of Three]
题目大意: 让你判断一个int是否为3的幂; 最简单的思路 C++ class Solution { public: bool isPowerOfThree(int n) { for(long lon ...
- Letter of inquiry about employment possibilities, e-mail version
Subject: (logical to recipient!) Inquiry about software engineering position after completion of M.S ...
- SQL server 2008数据库的备份与还原、分离(转)
SQL server 2008数据库的备份与还原.分离(转) 一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Ser ...
- AngularJs中关于ng-class的三种使用方式说明
在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一 ...
- ie8下的透明 问题
团队里经常遇到,索性整理一起 是我们在前端开发中经常遇到的,在问题中经常遇到的两个问题是背景色透明和整体透明 先说下背景色透明,背景色透明,在现代浏览器中,可以用rgba颜色作为背景色. 简单介绍下r ...
- Windows下Redis的安装
1.安装Redis 官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.c ...
- 一步一步学python(六) - 抽象
1.string转数字 import locale locale . atoi( str ) 2.创建函数 函数是可以调用(可能包含参数),执行某种行为并返回一个值 >>>impo ...
- 厂商自定义USB设备固件程序及特性
通过前面的学习,大家应该对USB固件程序结构有了比较深的认识,现在再来详细说说固件里决定设备识别成厂商自定义USB设备的地方有哪些,或者说厂商自定义USB设备的固件特性有哪些. 之前不止一次说过学习U ...
- POJ 2892 Tunnel Warfare (SBT + stack)
题意:给定了初始的状态:有n个村庄连成一条直线,现在有三种操作: 1.摧毁一个村庄 2.询问某个村庄,输出与该村庄相连的村庄数量(包括自己) 3.修复被摧毁的村庄,优先修复最近被摧毁的........ ...
- xcode 5与ios 7的屏幕适配问题
#define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568) float height = DEVICE_ ...