| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace common\components;
- use Yii;
- class imgUtil
- {
- //获取图片路径的前缀 ssh 2021.3.12
- public static function getPrefix()
- {
- //$style 1花店 2供货商 3商城
- $style = Yii::$app->params['ptStyle'];
- if ($style == 2) {
- return Yii::$app->params['ghsImgHost'];
- } elseif ($style == 1) {
- return Yii::$app->params['hdImgHost'];
- } elseif ($style == 3) {
- return Yii::$app->params['mallImgHost'];
- } else {
- return Yii::$app->params['hdImgHost'];
- }
- }
- //组合出图片路径 ssh 2021.4.9
- public static function groupImg($pic)
- {
- $prefix = self::getPrefix();
- if (empty($pic)) {
- return $prefix . 'hhb_small.png';
- }
- return $prefix . $pic;
- }
- /**
- * OSS 图片水印用的对象路径(桶内相对路径,不要带域名)
- * xhGoods.cover 常为 default-img.png,实际对象在 retail/default-img.png
- */
- public static function normalizeOssObjectPath($pic)
- {
- $pic = trim((string)$pic);
- if ($pic === '') {
- return 'retail/default-img.png';
- }
- if (strpos($pic, 'http://') === 0 || strpos($pic, 'https://') === 0) {
- $pic = ltrim(parse_url($pic, PHP_URL_PATH) ?? '', '/');
- }
- $pic = ltrim($pic, '/');
- if ($pic === 'default-img.png' || $pic === 'hhb_small.png') {
- return 'retail/default-img.png';
- }
- return $pic;
- }
- /**
- * 通过 OSS image/info 读取图片宽高(用于海报动态画布)
- *
- * @return array{0:int,1:int} [width, height]
- */
- public static function getOssImageSize($objectPath, $prefix = null)
- {
- $objectPath = self::normalizeOssObjectPath($objectPath);
- if ($prefix === null || $prefix === '') {
- $prefix = self::getPrefix();
- }
- $infoUrl = rtrim($prefix, '/') . '/' . $objectPath . '?x-oss-process=image/info';
- $context = stream_context_create([
- 'http' => [
- 'timeout' => 5,
- 'ignore_errors' => true,
- ],
- ]);
- $json = @file_get_contents($infoUrl, false, $context);
- if ($json === false || $json === '') {
- return [0, 0];
- }
- $data = json_decode($json, true);
- if (!is_array($data)) {
- return [0, 0];
- }
- $w = (int)($data['ImageWidth']['value'] ?? 0);
- $h = (int)($data['ImageHeight']['value'] ?? 0);
- return [$w, $h];
- }
- }
|