•  0
    PHP

    Convert jgp image in base64 string to bitmap image

      Admin     2231        0        Report content

    Simple functions to convert jpeg image in base64 string to bitmap image

    
    function base64_to_bmp($base64_string) {
    	$bmpfile = 'filename.bmp';
    	if(file_exists($bmpfile)){		
    		unlink($bmpfile);
    	}
    	$imageSource = imagecreatefromstring(base64_decode($base64_string));
    	imagebmp($imageSource,$bmpfile);
    }
    function imagebmp(&$im, $filename = ""){
        if (!$im) return false;
        $w = imagesx($im);
        $h = imagesy($im);
        $result = '';
        if (!imageistruecolor($im)) {
            $tmp = imagecreatetruecolor($w, $h);
            imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h);
            imagedestroy($im);
            $im = & $tmp;
        }
        $biBPLine = $w * 3;
        $biStride = ($biBPLine + 3) & ~3;
        $biSizeImage = $biStride * $h;
        $bfOffBits = 54;
        $bfSize = $bfOffBits + $biSizeImage;
        $result .= substr('BM', 0, 2);
        $result .=  pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
        $result .= pack ('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
        $numpad = $biStride - $biBPLine;
        for ($y = $h - 1; $y >= 0; --$y) {
            for ($x = 0; $x < $w; ++$x) {
                $col = imagecolorat ($im, $x, $y);
                $result .=  substr(pack ('V', $col), 0, 3);
            }
            for ($i = 0; $i < $numpad; ++$i)
                $result .= pack ('C', 0);
        }
        if($filename==""){
            echo $result;
        }
        else
        {
            $file = fopen($filename, "wb");
            fwrite($file, $result);
            fclose($file);
        }
        return true;
    }
    

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations