PHP – Save an Image File from Base64 String

This post shows you how to save an image file from a Base64 data string. Here are two approaches: using Regular Expression and using explode() function to extract image data from a Base64 string.

 

1. Using Regular Expression

example1.php
<?php

$base64DataString = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCABQAFADASIAAhEBAxEB/8QAHAAAAgMBAQEBAAAAAAAAAAAAAAgBBwkGAgQD/8QANxAAAQMDAgQDBQYGAwAAAAAAAQIDBAAFEQYHCBIhMRNBURQyQmGBCSJxgpGhFTNykrTDVHSi/8QAGgEAAgMBAQAAAAAAAAAAAAAABAUDBgcACP/EADQRAAAEAwUECAYDAAAAAAAAAAABAgQDERIFBhMxkSFhcfAUFRZRobHB0QcXMkFCUiIzgf/aAAwDAQACEQMRAD8AqKiiivRA8xAqPy1NFcOBUetMJw2cKEze62ydWX6+u2ewMPmM0WGgt+U4ACvk5uiUjIGSDk5GOhq4r39nLp1xgnTm5dzjvcvQTYTb6T/YW8Ugc3ms5pGOBEXtLPYZy0Fha3VtN5AJxBh/xPLaRT1MI1RVx7qcKO7u1jL1yl2pq92hrKlTrWS6GkerjZAWj5nBA9apsHIzTVq9bvkYkBRKLcFDxi5Yrw3CDSe8Hn9KPSpoooCgooq5+ErayPunu/Bj3OOHbRY0G6T0qGUuchAbbPqFLKcjzSlVCvXSGLdUdeSSmCmLRb9yhvDzUchdfDdwU2C96Zj613jhS31XJCXYVnS84wGmT1DjxQQvnV3CQRgd8k4TwPFlw66Q2tYY1Zt/7RHty5IiS4DzxeDRWCUuNqXleOmCCT3GK0Fvl0btNuU/nB5elZ1cWe9CNZXpzb+zKC4lslh2fIB6OSEAgND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/tpTac3Rq6rRPvPzMIr609brp7inoQj81XPwlbqR9rN34Mi6Sg1aL4g2ucpRwlvxCC24fQJWE5PklSqpmoIzT161hvm6m68lFIV1i7WwcocQ80nMbR3CG3PjFtQByOlIzxR8J9/N5mbi7b2tc1MtReudrYRl3xPN5lPx57lI656jOTjpuE7i3gy7dC2w3QuaY82MlMe1XWQvCJKB0Sy6o9nB0AWeihgH7/vOCHockdVJrIUqe3Ye5bfBRc6DbTJhe5gW33SfP+GMXXWnWHlsSG1tOtkocbWOVSSO4IPY1LDL0l5uNGacdddWEtttgqUonsAB3Na/ak2u211g57TqnQ9huz+MB6ZAadcA/rUM/vU6b20210U57TpfRFgtD2MeLEgtNOY/qSM/vVo7dw8P+k6uOzy9BUfl3FxJYxU8Nuk/UKhwk8JV6g3mFulujbFw/Ylh+02mQjDvi/C+8n4OXulJ65wTjHVudY3tq125aAsc6x2zX6XnVcC2MKCHQpePWkf4neJpFwEvQ+iLiH5TuWZ89lWUx0di22R3cPYke7+ParGb2874jMtvgkudRb0pYXSYGU/dR86CluIPX7e4W5k2ZDkeLbrWPYIqwejnISXHB+KycHzAFVx+aoSkISEpGAK9VrzFollATARkkpDEn7xb9ytxEzUcwUUUUWBB5IyPvVb+2nFNu1tnHatca7IvVpaAS3CuYLvhI9G3AQtA9BkgelVFRQrpm3epojpJRbwUzfOWK8Rus0nuDlWv7QOAtgfxvQVxju46+yTW3kk/nCcV8l84+IjrKk2TQ9xecI6e1S22QP7AqlAopJ2RsuqdB6n7ixdtLXppxC4yL2FmbicRe524yHYUy5otVtd6LiW/LfiD0ccJKz8xkA+lVilKUjlCcD5V6op20ZQGSKICSSW4V54/cvl4jhZqPeCiiiigIP/Z';

// extract image data from base64 data string
$pattern = '/data:image\/(.+);base64,(.*)/';
preg_match($pattern, $base64DataString, $matches);

// image file extension
$imageExtension = $matches[1];

// base64-encoded image data
$encodedImageData = $matches[2];

// decode base64-encoded image data
$decodedImageData = base64_decode($encodedImageData);

// save image data as file
file_put_contents("image.{$imageExtension}", $decodedImageData);

In this approach, we use a regex pattern to extract image data from the Base64 data string and then save it as an image file using file_put_contents() function.

 

2. Using explode() function

example2.php
<?php

$base64DataString = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCABQAFADASIAAhEBAxEB/8QAHAAAAgMBAQEBAAAAAAAAAAAAAAgBBwkGAgQD/8QANxAAAQMDAgQDBQYGAwAAAAAAAQIDBAAFEQYHCBIhMRNBURQyQmGBCSJxgpGhFTNykrTDVHSi/8QAGgEAAgMBAQAAAAAAAAAAAAAABAUDBgcACP/EADQRAAAEAwUECAYDAAAAAAAAAAABAgQDERIFBhMxkSFhcfAUFRZRobHB0QcXMkFCUiIzgf/aAAwDAQACEQMRAD8AqKiiivRA8xAqPy1NFcOBUetMJw2cKEze62ydWX6+u2ewMPmM0WGgt+U4ACvk5uiUjIGSDk5GOhq4r39nLp1xgnTm5dzjvcvQTYTb6T/YW8Ugc3ms5pGOBEXtLPYZy0Fha3VtN5AJxBh/xPLaRT1MI1RVx7qcKO7u1jL1yl2pq92hrKlTrWS6GkerjZAWj5nBA9apsHIzTVq9bvkYkBRKLcFDxi5Yrw3CDSe8Hn9KPSpoooCgooq5+ErayPunu/Bj3OOHbRY0G6T0qGUuchAbbPqFLKcjzSlVCvXSGLdUdeSSmCmLRb9yhvDzUchdfDdwU2C96Zj613jhS31XJCXYVnS84wGmT1DjxQQvnV3CQRgd8k4TwPFlw66Q2tYY1Zt/7RHty5IiS4DzxeDRWCUuNqXleOmCCT3GK0Fvl0btNuU/nB5elZ1cWe9CNZXpzb+zKC4lslh2fIB6OSEAgND5Jycn16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Rq6rRPvPzMIr609brp7inoQj81XPwlbqR9rN34Mi6Sg1aL4g2ucpRwlvxCC24fQJWE5PklSqpmoIzT161hvm6m68lFIV1i7WwcocQ80nMbR3CG3PjFtQByOlIzxR8J9/N5mbi7b2tc1MtReudrYRl3xPN5lPx57lI656jOTjpuE7i3gy7dC2w3QuaY82MlMe1XWQvCJKB0Sy6o9nB0AWeihgH7/vOCHockdVJrIUqe3Ye5bfBRc6DbTJhe5gW33SfP+GMXXWnWHlsSG1tOtkocbWOVSSO4IPY1LDL0l5uNGacdddWEtttgqUonsAB3Na/ak2u211g57TqnQ9huz+MB6ZAadcA/rUM/vU6b20210U57TpfRFgtD2MeLEgtNOY/qSM/vVo7dw8P+k6uOzy9BUfl3FxJYxU8Nuk/UKhwk8JV6g3mFulujbFw/Ylh+02mQjDvi/C+8n4OXulJ65wTjHVudY3tq125aAsc6x2zX6XnVcC2MKCHQpePWkf4neJpFwEvQ+iLiH5TuWZ89lWUx0di22R3cPYke7+ParGb2874jMtvgkudRb0pYXSYGU/dR86CluIPX7e4W5k2ZDkeLbrWPYIqwejnISXHB+KycHzAFVx+aoSkISEpGAK9VrzFollATARkkpDEn7xb9ytxEzUcwUUUUWBB5IyPvVb+2nFNu1tnHatca7IvVpaAS3CuYLvhI9G3AQtA9BkgelVFRQrpm3epojpJRbwUzfOWK8Rus0nuDlWv7QOAtgfxvQVxju46+yTW3kk/nCcV8l84+IjrKk2TQ9xecI6e1S22QP7AqlAopJ2RsuqdB6n7ixdtLXppxC4yL2FmbicRe524yHYUy5otVtd6LiW/LfiD0ccJKz8xkA+lVilKUjlCcD5V6op20ZQGSKICSSW4V54/cvl4jhZqPeCiiiigIP/Z';

list($dataType, $imageData) = explode(';', $base64DataString);

// image file extension
$imageExtension = explode('/', $dataType)[1];

// base64-encoded image data
list(, $encodedImageData) = explode(',', $imageData);


// decode base64-encoded image data
$decodedImageData = base64_decode($encodedImageData);

// save image data as file
file_put_contents("image.{$imageExtension}", $decodedImageData);

In this approach, we use explode() function to extract image data from the Base64 data string and then save it as an image file using file_put_contents() function.

Leave a Reply

avatar