Base64 to Image Convert :: PHP CI

0
1425

When we are trying to save our base64 code in Database then it takes some space and if we have to replace some sort of images time to time like for Product or User images then their is hurdle.

So, Here is the solution for that :-

public function testImage()
{

    define('UPLOAD_DIR', 'images/');

    $base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAArRJREFUeNrEU0tIVFEY/s+9d2bu3Ovc6zQ6o1POqI1jpA2CURhCVChRgoZlVpvITYvCZdTGZYsemwqCHhRELWohUShEkj10UVQkhpqmmCPovB/OzH2cczpGC3HRxkU/fAfOd/7/4/B95yBKKWykONhgCZfPyuu5AEMdwRoniJ6haFJLO4TYAYtNUhk/xjD1rxu0MBwi2ChzKCVf6vf3VXwMN96VN7fO8kjjCTEOs/Mja+fWCuxiaOaIQa2AB/Z13ZibnNNPiTb+oLfuTIPi755xOFxJoKaX9bWtF7AwhDhKjBwvTb+tvziflUJqPhaOnexsv+ZyKkt5d4eKy3sqNOJwISC1rF/948FfATeDz0oK0UHfeeWN53TfxKc5Ly5p9OHpDLXNpkIJzvs9kHJEOzjdRwGtRudb9UQQuRxQimRMEMdozLxzQjzZNW2VtkOxAhBJAKQKAPnUjNdMPLTZM8sm4ryAuDKKuDFhMNoJsi1JNaWyOVMclJLI80OIZ3tlBzwWRFIqmAZg0yxkcvhclq9tfZG7dELS5tOQWX4+sxgAYThxtLqtperljq0lNT/DYVAXBp2Y2H1NdfWKt5hZU6pCKq7zr99P9PnpuCpuagoEq7stkYXos5Gp8XahDOV6ghKtKURyoE7dHt6rPXk0JTSo2eS9kIHABmkd9DQ298D9VyF5KD2T3L2zELlyvELhSrcUQa+Q+JUevXpzFAxRglC5x+3zObdl8zb3g4FxhVgE4BYzAGliP+bHjWlR/fZ10l/57ukIWLQVEAE+IDV4HZBJOkBAF0xRrLLJhqGL0gQWhSCycjK/lKOQNU0o4M92I1NfSFkkXtPCbH8LAdwRgK0shn4waD97ba48KQLJHosZRpHTJDIPOgakmxQMM6avWFQ7V7Bik8QNijDPrQb6v3/jbwEGANnfKx797zwsAAAAAElFTkSuQmCC";

    $image_parts = explode(";base64,", $base64);
    $image_type_aux = explode("image/", $image_parts[0]);

    print_r($image_type_aux);

    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);

    print_r($image_parts);

    echo $image_base64;

    $file_name = uniqid() . '.'.$image_type_aux[1];;

    $uploadPath = 'uploads'.DIRECTORY_SEPARATOR.'Gallery'.DIRECTORY_SEPARATOR . $file_name;

    file_put_contents($uploadPath, $image_base64);


}

  }

You can pass as an array or do necessary changes as per requirement.

LEAVE A REPLY

Please enter your comment!
Please enter your name here