Php 使用HttpRequest发送Imagick图像(POST)

Php 使用HttpRequest发送Imagick图像(POST),php,http,post,imagick,Php,Http,Post,Imagick,如何使用http post发送图像而不将图像写入磁盘?下面的代码不起作用 // function returns an Imagick Object $image = $this->generateLvlImage($avatarUrl, $lvl); // I want to send it with http post $postRequest = new \HttpRequest($uploadUrl, \HttpRequest::METH_POST);

如何使用http post发送图像而不将图像写入磁盘?下面的代码不起作用

 // function returns an Imagick Object
    $image = $this->generateLvlImage($avatarUrl, $lvl);

    // I want to send it with http post
    $postRequest = new \HttpRequest($uploadUrl, \HttpRequest::METH_POST);
    $postRequest->addRawPostData('photo='.(string)$image);
    try {
        $postRequest->send();
        if ($postRequest->getResponseCode() === 200) {
            $response = $postRequest->getResponseBody();
        } else {
            throw new \Exception('post.failed');
        }
    } catch (\HttpException $e) {
        throw new \Exception($e->getMessage());
    }

如果不确切知道你想提交给谁/什么,很难确定。通常,您将对图像进行base64_编码,然后在另一端对其进行base64_解码

$postRequest->addRawPostData('photo='.base64_encode($image->getImageBlob()));

编辑:如果你想通过多部分表单数据发送,那么如果不抄袭他人的作品,我无法给你答案,因此这里有一个关于它的教程:

否,我正在将图像发送到vk.com服务器<代码>$postRequest->addPostFile('photo',$imagePath,'image/png')从文件中发送图像,图像的名称应为“photo”。对,那么您很可能希望将其作为多部分表单数据发送,这涉及的内容更多。