/** * Encrypt an image file, upload the blob and send the image message to the threemaId * * @param string $threemaId * @param string $imagePath * @return \Threema\MsgApi\Commands\Results\SendE2EResult * @throws \Threema\Core\Exception */ public final function sendImageMessage($threemaId, $imagePath) { //analyse the file $fileAnalyzeResult = FileAnalysisTool::analyse($imagePath); if (null === $fileAnalyzeResult) { throw new Exception('could not analyze the file'); } if (false === in_array($fileAnalyzeResult->getMimeType(), array('image/jpg', 'image/jpeg', 'image/png'))) { throw new Exception('file is not a jpg or png'); } //fetch the public key $receiverPublicKey = $this->fetchPublicKeyAndCheckCapability($threemaId, function (CapabilityResult $capabilityResult) { return true === $capabilityResult->canImage(); }); //encrypt the image file $encryptionResult = $this->cryptTool->encryptImage(file_get_contents($imagePath), $this->privateKey, $receiverPublicKey); $uploadResult = $this->connection->uploadFile($encryptionResult->getData()); if ($uploadResult === null || !$uploadResult->isSuccess()) { throw new Exception('could not upload the image (' . $uploadResult->getErrorCode() . ' ' . $uploadResult->getErrorMessage() . ') ' . $uploadResult->getRawResponse()); } $nonce = $this->cryptTool->randomNonce(); //create a image message box $imageMessage = $this->cryptTool->encryptImageMessage($uploadResult, $encryptionResult, $this->privateKey, $receiverPublicKey, $nonce); return $this->connection->sendE2E($threemaId, $nonce, $imageMessage); }