Skip to content

Media

Media travels as Pam\WhatsApp\MessageMedia: a MIME type, base64 data, and an optional file name and size. You can build it from a file or a URL and send it like any other content, or use the one-call helpers that resolve a phone number and send a file in one step.

Each helper normalizes the phone number, verifies it is on WhatsApp, reads the file, and sends it:

$phone = '+1 555 555 0123';
$client->sendImageToNumber($phone, __DIR__.'/storage/receipt.jpg', 'Your receipt');
$client->sendAudioToNumber($phone, __DIR__.'/storage/greeting.ogg'); // voice note by default
$client->sendAudioToNumber($phone, __DIR__.'/storage/track.mp3', asVoiceNote: false);
$client->sendVideoToNumber($phone, __DIR__.'/storage/demo.mp4', 'Product demo');
$client->sendVideoToNumber($phone, __DIR__.'/storage/loop.mp4', asGif: true);
$client->sendDocumentToNumber($phone, __DIR__.'/storage/invoice.pdf', 'Invoice 1042');
$client->sendStickerToNumber($phone, __DIR__.'/storage/sticker.webp', 'Acme pack', 'Acme');

They all return the sent Message and throw ContactNotFoundException when the number is not registered.

Use MessageMedia with sendMessage() when you already have a chat ID or need full control over the options:

use Pam\WhatsApp\MessageMedia;
use Pam\WhatsApp\MessageSendOptions;
$media = MessageMedia::fromFilePath(__DIR__.'/storage/report.pdf');
$client->sendMessage($chatId, $media, new MessageSendOptions(
caption: 'Monthly report',
sendMediaAsDocument: true,
));

Fetch media over HTTP, HTTPS, or a data: URL with fromUrl():

use Pam\WhatsApp\MediaFromURLOptions;
$media = MessageMedia::fromUrl('https://example.com/banner.png', new MediaFromURLOptions(
filename: 'banner.png',
));

Or construct it from bytes you generated in memory:

$media = new MessageMedia('image/png', base64_encode($pngBytes), 'chart.png');

Check hasMedia, then call downloadMedia(). It returns a MessageMedia whose data is base64:

use Pam\WhatsApp\Event\MessageReceived;
$client->onMessage(static function (MessageReceived $event): void {
$message = $event->message;
if (!$message->hasMedia) {
return;
}
$media = $message->downloadMedia();
if ($media !== null) {
$name = $media->filename ?? $message->id->id;
file_put_contents(__DIR__.'/storage/inbox/'.basename($name), base64_decode($media->data, true));
}
});

downloadMedia() returns null when the message has no media or WhatsApp Web can no longer provide it.

downloadMedia() holds the whole file in memory. For videos and large documents, stream it in chunks instead:

use Pam\WhatsApp\MediaStreamOptions;
$download = $message->downloadMediaStream(new MediaStreamOptions(
chunkSize: 1024 * 1024, // 1 MiB per chunk; default 10 MiB
));
if ($download !== null) {
$file = fopen(__DIR__.'/storage/inbox/video.mp4', 'wb');
foreach ($download->stream as $chunk) {
fwrite($file, $chunk);
}
fclose($file);
}

The result also exposes mimetype, filename, and filesize. Chunks are fetched lazily as you iterate.

use Pam\WhatsApp\MessageMedia;
$url = $client->getProfilePicUrl('[email protected]'); // ?string
$client->setProfilePicture(MessageMedia::fromFilePath(__DIR__.'/logo.jpg'));
$client->deleteProfilePicture();

Groups have their own setPicture() and deletePicture() methods on GroupChat.

WhatsApp Web can download incoming media automatically. Turn that on or off per type for the linked device:

$client->setAutoDownloadPhotos(false);
$client->setAutoDownloadVideos(false);
$client->setAutoDownloadAudio(false);
$client->setAutoDownloadDocuments(false);