.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - Convert html block to image in laravel - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert html block to image in laravel - Stack Overflow

programmeradmin1浏览0评论

I am creating a html code by sending an ajax request.

Ajax script

$.ajax({
    url: "{{url('/')}}/phpScript", //Server script to process data
    type: 'POST',
    dataType: 'json',
    data: data, //this is an object with all my required variables
    success: function (html) {
        if (html) {
            $('#someTag').html(html); //change the html of #someTag with new html   
        }
    }

});

PHP Script:

static function createHtml() {
    ob_start(); ?>
        <div class="large-12 medium-12 columns" id="certificate-holder">
            <div class="middle-wrappe">
               <?php //some other code here to create the html ?>
            </div>
            <style>/** some style here**/</style>
        </div>


    <?php return ob_get_clean();
}

This is working fine and I am able to create html and change the html with new html.

Is there any way to create the image of that html block, so that I can return the image back and show the image in that #someTag instead of the html.

I have used anam/phantommagick to create image from html, but in this I need to pass the url of page to create image.

Thanks

I am creating a html code by sending an ajax request.

Ajax script

$.ajax({
    url: "{{url('/')}}/phpScript", //Server script to process data
    type: 'POST',
    dataType: 'json',
    data: data, //this is an object with all my required variables
    success: function (html) {
        if (html) {
            $('#someTag').html(html); //change the html of #someTag with new html   
        }
    }

});

PHP Script:

static function createHtml() {
    ob_start(); ?>
        <div class="large-12 medium-12 columns" id="certificate-holder">
            <div class="middle-wrappe">
               <?php //some other code here to create the html ?>
            </div>
            <style>/** some style here**/</style>
        </div>


    <?php return ob_get_clean();
}

This is working fine and I am able to create html and change the html with new html.

Is there any way to create the image of that html block, so that I can return the image back and show the image in that #someTag instead of the html.

I have used anam/phantommagick to create image from html, but in this I need to pass the url of page to create image.

Thanks

Share Improve this question edited Mar 25, 2020 at 15:20 user3601546 3181 gold badge6 silver badges15 bronze badges asked Nov 23, 2015 at 8:22 Anshul MishraAnshul Mishra 1,7162 gold badges16 silver badges40 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I use following technique

https://jsfiddle/8ypxW/3/

Jquery Code:

$(function() { 
     $("#btnSave").click(function() { 
        html2canvas($("#widget"), {
        onrendered: function(canvas) {
            theCanvas = canvas;
            document.body.appendChild(canvas);

            // Convert and download as image 
            Canvas2Image.saveAsPNG(canvas); 
            $("#img-out").append(canvas);
            // Clean up 
            //document.body.removeChild(canvas);
        }
    });
});

});

Its works for me. I hope you find your solution.

There are two ways two generate image from HTML block are:-

1.Using Jquery:-

html2canvas(document.getElementById('frame')).then(function(canvas) {
       // to view the generated image on the same page
        document.body.appendChild(canvas);

        // to save the image as png file
        Canvas2Image.saveAsPNG(canvas);

        // Get base64URL
    canvas.toDataURL('image/jpeg').replace('image/jpeg','image/octet-stream');
});

https://cdnjs.cloudflare./ajax/libs/html2canvas/0.5.0-beta4/html2canvas.min.js

2. Using spatie/browsershot package:-

This package can be installed through Composer.

poser require spatie/browsershot

In all examples it is assumed that you imported this namespace at the top of your file

use Spatie\Browsershot\Browsershot;

Here's the easiest way to create an image of a webpage:

Browsershot::url('https://example.')->save($pathToImage);

By default the screenshot's type will be a png. (According to Puppeteer's Config) But you can change it to jpeg with quality option.

Browsershot::url('https://example.')
    ->setScreenshotType('jpeg', 100)
    ->save($pathToImage);

By default the screenshot's size will match the resolution you use for your desktop. Want another size of screenshot? No problem!

Browsershot::url('https://example.')
    ->windowSize(640, 480)
    ->save($pathToImage);

You can take a screenshot of an element matching a selector using select.

Browsershot::url('https://example.')
    ->select('.some-selector')
    ->save($pathToImage);

You can take a screenshot of the full length of the page by using fullPage().

Browsershot::url('https://example.')
    ->fullPage()
    ->save($pathToImage);

For more options you can use the official documentation:- https://github./spatie/browsershot

发布评论

评论列表(0)

  1. 暂无评论