基于Laravel封装的图片处理类



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];
    }
}

你可能感兴趣的:(php)