How to convert image path to base64 encode data using PHP












0















I need to convert the image URL to base64_encode data using PHP. I am explaining my code below.



<?php
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
?>


Here I am getting the blank output. Please help me to resolve this issue.










share|improve this question

























  • Enable the error logs. This path does not exist

    – pr1nc3
    Nov 19 '18 at 12:46













  • I have changed the original path. In my case path is correct.

    – subhra
    Nov 19 '18 at 12:48











  • The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

    – pr1nc3
    Nov 19 '18 at 12:51













  • Duplicate of stackoverflow.com/questions/3967515/…

    – akshaypjoshi
    Nov 19 '18 at 14:12











  • Possible duplicate of How to convert an image to base64 encoding?

    – akshaypjoshi
    Nov 19 '18 at 14:12
















0















I need to convert the image URL to base64_encode data using PHP. I am explaining my code below.



<?php
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
?>


Here I am getting the blank output. Please help me to resolve this issue.










share|improve this question

























  • Enable the error logs. This path does not exist

    – pr1nc3
    Nov 19 '18 at 12:46













  • I have changed the original path. In my case path is correct.

    – subhra
    Nov 19 '18 at 12:48











  • The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

    – pr1nc3
    Nov 19 '18 at 12:51













  • Duplicate of stackoverflow.com/questions/3967515/…

    – akshaypjoshi
    Nov 19 '18 at 14:12











  • Possible duplicate of How to convert an image to base64 encoding?

    – akshaypjoshi
    Nov 19 '18 at 14:12














0












0








0








I need to convert the image URL to base64_encode data using PHP. I am explaining my code below.



<?php
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
?>


Here I am getting the blank output. Please help me to resolve this issue.










share|improve this question
















I need to convert the image URL to base64_encode data using PHP. I am explaining my code below.



<?php
$path='https://www.jquery-az.com/html/images/banana.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64=base64_encode($data);
print_r($base64);exit;
?>


Here I am getting the blank output. Please help me to resolve this issue.







php base64






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 12:49







subhra

















asked Nov 19 '18 at 12:41









subhrasubhra

1,34211134




1,34211134













  • Enable the error logs. This path does not exist

    – pr1nc3
    Nov 19 '18 at 12:46













  • I have changed the original path. In my case path is correct.

    – subhra
    Nov 19 '18 at 12:48











  • The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

    – pr1nc3
    Nov 19 '18 at 12:51













  • Duplicate of stackoverflow.com/questions/3967515/…

    – akshaypjoshi
    Nov 19 '18 at 14:12











  • Possible duplicate of How to convert an image to base64 encoding?

    – akshaypjoshi
    Nov 19 '18 at 14:12



















  • Enable the error logs. This path does not exist

    – pr1nc3
    Nov 19 '18 at 12:46













  • I have changed the original path. In my case path is correct.

    – subhra
    Nov 19 '18 at 12:48











  • The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

    – pr1nc3
    Nov 19 '18 at 12:51













  • Duplicate of stackoverflow.com/questions/3967515/…

    – akshaypjoshi
    Nov 19 '18 at 14:12











  • Possible duplicate of How to convert an image to base64 encoding?

    – akshaypjoshi
    Nov 19 '18 at 14:12

















Enable the error logs. This path does not exist

– pr1nc3
Nov 19 '18 at 12:46







Enable the error logs. This path does not exist

– pr1nc3
Nov 19 '18 at 12:46















I have changed the original path. In my case path is correct.

– subhra
Nov 19 '18 at 12:48





I have changed the original path. In my case path is correct.

– subhra
Nov 19 '18 at 12:48













The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

– pr1nc3
Nov 19 '18 at 12:51







The above code returns the result in base64_encoding format. I don't get what's the error. Enable your error log. Blank page is never "blank" there are errors that are hidden there.

– pr1nc3
Nov 19 '18 at 12:51















Duplicate of stackoverflow.com/questions/3967515/…

– akshaypjoshi
Nov 19 '18 at 14:12





Duplicate of stackoverflow.com/questions/3967515/…

– akshaypjoshi
Nov 19 '18 at 14:12













Possible duplicate of How to convert an image to base64 encoding?

– akshaypjoshi
Nov 19 '18 at 14:12





Possible duplicate of How to convert an image to base64 encoding?

– akshaypjoshi
Nov 19 '18 at 14:12












4 Answers
4






active

oldest

votes


















1














Here's code to convert image to base64 format



        $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo $base64;


Thanks






share|improve this answer































    0














    your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true



    add this line and check for error message



    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $path='https://www.jquery-az.com/html/images/banana.jpg';
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64=base64_encode($data);
    print_r($base64);exit;





    share|improve this answer































      0














      Please use the unescaped url in $path variable,



      <?php    
      $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';





      share|improve this answer































        0














        Try this code. It will help you.



        According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .



        $path = 'your-file-path';
        $file = 'your-file';
        $image_with_path = $path.'/'.$file;
        if(file_exists($image_with_path)){
        $base64 = base64_encode($image_with_path );
        }





        share|improve this answer





















        • 1





          How will this code help? Please explain your answer

          – Martin
          Nov 19 '18 at 14:29











        • I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

          – Martin
          Nov 20 '18 at 10:55











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374888%2fhow-to-convert-image-path-to-base64-encode-data-using-php%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Here's code to convert image to base64 format



                $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        echo $base64;


        Thanks






        share|improve this answer




























          1














          Here's code to convert image to base64 format



                  $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
          $type = pathinfo($path, PATHINFO_EXTENSION);
          $data = file_get_contents($path);
          $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
          echo $base64;


          Thanks






          share|improve this answer


























            1












            1








            1







            Here's code to convert image to base64 format



                    $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
            $type = pathinfo($path, PATHINFO_EXTENSION);
            $data = file_get_contents($path);
            $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
            echo $base64;


            Thanks






            share|improve this answer













            Here's code to convert image to base64 format



                    $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';
            $type = pathinfo($path, PATHINFO_EXTENSION);
            $data = file_get_contents($path);
            $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
            echo $base64;


            Thanks







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 12:48









            GulshanGulshan

            324318




            324318

























                0














                your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true



                add this line and check for error message



                <?php
                error_reporting(E_ALL);
                ini_set('display_errors', 1);

                $path='https://www.jquery-az.com/html/images/banana.jpg';
                $type = pathinfo($path, PATHINFO_EXTENSION);
                $data = file_get_contents($path);
                $base64=base64_encode($data);
                print_r($base64);exit;





                share|improve this answer




























                  0














                  your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true



                  add this line and check for error message



                  <?php
                  error_reporting(E_ALL);
                  ini_set('display_errors', 1);

                  $path='https://www.jquery-az.com/html/images/banana.jpg';
                  $type = pathinfo($path, PATHINFO_EXTENSION);
                  $data = file_get_contents($path);
                  $base64=base64_encode($data);
                  print_r($base64);exit;





                  share|improve this answer


























                    0












                    0








                    0







                    your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true



                    add this line and check for error message



                    <?php
                    error_reporting(E_ALL);
                    ini_set('display_errors', 1);

                    $path='https://www.jquery-az.com/html/images/banana.jpg';
                    $type = pathinfo($path, PATHINFO_EXTENSION);
                    $data = file_get_contents($path);
                    $base64=base64_encode($data);
                    print_r($base64);exit;





                    share|improve this answer













                    your code is working for me. I suppose the error is that in php.ini is disallowed to open remote urls: http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen set it to true



                    add this line and check for error message



                    <?php
                    error_reporting(E_ALL);
                    ini_set('display_errors', 1);

                    $path='https://www.jquery-az.com/html/images/banana.jpg';
                    $type = pathinfo($path, PATHINFO_EXTENSION);
                    $data = file_get_contents($path);
                    $base64=base64_encode($data);
                    print_r($base64);exit;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 12:53









                    myxaxamyxaxa

                    61036




                    61036























                        0














                        Please use the unescaped url in $path variable,



                        <?php    
                        $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';





                        share|improve this answer




























                          0














                          Please use the unescaped url in $path variable,



                          <?php    
                          $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';





                          share|improve this answer


























                            0












                            0








                            0







                            Please use the unescaped url in $path variable,



                            <?php    
                            $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';





                            share|improve this answer













                            Please use the unescaped url in $path variable,



                            <?php    
                            $path='http://abcd.com/pub/media/catalog/product//m/i/mi-l32m5-ai.jpeg';






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 '18 at 12:56









                            JeevanJeevan

                            1326




                            1326























                                0














                                Try this code. It will help you.



                                According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .



                                $path = 'your-file-path';
                                $file = 'your-file';
                                $image_with_path = $path.'/'.$file;
                                if(file_exists($image_with_path)){
                                $base64 = base64_encode($image_with_path );
                                }





                                share|improve this answer





















                                • 1





                                  How will this code help? Please explain your answer

                                  – Martin
                                  Nov 19 '18 at 14:29











                                • I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                  – Martin
                                  Nov 20 '18 at 10:55
















                                0














                                Try this code. It will help you.



                                According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .



                                $path = 'your-file-path';
                                $file = 'your-file';
                                $image_with_path = $path.'/'.$file;
                                if(file_exists($image_with_path)){
                                $base64 = base64_encode($image_with_path );
                                }





                                share|improve this answer





















                                • 1





                                  How will this code help? Please explain your answer

                                  – Martin
                                  Nov 19 '18 at 14:29











                                • I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                  – Martin
                                  Nov 20 '18 at 10:55














                                0












                                0








                                0







                                Try this code. It will help you.



                                According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .



                                $path = 'your-file-path';
                                $file = 'your-file';
                                $image_with_path = $path.'/'.$file;
                                if(file_exists($image_with_path)){
                                $base64 = base64_encode($image_with_path );
                                }





                                share|improve this answer















                                Try this code. It will help you.



                                According to his question, he needs to encode the image path. First, check if the image exists, if yes then encode its path .



                                $path = 'your-file-path';
                                $file = 'your-file';
                                $image_with_path = $path.'/'.$file;
                                if(file_exists($image_with_path)){
                                $base64 = base64_encode($image_with_path );
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 20 '18 at 10:55









                                Martin

                                12.6k53579




                                12.6k53579










                                answered Nov 19 '18 at 12:49









                                Danish AliDanish Ali

                                1,1481819




                                1,1481819








                                • 1





                                  How will this code help? Please explain your answer

                                  – Martin
                                  Nov 19 '18 at 14:29











                                • I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                  – Martin
                                  Nov 20 '18 at 10:55














                                • 1





                                  How will this code help? Please explain your answer

                                  – Martin
                                  Nov 19 '18 at 14:29











                                • I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                  – Martin
                                  Nov 20 '18 at 10:55








                                1




                                1





                                How will this code help? Please explain your answer

                                – Martin
                                Nov 19 '18 at 14:29





                                How will this code help? Please explain your answer

                                – Martin
                                Nov 19 '18 at 14:29













                                I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                – Martin
                                Nov 20 '18 at 10:55





                                I would have thought that with 700+ Rep you would have understood the ability to edit your post and write a conherent and full answer in your post.....

                                – Martin
                                Nov 20 '18 at 10:55


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374888%2fhow-to-convert-image-path-to-base64-encode-data-using-php%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                ComboBox Display Member on multiple fields

                                Is it possible to collect Nectar points via Trainline?