Blame view

library/Plugin/Images.php 6.27 KB
1
<?php
2
3 4 5 6 7
/**
 * User: Zip
 * Date: 15/10/28
 * Time: 下午13:08
 */
8
9
namespace Plugin;
10
11 12
class Images
{
13
14 15 16 17 18 19 20 21 22 23 24 25 26 27
    private static $domain = '.static.yhbimg.com';
    private static $default_image = '/2015/08/25/02/01dd632a6e07bfef457ce4beda21dd6413.png';
    private static $domainList = array(
        '01' => array(
            'img10.static.yhbimg.com',
            'img11.static.yhbimg.com',
        ),
        '02' => array(
            'img12.static.yhbimg.com',
            'img13.static.yhbimg.com'
        ),
        'yhb-head' => 'head.static.yhbimg.com'
    );
    private static $qiniuDomain = 'yhfair.qiniudn.com';
28
29 30 31 32 33 34 35 36 37 38 39 40
    /**
     * 缩略图模板
     * @param $fileName
     * @param $bucket
     * @param string $position ()
     * @param string $background
     * @return string
     */
    static function template($fileName, $bucket = 'yhfair', $mode = 1)
    {
        return self::url($fileName, $bucket, $mode);
    }
41
42 43 44 45 46 47 48 49
    /**
     * 
     * @param unknown $fileName
     * @param string $bucket
     * @return string
     */
    public static function getSourceUrl($fileName, $bucket = 'yhfair')
    {
50
        if (stripos($fileName, 'http://') === 0) {
51 52 53 54 55
            return $fileName;
        }
        $domain = self::getDomain($bucket, $fileName);
        return 'http://' . $domain . '/' . $bucket . $fileName;
    }
56
57 58 59 60 61 62 63 64 65
    /**
     * 根据尺寸获得图片url
     * @param unknown $fileName
     * @param unknown $width
     * @param unknown $height
     * @param number $mode
     * @param string $bucket
     * @return mixed
     */
66
    public static function getImageUrl($fileName, $width, $height, $mode = 2, $bucket = 'goodsimg')
67
    {
68 69 70 71 72 73 74
        if (!is_string($fileName)) {
            return self::template(self::$default_image, $bucket, $mode);
        }
        if (stripos($fileName, 'http://') !== 0) {
            $fileName = self::template($fileName, $bucket, $mode);
        }
        return strtr($fileName, array('{width}' => $width, '{height}' => $height, '{mode}' => $mode));
75
    }
76
77 78 79 80 81 82 83 84 85 86 87 88
    /**
     * 获取图片URL模板
     * @param $fileName
     * @param int $mode
     * @param string $bucket
     * @return string
     */
    public static function url($fileName, $bucket = 'yhfair', $mode = 1)
    {
        $domain = self::getDomain($bucket, $fileName);
        return self::getImgTemplateUrl($bucket . $fileName, $mode, $domain);
    }
89
90 91 92
    public static function getDomain($bucket, $fileName)
    {
        $domain = '';
93
        if (!empty(self::$domainList[$bucket])) {
94
            $domain = self::$domainList[$bucket];
95
        } else {
96 97 98 99 100 101 102 103 104
            $node = mb_substr($fileName, 15, 2);
            if (!empty(self::$domainList[$node])) {
                $domainList = self::$domainList[$node];
                $nodeNum = sprintf('%u', crc32($fileName)) % count($domainList);
                $domain = $domainList[$nodeNum];
            }
        }
        return $domain;
    }
105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

	/**
	 * 图片上传
	 * @param string $name 文件表单name, 即用于$_FILES[$name]
	 */
	public static function saveImage($name)
	{
		if (empty($_FILES[$name]))
		{
			return array();
		}
		$files = $_FILES[$name];
		$images = array();
		if (is_array($files['tmp_name']))
		{
			foreach ($files['tmp_name'] as $k => $tmp_name)
			{
				if(!empty($tmp_name))
				{
					$images[$files['name'][$k]] = $tmp_name;
				}
			}
		}
		else
		{
			$images[$files['name']] = $files['tmp_name'];
		}
133 134

		return self::uploadStreamImage($images);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}

	/**
	 * 上传图片[图片上传域名限制于http://test.service.api.yohobuy.com]
	 *
	 * @param string | array(filename => absolute file path)  $file
	 *   url:http://upload.static.yohobuy.com?project=sns&fileData=xxx
	 * @return mixed
	 */
	public static function uploadStreamImage($file)
	{
		$end ="\r\n";
		$twoHyphens ="--";
		$boundary = "*****";
		$stream = '';
		$files = is_array($file) ? $file : array($file);
		foreach($files as $name => $filename)
		{
			if(!file_exists($filename))
			{
				continue;
			}
			$name = is_numeric($name) ? name.'.jpg' : $name;
			$stream .= $twoHyphens.$boundary.$end;
			$stream .="Content-Disposition: form-data; "."name=\"fileData\";filename=\"".$name ."\"".$end; // form file element name :fileData
			$stream .= $end;
			$stream .= file_get_contents($filename);
			$stream .= $end;
		}
		if(empty($stream))
		{
			return false;
		}
		$stream .= $twoHyphens.$boundary.$end;
		$stream .="Content-Disposition: form-data; "."name=\"project\"".$end;
		$stream .= $end;
171
		$stream .= "suggest";//project sns
172 173 174 175 176 177 178 179 180 181 182 183 184
		$stream .= $end;
		$stream .= $twoHyphens .$boundary .$twoHyphens .$end;
		$opts = array(
			'http' => array(
				'method' => 'POST',
				'header' => 'content-type:multipart/form-data;boundary='.$boundary,
				'content' => $stream
			)
		);
		$context = stream_context_create($opts);
		$result = json_decode(file_get_contents('http://upload.static.yohobuy.com', false, $context), true);
		if(!empty($result['data']['imagesList']))
		{
185
			$imgList = array('imgList'=>array());
186 187
			if(count($file) == 1 || !is_array($file))
			{
188
				$imgList['imgList'][] = array('imgUrl' => self::getSourceUrl(current($result['data']['imagesList']), 'suggest'));
189 190 191
			}
			else
			{
192 193 194 195 196
				$img = array();
				foreach ($result['data']['imagesList'] as $val) {
					$img = array();
					$img['imgUrl'] = self::getSourceUrl($val, 'suggest');
					$imgList['imgList'][] = $img;
197 198
				}
			}
199 200

			return $imgList;
201 202 203 204 205 206 207
		}
		else
		{
			return false;
		}
	}
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    /**
     * 获取模板的图片地址
     * @param $fileName
     * @param int $width
     * @param int $height
     * @param int $mode
     * @param null $domain
     * @return string
     */
    private static function getImgTemplateUrl($fileName, $mode = 1, $domain = null)
    {
        if ($domain == null) {
            $domain = self::$qiniuDomain;
        }
        $baseUrl = self::MakeBaseUrl($domain, $fileName);
        return self::MakeTemplateRequest($baseUrl);
    }
225
226 227 228 229 230
    private static function MakeBaseUrl($domain, $key) // => $baseUrl
    {
        $keyEsc = str_replace("%2F", "/", rawurlencode($key));
        return "http://$domain/$keyEsc";
    }
231
232 233
    private static function MakeTemplateRequest($url)
    {
234
        $ops = array();
235 236 237
        $ops[] = '{mode}';
        $ops[] = 'w/{width}';
        $ops[] = 'h/{height}';
238
239 240
        if (empty($ops)) {
            return $url;
241
        }
242
243
        return $url . "?imageView/" . implode('/', $ops);
244
    }
245 246

}