Php convert file to binary

I wanted to try to convert an image to Binary.

I found a script online but it does not work.

Could someone please advise why?


I have an error come up on line 8 and 9

Warning: fread[] expects parameter 1 to be resource

and

Warning: fclose[] expects parameter 1 to be resource,

In this article, we will discuss how to convert a raw image to a binary image in PHP.

First of all, we will discuss what exactly a binary image is.

What is a binary image?

A binary image is a 64bit encoded binary data that takes 33% more data than the original image. It is basically applied to images with less size for faster loading of the image.

How to convert to a binary image in PHP

In PHP, the base64_encode[] method converts the original image to a binary image. It needs the base URL of an image to perform the task.

You may also learn: How To Add text to image in PHP

Syntax:-

base64_encode[$image];

The example is as follows,

Output:-

iVBORw0KGgoAAAANSUhEUgAAAMIAAACpCAMAAABODBKaAAAAOVBMVEX//////+Xd/////+JvOLBUwf+V4f+5gLCVXrD/wssVOMu5////4dhFOLAVXthvwv9FovLdor0VgOWHRjGwAAAABHRSTlMAAAAAs5NmmgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAAAEgAAABIAEbJaz4AAANQSURBVHja7ZzJttwgDESdgRhnxPn/j80iq7YFxqIoeO6qdR/RFxAGJLQskiRJkiRJkiRJkiRJ76FPX8OLvuV/ulb/kqcYzqpHGM+xBVM3EUL4/mMQwM9fAYQQwu/PIwhy/8aFEMIfPkEMWISQJiJwIrDHofRXvAjcpWkLPRCYPn38loEQmFNpD30QiMMQOyHwhiH7TWtGSCyEi3n03gg0Z1i7IdCcIfZDSByCrDf/rbexzohwt3Hbyo1egCPc7z7zE09CsDZInnOXZYe0JG2g3rOGYSBCAiGQTtHWl821nsepEFynlXE3Mjuq84TQoBWFAJuRQnhvhG0mBN9HVQhCEMJ/WQePYQi+k4qFwDn/RyE8CcE681AQrIZTT0tf4AjW8PsQrPGkIFiruXP466akEAzBTv+1xzY8AvC4uFb1BhzB8mbv1UndgMIRrJ7zIpix39OIUhC8l7nmBfPJGBrBDAwkpzE7fn2cSmAEO8zm3iGb8aLjtMQi2AT+i9Aqe0iEXN5CclvMxbxe5hIOwZVChTCKQSgGKluCSzUpNQSEpuNuheX+CG0RvmI+BwmhNTq2D0dojyytoxEA9z5XxvsiYKJ7+0AEVJR4G4aQMABLdl36QKOQm0kfxxey/qwVqbKVB3wXmv2htNFj7ZFSk+Vi7rR2qvUNtWxVy4nHSIQyRsMw7BVGocf/TIJqgiO8DCzjBsOPUPWqjHGP5F9XzcnZ9x4pM5fcATIT4ehaaARQnm3BWPc7VbPjvAiDbrZRibY5W2fHwkd5Yk2zdbIWh3T61dSBqrrMNk7EExdro6QLA1NwgHOyGaFr3BkvYPJHXQLDzAi4hJRhfTcsmQeHMFVK1QMQcIltD8iQFAIb4QHZwkIQwkEPeEXygLc8vpafiUB64/kAhJ7PVIXQguDZIyOTdm8K9erc6oqBD+c9G72BCHZ87G7j89XBuNc+oqBJiy7KbBVd8qqoT+IgXJW1aUFgVebZ+yGw6s5t3RBo9TAvnKEBgeTNy5UzNCDwCudtnRCYdVVjH4TEIygPgxuBWxd27YFALjIc8QjsuraFhdWJMFNlXh/CkFLVGxBhTIXnxfNGDP2qDKGbR3jgS2OsoheBtyeSJEmSJEmSJEmSJEkaqH+bT4RNYLoIXAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wNy0xMFQwOTowNTo1MS0wNTowMGAO7pQAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDctMTBUMDk6MDU6NTEtMDU6MDARU1YoAAAAAElFTkSuQmCC

Note:-

  • In the above script, file_get_contents[] method load an image from a path.
  •  file_get_contents[] method can also load an image from a link.

Example of Image from a path is as follows,

$image_path=file_get_contents['D:/PICS/Pexels/photo-814194.jpeg'];

In this way, we can convert an image to binary from raw image using PHP. If you have any doubts about this article, leave a comment below.

See also,

  • Decimal to Binary conversion in JAVA

Create a Free Account

Sign up to unlock all of IQCode features:

  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge

Sign up Email Password

Or sign up with

By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.

    Table of contents
  • Convert PHP file to binary
  • PHP convert Image file to binary string
  • PHP converting an image to a binary file test
  • How to convert image to binary string using php and file upload?
  • Convert string to binary then back again using PHP
  • Converting string to binary and binary to string with PHP
  • How to convert an image to binary image in PHP?
  • Hex2bin

Convert PHP file to binary

// this needs to match the compiler version that php was compiled with
#define _CRT_SECURE_NO_WARNINGS
#define PHP_COMPILER_ID "VC14"

#pragma once
/* You must include config.w32.h first */
#include "win32/config.w32.h"

#include "php.h"

ZEND_FUNCTION[ReturnString];

zend_function_entry Project1_functions[] =
{
    ZEND_FE[ReturnString, NULL]
    {
        NULL, NULL, NULL
    }
};

zend_module_entry Project1_module_entry =
{
    STANDARD_MODULE_HEADER,
    "Project1 Module",
    Project1_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE[Project1]

ZEND_FUNCTION[ReturnString]
{
    zval* value;

    if [zend_parse_parameters[ZEND_NUM_ARGS[] TSRMLS_CC, "z", &value] == FAILURE]
    {
        RETURN_FALSE;
    }

    convert_to_string[value];

    RETURN_STRING[Z_STRVAL_P[value], true];
}
The script is quite simple:


PHP convert Image file to binary string

CREATE TABLE images [
    image_id int,
    image blob 
    PRIMARY KEY [image_id]];
Filename:
*

Chủ Đề