【七牛】基于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实现七牛抓取远程图片
- 基于php实现七牛抓取远程图片
- PHP 裁剪图片成固定大小代码方法
- php图片处理类:缩略,裁剪,圆角,倾斜
- PHP图片的裁剪与缩放
- 基于PHP服务端图片生成缩略图的方法详解
- 基于php上传图片重命名的6种解决方法的详细介绍
- 基于j2me的图片特效(缩放,裁剪,调整大小,镜像,对比度亮度调整,黑白,线条,粉笔画)
- php图片裁剪扩展Tclip
- 利用PHP结合GD实现图片裁剪和缩略图功能
- php学习笔记(十六)图片的处理缩放、旋转、透明、锐化、翻转、裁剪
- php图片自动裁剪工具,解决图片变形问题,缩略图问题
- php图片处理类:缩略,裁剪,圆角,倾斜
- 让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
- 让php处理图片变得简单 基于gb库的图片处理类附实例代码下载
- php图片的裁剪与缩放生成符合需求的缩略图
- js+php实现的图片选框手动裁剪生成缩略图代码
- php 修改图片的大小、裁剪图片以及生成缩略图方法
- 基于jQuery+HttpHandler实现图片裁剪效果代码(适用于论坛, SNS)
- php全功能无变形图片裁剪