/** * 七牛上传图片 * file_name 上传文件名 * file_data 文件二进制 **/ function qinuu_upload($file_name, $file_data, $file_to_name = 0) { $qiniu = new erp_qiniu(); //七牛的接口类 $brand_qiniu_account = $qiniu->getAccountByBrand($_SESSION['brandid']); //获取品牌的七牛子账号信息 if (count($brand_qiniu_account) == 0) { throw new Exception('品牌暂未绑定七牛帐号'); } //获取品牌的七牛权限 $auth = new Auth($brand_qiniu_account[0]['access_key'], $brand_qiniu_account[0]['secret_key']); // 生成上传 Token $token = $auth->uploadToken($brand_qiniu_account[0]['space']); //创建七牛资源类 $Upload = new UploadManager(); $ret = $Upload->putFile($token, $file_name, $file_data); return $ret; }
/** * $array 裁剪信息 * {watermark_id 水印ID号,为0代表无水印 * watermark_width 水印图片的宽 * watermark_height 水印图片的高 * watermark_url 水印图片的地址 * watermark_positionx 水印图片x坐标 * watermark_positiony 水印图片y坐标 * patform_width 平台的宽 * patform_height 平台的高 * image_url 推送的图片地址 * cut_positionx 裁剪的x坐标 * cut_positiony 裁剪的y坐标 * cut_width 裁剪的宽 * cut_height 裁剪的高} * $brandid品牌ID */ public function cut_image($array, $brandid) { $ret = array('status' => 0, 'mag' => '', 'url' => "", 'qiniu_array' => array()); if (count($array) == 0) { return $ret; } else { if (empty($array['image_url']) || empty($array['patform_width']) || empty($array['patform_height'])) { return $ret; } } $back_array = array(); $memcache = md5('qiniucount' . $brandid); $boo = Yii::app()->cache->get($memcache); $qiniu_array = array(); if (empty($boo)) { $erp_qiniu = new erp_qiniu(); $qiniu_array = $erp_qiniu->getAccountByBrand($brandid); $output = json_encode($qiniu_array); Yii::app()->cache->set($output, $memcache, 18000); //添加缓存 } else { $qiniu_array = json_decode($boo, true); } if (count($qiniu_array) > 0) { $type = ".jpg"; if (strpos($array['image_url'], ".png")) { $type = ".png"; } $background_y_url = $qiniu_array[0]['domain'] . "background" . $type; } else { return $ret; } //将背景放大到平台设置的尺寸 $background_m_url = $background_y_url . "?imageMogr2/thumbnail/{$array['patform_width']}x{$array['patform_height']}!"; $cut_y_width = $array['cut_width']; //数据库记录的图片裁剪宽 $cut_y_height = $array['cut_height']; //数据库记录的图片裁剪高 $array['cut_width'] = $array['cut_width'] == 0 ? $array['img_w'] : $array['cut_width']; $array['cut_height'] = $array['cut_height'] == 0 ? $array['img_h'] : $array['cut_height']; //通过原始坐标和宽高计算在原图裁剪具体的位置及宽高 $cut_m_positionx = $array['cut_positionx'] < 0 ? 0 : $array['cut_positionx']; $cut_m_positiony = $array['cut_positiony'] < 0 ? 0 : $array['cut_positiony']; $cut_m_width = $array['cut_width']; //需要裁剪的宽 $cut_m_height = $array['cut_height']; //需要裁剪的高 //echo $cut_m_positionx."==".$cut_m_positiony."==".$cut_m_width."==".$cut_m_height; //通过裁剪坐标及宽高对原图进行裁剪 $cut_m_url = $qiniu_array[0]['domain'] . $array['image_url'] . "?imageMogr2/crop/!{$cut_m_width}x{$cut_m_height}a{$cut_m_positionx}a{$cut_m_positiony}"; $image_width = $array['img_w']; //原图宽 $image_height = $array['img_h']; //原图高 $zoom_ratio = 1; $image_width_cut_ratio = $array['patform_width'] / $cut_m_width; //平台设置的宽和需要裁剪的宽之间的比例 $image_height_cut_ratio = $array['patform_height'] / $cut_m_height; if ($cut_m_width > $array['patform_width'] || $cut_m_height > $array['patform_height']) { $zoom_ratio = $image_width_cut_ratio < $image_height_cut_ratio ? $image_width_cut_ratio : $image_height_cut_ratio; } $a = $zoom_ratio * 100; $cut_m_url = $cut_m_url . "|imageMogr2/thumbnail/!{$a}p"; //echo $cut_m_url."\n\r"; $dx = $array['cut_positionx'] < 0 ? round(abs($array['cut_positionx']) * $zoom_ratio) : 0; $dy = $array['cut_positiony'] < 0 ? round(abs($array['cut_positiony']) * $zoom_ratio) : 0; if ($cut_y_width == 0 && $cut_y_height == 0) { $cut_y_width = round($cut_m_width * $zoom_ratio); $cut_y_height = round($cut_m_height * $zoom_ratio); $dx = round(($array['patform_width'] - $cut_y_width) / 2); $dy = round(($array['patform_height'] - $cut_y_height) / 2); } $cut_m_64_url = \Qiniu\base64_urlSafeEncode($cut_m_url); //http://developer.qiniu.com/resource/gogopher.jpg?imageMogr2/thumbnail/200x300! //$water_url=$baiyi_domin.$array['watermark_url']"?imageMogr2/thumbnail/200x300!"; $cut_m_url = $background_m_url . "|watermark/3/image/{$cut_m_64_url}/gravity/NorthWest/dx/{$dx}/dy/{$dy}"; if (isset($array['watermark_id']) && $array['watermark_id'] > 0 && !empty($array['watermark_url']) && !empty($array['watermark_width']) && !empty($array['watermark_height'])) { $baiyi_domin = "http://erp.beubeu.com/"; $water_url = \Qiniu\base64_urlSafeEncode($baiyi_domin . $array['watermark_url']); $watermark_url = getimagesize($baiyi_domin . $array['watermark_url']); $weight = $watermark_url["0"]; ////获取水印图片的宽 $height = $watermark_url["1"]; ///获取水印图片的高 $back_warter_url = $background_y_url . "?imageMogr2/thumbnail/{$weight}x{$height}!"; $back_warter_url = $back_warter_url . "|watermark/3/image/{$water_url}/gravity/NorthWest/dx/0/dy/0|imageMogr2/thumbnail/{$array['watermark_width']}x{$array['watermark_height']}!"; $water_url = \Qiniu\base64_urlSafeEncode($back_warter_url); $water_dx = $array['watermark_positionx'] > 0 ? $array['watermark_positionx'] : 0; $water_dy = $array['watermark_positiony'] > 0 ? $array['watermark_positiony'] : 0; $cut_m_url .= "|watermark/3/image/{$water_url}/gravity/NorthWest/dx/{$water_dx}/dy/{$water_dy}"; } $ret['qiniu_array'] = $qiniu_array; $ret['url'] = $cut_m_url; $ret['status'] = 1; return $ret; }