您的位置:首页 > 编程语言 > PHP开发

【七牛】基于PHP 5.6与七牛云存储的图片裁剪

2016-11-21 15:51 351 查看

项目过程中使用到七牛云存储的图片裁剪



基于php5.6的gd2库 制作了此类



注:使用此类之前 



1.你需要有七牛的buket的 appkey 与 appsecret



2.php需要安装GD库 版本2.x以上

3.调用方法 引入此类 调用Cut静态方法即可

<?php
/**
* Created by PhpStorm.
* User: 陈建华
* Date: 2016-6-28
* Time: 16:30
*/
require_once('path_to_php_qiniu_sdk');
namespace core;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

//图片裁剪上传类
class WDLImage
{
const MSG_NOT_SUCCESS = '操作没有成功';
const CODE_NOT_SUCCESS = '701'; //操作没有成功

public static $qiniu_upload_bucket = '';//你的七牛buket名

public static $qiniu_resource_domain = '';//你的七牛buket域名URL

private static $qiniu_appKey = 'xxxxxxxxx';//你的七牛appkey

private static $qiniu_appSecret = 'xxxxxxxxx';//你的七牛 appSecret
//$old_img_url表示旧的七牛图片地址url,$start_x 开始裁剪的图片x坐标 $start_y开始裁剪的y坐标,
//$widthb表示新产生的图片的宽度 $height表示新产生的图片的高度

public static function Cut($old_img_url, $start_x, $start_y, $width, $height)
{
try {
//创建源图的实例
$source = imagecreatefromstring(file_get_contents($old_img_url));
if (!$source) {
throw new Exception('该图片的七牛数据源出错不可用');
} else {
if (!file_exists('./uploadtmp')) { //设置本地缓存文件夹
mkdir('./uploadtmp', 7777);
}
$formatter = pathinfo($old_img_url, PATHINFO_EXTENSION);
$final_img = imagecreatetruecolor((int)$width, (int)$height);
imagecopy($final_img, $source, 0, 0, $start_x, $start_y, $width, $height);
$tmpTargetFileName = './uploadtmp/' . self::genFileName() . '.' . $formatter;
switch ($formatter) {
//创建图片
case strtolower('png'):
imagepng($final_img, $tmpTargetFileName);
break;
case strtolower('jpg'):
imagejpeg($final_img, $tmpTargetFileName);
break;
case strtolower('jpeg'):
imagejpeg($final_img, $tmpTargetFileName);
break;
case strtolower('gif'):
imagegif($final_img, $tmpTargetFileName);
break;
default:
throw new Exception('不支持的图片格式' . $formatter);
}
}
//上传七牛
//销毁内存资源
$new_image_name = self::QiniuNewImgUrl($tmpTargetFileName); //新的图片上传至七牛删除旧的图片返回性的图片urlimageUrl
//返回图片地址
if (unlink($tmpTargetFileName)) {
} else {
throw new Exception('删除本地图片出错');
}
imagedestroy($source);
imagedestroy($final_img);
return $new_image_name;
} catch (Exception $ex) {
return new Exception($ex->getMessage(), self::CODE_NOT_SUCCESS);
}

}

//生成文件名
public static function genFileName()
{
return md5(uniqid('wordoor')) . md5(uniqid('wordoor'));
}

//七牛图片上传和删除
public static function QiniuNewImgUrl($location_file)
{
try {
$oldfile_name = pathinfo($location_file, PATHINFO_BASENAME);
$auth = new Auth(self::$qiniu_appKey, self::$qiniu_appSecret);
$token = $auth->uploadToken(\Yii::$app->params['qiniu_upload_bucket']);
// 初始化 UploadManager 对象并进行文件的上传
$filename = self::genFileName() . '.' . pathinfo($location_file, PATHINFO_EXTENSION);
$uploadMgr = new UploadManager();
$bucketMgr = new BucketManager($auth);
// 调用 UploadManager 的 putFile 方法进行文件的上传
$upresult = $uploadMgr->putFile($token, $filename, $location_file);
//var_dump($upresult);
$new_img_url = self::$qiniu_resource_domain . $upresult[0]['key'];
if ($upresult[1] == null) {
//上传成功之后 删除服务器原图
$delRes = $bucketMgr->delete(self::$qiniu_upload_bucket, $oldfile_name);
} else {
throw new Exception('七牛再次上传图片出错');
}
return $new_img_url;
} catch (Exception $ex) {
return new Exception($ex->getMessage(), self::CODE_NOT_SUCCESS);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 云存储 七牛