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

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


基于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);
        }

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值