基于Laravel封装的图片处理类

<?php

use Intervention\Image\ImageManagerStatic as Image;

class Util
{
    
    /**
     * 截图图片
     * @param $imagePath
     * @param string $imageMode
     * @param string $newImageName
     * @param string $newImagePath
     * @return string
     */
    public function imageProcessing($imagePath, $imageMode = '', $newImageName = '', $newImagePath = '')
    {
        try {
            if (!$imagePath) return '';

            //获取图片信息
            $imageInfo = getimagesize($imagePath);
            $width = $imageInfo['0'];
            $height = $imageInfo['1'];

            //通过图片类型获取图片比例
            list($width, $ratio) = $this->imageRatio($width, $imageMode);

            //图片高拉伸
            $newHeight = $width / $ratio;

            if ($newHeight == $height) {
                return $imagePath;
            }

            //图片后缀
            $pathTmp = explode('/', $imageInfo['mime']);
            $suffix = '.' . ltrim(end($pathTmp), '.');

            //新图片名字为空时,生成新图片名称
            if (!$newImageName) {
                $newImageName = uniqid() . $suffix;
            }

            //保存位置
            if (!$newImagePath) {
                $newImagePath = public_path() . '/upload_image/';
                @mkdir($newImagePath, 0777, true);
            }

            // 修改指定图片的大小
            $img = Image::make($imagePath)->resize($width, $newHeight);

            $path = $newImagePath . $newImageName;

            // 将处理后的图片重新保存到其他路径
            $img->save($path);

            return $path;
        } catch (\Exception $e) {
            \Log::error('图片裁剪出错:' . $e->getMessage());

            return $imagePath;
        }
    }

    /**
     * @param $width
     * @param string $imageMode
     * @return array
     */
    private function imageRatio($width, $imageMode = '')
    {
        $ratio = 1;
        $min_width = $width;
        $max_width = $width;

        switch ($imageMode) {
            case EnumList::IMAGE_MODE_LARGE:
                $min_width = 600;
                $max_width = 2400;
                $ratio = 1.91;
                break;
            case EnumList::IMAGE_MODE_LARGE_VERTICAL:
                $min_width = 187;
                $max_width = 750;
                $ratio = 0.62;
                break;
            case EnumList::IMAGE_MODE_BIG_ONE:
                $min_width = 320;
                $max_width = 1280;
                break;
            case EnumList::IMAGE_MODE_VIDEO:
                $min_width = 1280;
                $max_width = 2560;
                $ratio = 1.78;
                break;
            case EnumList::IMAGE_MODE_VIDEO_SQUARE:
                $min_width = 640;
                $max_width = 1280;
                break;
            case EnumList::IMAGE_MODE_VIDEO_VERTICAL:
                $min_width = 720;
                $max_width = 1440;
                $ratio = 0.56;
                break;

        }

        if ($width > $max_width) {
            $width = $max_width;
        } else if ($width < $min_width) {
            $width = $min_width;
        }

        return [$width, $ratio];
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值