What is the best way to scale an image in headershot HTML without risking cropping and still preserving the aspect ratio?

In our Laravel project, I utilized browsershot to create A4 pdf files. In the headerHtml of browsershot, I wanted to display an image logo that would automatically adjust its size to fit a div container while preserving its aspect ratio. While following this guide on auto-resizing images to fit a "div" container, I was successful in achieving the desired outcome with other images of varying dimensions. However, there is one specific image where the bottom side is getting cropped.

Here is the image where the bottom side is being clipped:

https://i.sstatic.net/8tYkC.png

The particular image experiencing cropping at the bottom: https://ibb.co/FJKFSpS

Header blade file:

@props(['proposal_title', 'proposal_logo', 'file_type'])
<header style="outline: 1px solid blue; opacity: 0.6; color: #672F09; width: 100%; font-size: 18px; padding: 0 2cm; display: flex; justify-content: space-between; align-items: center;">
    <div style="outline: 1px solid red; width: 75px; height: 75px;">
        <img style="object-fit: contain; height: 100%; width: 100%;" src="data:image/{{ $file_type }};base64,{{ $proposal_logo }}" alt="Logo">
    </div>
    <p style="text-align: center; font-weight: 500; width: 70%;">{{ $proposal_title }}</p>
    <div style="width: 75px; height: 75px;"></div>
</header>

To replicate using browsershot, you can use placeholder data for the $html and $footer blade file.

$html = view('grantproposals.index', ['grant_proposal' => $grant_proposal])->render();
$header = view('grantproposals._header', ['proposal_title' => $grant_proposal->proposal_title, 'proposal_logo' => $pdo->image->grant_proposal_logo, 'file_type' => $pdo->image->file_type])->render();
$footer = view('grantproposals._footer', ['proposal_title' => $grant_proposal->proposal_title])->render();

            Browsershot::html($html)
                ->showBrowserHeaderAndFooter()
                ->headerHtml($header)
                ->footerHtml($footer)
                ->format('A4')
                ->waitUntilNetworkIdle()
                ->margins(35.4, 0, 25.4, 0)
                ->newHeadless()
                ->timeout(120)
                ->save($path . 'filename.pdf');

It's important to convert the image to base64 for it to work correctly in headerHtml.

logo base64: https://pastebin.com/x52Y8M6z

or

convert the logo to base64:

I'm facing challenges with this specific image, any advice would be greatly appreciated. Thank you!

Answer №1

The reason for the issue is currently unknown, but initially, the image appeared corrupted in PNG format. When viewed in an image viewer, it appears distorted. Subsequent tests indicate that the problem may not be related to quality or encoding, suggesting it could be triggered by something else.

https://i.sstatic.net/manbZ.png

Switching the image to a JPEG format like this onehttps://i.sstatic.net/GQr1W.jpg should resolve the issue.

In Chromium Edge, the image displays incorrectly with blank areas as shown here https://i.sstatic.net/2kWnC.png.

In Firefox, the image also gets cut off at a different location, and its appearance changes when zoomed in https://i.sstatic.net/qF9VW.png

It appears that the base64 content of the image is valid, but it seems to have compatibility issues with the CSS layout in HTML. Essentially, it is being treated as if it were in an iframe, restricting it to a default height regardless of image resolution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Selenium encountered an error: Element not found - Could not locate element: {"method":"CSS selector","selector":"*[data-test="email-input"]}

Recently, my application has been encountering an error when trying to log in through a web form. The issue seems to be with locating the email input field: "no such element: Unable to locate element: {"method": "css selector", "s ...

Issue with CSS3 calc() validation: Invalid value provided for width, resulting in a parse error

Can you help me validate a CSS3 file that contains the following code snippet? width:calc(96.3% - 312px) When I try to validate it, I get this error message: Value Error : width Parse Error - 312px) My solution might be to implement a JavaScript func ...

What is the best way to incorporate one Div within another Div using JavaScript?

I have a single main div with an id, and I am looking to insert 4 additional child divs inside it. Each of these child divs will then contain 5 more divs each. The code snippet that I currently have is as follows: $( document ).ready(function() { for( ...

What steps can I take to maintain the horizontal scroll position of a div even after adding new elements to it?

I'm struggling to achieve an infinite x-scroll using HTMX. The issue I'm facing is that the request keeps getting triggered endlessly because the element that triggers it gets inserted on the left side of the div, causing a new request to load it ...

JQuery is failing to locate elements that have dynamic data titles assigned to them

I've been attempting to locate an element using a jQuery regex comparison in the data title. My situation involves having divs with the class .textbox, each containing a dynamically generated data title. To target specific boxes with that particular d ...

Guide to making a Material Design Radial effect animation

I am looking to create a unique toolbar effect by following the material design radial reaction choreography guideline. https://i.stack.imgur.com/6oB8r.gif I want to achieve this using an angular 2 transition, but I need some guidance on how to implement ...

Is it advisable to turn off CSRF verification?

I'm currently working on an online quiz application using Laravel. One of the features I have implemented is the ability for users to resume their sessions during the quiz. This means that if a user reloads the page while taking the quiz, their sessi ...

Can you explain the distinction between using return redirect('/') and Redirect::to('/')->send() in Laravel?

The first code is functioning correctly. Details below. However, the second code appears to be malfunctioning. More information provided below. Could someone assist in providing documentation for the code Redirect::to('/')->send();? ...

ngx-datatable detail row failing to expand properly

I am striving to develop an ngx-datatable component that can be utilized universally for consistent styling and functionality. Although most features are working correctly, I'm struggling to understand why the details row isn't expanding as expe ...

The Angular Material FAB Speed Dial feature is causing a slight offset within the Toolbar

Attempting to incorporate design examples from the angular material site, I am utilizing Angular Material 1.0.0RC5. I am aiming to position the FAB Speedial within the Toolbar, similar to the example demonstrated on their site: https://material.angularjs. ...

Troubleshooting Laravel's Wildcard Subdomain Routing Issue

I am currently working on a project using the Laravel framework to create a web app where users can personalize their own profile pages. I'm encountering an issue with using subdomains for user profiles. In my routes file, I have the following code: ...

Is there a component that functions as a dropdown menu? I've searched online but can't seem to

I have been working on creating a website that allows users to download PDF and MP3 files of sermons. However, I am facing challenges in implementing a dropdown menu for this functionality. Despite searching resources like Bootstrap and W3schools, I have ...

Is there a way for me to track the number of times the local HTML document has been accessed?

Imagine we have a standalone HTML file that is sent to someone. Is there a way, possibly using third-party services, to track if the document has been opened without the user knowing? I thought about placing a PHP script on a "friendly" server and making ...

ASP repeater exceeding container boundaries

Having trouble with my asp repeater that reads from a database and generates repeatable divs. The issue arises when the h4 spills over the div, particularly when using <%Eval. Any suggestions on how to fix this? Manual input divs don’t seem affected o ...

`The background video is not being displayed on the transparent PNG`

I have created a transparent PNG image featuring a theater setup and I am attempting to overlay a 1-second video as a background element, similar to a GIF. However, when I try to display the video, instead of seeing it, the background color appears. Additi ...

IE6 disrupts stored layouts

I've encountered a strange issue with my design in IE6. It loads perfectly at first, but after reloading it a couple of times, everything suddenly collapses. The strange part is that when I upload an updated version, it fixes the issue, only to break ...

Using Django and jQuery to retrieve a file and display a prompt for downloading the file in the browser

Previously, I had a question about passing files to the browser for download, which was easily achieved by passing strings created at the end of a function to an iframe's src attribute. Now, I have a more complex task at hand – I need to pass pre-e ...

Troubleshooting Angular: Issues with Table Data Display and Source Map Error

I'm currently tackling a challenge in my Angular application where I am unable to display data in a table. When I fetch data from a service and assign it to a "rows" variable within the ngOnInit of my component, everything seems to be working fine bas ...

Tips on saving a form submit button data to localStorage

As a newcomer, I am on a quest to make this function properly. My goal is to create a form where the submit button saves data to the localStorage. Here's what I have tried thus far. <script> function storeRecipe() { localStorage.setItem($(" ...

How to insert commas in a string at specific positions using JavaScript

At this moment, my simple numpad is set up to input a dollar amount into the span tag .bill-amount__integer. The initial text in this span is 0.00. <div class="bill-amount__value"> <span>$</span><span class="bill-amount__integer"& ...