PHP图像上传压缩临时文件大小

$name = $_FILES['img']['name'];
$size = $_FILES['img']['size']; //need to compress file size
$type = $_FILES['img']['type'];
$ext = strtolower(pathinfo($_FILES['img']['name'], PATHINFO_EXTENSION));
$temp_name = $_FILES['img']['tmp_name'];

$data = file_get_contents($temp_name);
$img = 'data:image/'.$ext.';base64,'.base64_encode($data); //send this to api

I have a upload image required to compress size from tmp file and send base64 encode to api (after compress I need to check file size again make sure it small than 1mb)

Anyone know how to compress tmp file in php?

Try this:

$img = gzcompress('data:image/'.$ext.';base64,'.base64_encode($data)); //send this to api

Then you'll have to do `gzuncompress() on the other side.