图片上传及压缩,返回图片路径,

//图片上传\

$file = $request->file('pic');
$res = $this->upImg($file);
if($res['code']==0){
	msg('图片上传失败');
}
$data['pic'] = $res['url'];

上传

public function upImg($request){
    //dd($request);//图片信息

    $str = $request->getRealPath();     //图片流即图片的真实路径realpath
    //$ext = $request->getClientOriginalExtension();
    $type = $request->getClientMimeType();     //图片类型
    switch($type){
        case 'image/png':
            $ext='.png';
            break;
        case 'image/jpeg';
            $ext='.jpeg';
            break;
        case 'image/jpeg':
            $ext='.jpg';
            break;
        case 'image/bmp':
            $ext='.bmp';
            break;
        default:
            $ext='.jpg';
    }
    //保存路径
    $file_path='./uploads/recruit/'.date('Ymd').'/'.mt_rand(0,1000).md5(uniqid(microtime(true),true)).$ext;

    if(!file_exists(dirname($file_path))){
        mkdir(dirname($file_path),0777,true);
        Storage::makeDirectory(dirname($file_path));
    }

    $img_content =move_uploaded_file($str,$file_path);

    //压缩图像
    file_put_contents($file_path,($this->image_resize(file_get_contents($file_path), 480, 480)));
    return ['code'=>1,'msg'=>'文件上传成功','url'=>substr($file_path,1)];

}

用法 image_resize(file_get_contents(‘xxx.png’), 100, 100); 获取图片流传入

  /**
     * @param $imagedata    图像数据
     * @param $width        缩放宽度
     * @param $height       缩放高度
     * @param int $per      缩放比例,为0不缩放,>0忽略参数2、3的宽高
     * @return bool|string
    // 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
     */
	    public function image_resize($imagedata, $width, $height, $per = 0) {
	
	
	
	        // 获取图像信息
	        list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata);
	
	        // 缩放比例
	        if ($per > 0) {
	            $width  = $bigWidth * $per;
	            $height = $bigHight * $per;
	        }
	
	        // 创建缩略图画板
	        $block = imagecreatetruecolor($width, $height);
	
	        // 启用混色模式
	        imagealphablending($block, false);
	
	        // 保存PNG alpha通道信息
	        imagesavealpha($block, true);
	
	        // 创建原图画板
	        $bigImg = imagecreatefromstring($imagedata);
	
	        // 缩放
	        imagecopyresampled($block, $bigImg, 0, 0, 0, 0, $width, $height, $bigWidth, $bigHight);
	
	        // 生成临时文件名
	        $tmpFilename = tempnam(sys_get_temp_dir(), 'image_');
	
	        // 保存
	        switch ($bigType) {
	            case 1: imagegif($block, $tmpFilename);
	                break;
	
	            case 2: imagejpeg($block, $tmpFilename);
	                break;
	
	            case 3: imagepng($block, $tmpFilename);
	                break;
	        }
	
	        // 销毁
	        imagedestroy($block);
	
	        $image = file_get_contents($tmpFilename);
	
	        unlink($tmpFilename);
	
	        return $image;
	    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值