Search code examples
phpimageimagickresize-image

no decode delegate for this image format - Imagick - php


$path=getcwd();
$rawstring = $_POST['img_data'];    
$malestr = str_replace("data:image/jpeg;base64,", "", $rawstring);
$img = new Imagick();
$img->readImageBlob($malestr);
$img->writeImage($path."/media/import/new.jpg");  

This is my Imagick functions, I am trying to write an image using base64 encoded data. Its throwing error .

"no decode delegate for this image format"


Solution

  • According to manual, Imagick::readImageBlob needs a binary string, while you are providing a base64 encoded string to it. Decode the string at first with function base64_decode and give the result to readImageBlob.

    // your code above
    $malestr = str_replace("data:image/jpeg;base64,", "", $rawstring);
    
    $malestr = base64_decode($malestr);
    if (!$malestr) die('Unable to decode the string');
    
    $img = new Imagick();
    $img->readImageBlob($malestr);