"Unable to display image" for an image created by PHP within CSS/JavaScript

In my Laravel site, I have the following code snippet inside a controller:

serveAngledImage($session,$imgId,$angle) {
      [...]

        $maskExists =\Storage::disk('s3')->exists($usrDirectory.$maskFile);

        $maskUrl = \Storage::disk('s3')->temporaryUrl(
            $usrDirectory . $maskFile, now()->addMinutes(12)
        );

        if ($maskExists) {

          $img = Image::make($maskUrl);
          $originalWidth = $img->width();
          $originalHeight = $img->height();
          $img->rotate((-1 * $angle))->crop($originalWidth, $originalHeight)->encode('data-url');
        }
        else {
            $img = null;
        }

        return $img->encoded;

    } 

This code is called by the following route:

Route::get('/angleImage/{session_id}/{media_id}/{angle}', 'mediacenter@serveAngledImage');

When visiting this route using a browser, the expected result is an encoded image. However, when adding this as the source of a mask in CSS (via JavaScript), the image fails to load. The URL appears red in the web development tools with an alert stating "unable to load image".

var Mask = '-webkit-mask-image: url(/angleImage/'+this.sh_session+'/'+this.image.id+'/'+back.angle+'); -webkit-mask-repeat: no-repeat;position:absolute;top:0;left:0;display:block;';
                this.style = Mask;

If copying and pasting the link from the CSS into a new browser tab, the encoded image displays correctly. However, when viewing the data from the pasted link, it appears as something like: data:image/png;base64,iVBORw0KGgo....

Answer №1

When working with background image URLs in CSS, it is important to remember that they are relative to the file path. If you are serving the CSS file from a subfolder, you will need to adjust the URL path accordingly.

For example, if your CSS file is being loaded like this:

<link rel="stylesheet" href="/css/styles.css">

And the image is located at:

http://example.com/images/photo.png

Your background image URL should be adjusted to:

background: url('../images/photo.png');

Answer №2

The issue was located within the controller section, specifically in the intervention image code. To resolve it, I simply made the following adjustment:

$img->rotate((-1 * $angle))->crop($originalWidth, $originalHeight)->encode('data-url'); 

was replaced with

$img->rotate((-1 * $angle))->crop($originalWidth, $originalHeight)->response('png'); 

After implementing this change, everything is now functioning as expected.

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

Using jQuery to display items from GitHub API in a custom unordered list format

Attempting to access data from the GitHub API using jQuery (AJAX) and display it on a static webpage. Here are the HTML and JS code snippets: $(document).ready(function(){ $.ajax({ url: 'https://api.github.com/re ...

Bug found in Vue.js app: search result pagination not working as expected

I've been developing a small frontend application that showcases a list of users in an HTML5 table using Bootstrap 3, Axios, and Vue.js 2. The user data is fetched from a JSON API and displayed in a paginated manner. While implementing a search/filte ...

How can I send an array of objects to a PHP server using axios?

let dataArray = [ { fname: 'name #1', choice: 'choice #1', }, { fname: 'name #2', choice: 'choice #2', }, // more data could be appended here ]; I'm looking for guidance on how to send ...

What is the Proper Way to Import Three.js Modules in a Vue Application?

My goal is to utilize three.js for displaying a gltf file. To achieve this, I need to import the GLTFLoader module from the three.js node package. As per the documentation, the correct way to import it is: import { GLTFLoader } from 'three/examples/ ...

Sending formdata containing a file and variables via $.ajax

I've been working on a jquery file upload project based on a tutorial I found here: . The tutorial is great and the functionality works perfectly. However, I'm looking to add more control and security measures for user image uploads, such as inco ...

Invoke a parent method from a nested child component in Vue

After setting up a project with vue-cli using the webpack template, I decided to incorporate a reusable bootstrap modal dialog in the App component. To achieve this, I created a method called showMessage in the App component that handles displaying the mod ...

Exploring the Depths of React Routing: The Power

I'm currently diving into React and trying to figure out how to create dynamic routes similar to partial pages in Angular. Here is my main App component: import React from 'react'; import Header from '../common/Header'; export d ...

AJAX is designed to only modify one specific Django model instance at a time

My Django project displays a list of "issue" objects, each with a boolean field called "fixed". I am looking to implement a feature where users can press a button that will modify the "fixed" status using Ajax. However, currently, the button only changes o ...

How can I adjust the Line Opacity settings in Google Charts?

Within my Google Charts project, I have successfully implemented a feature that changes the color of a line halfway through a graph based on certain conditions using a dataView. Here is the code snippet demonstrating this functionality: var dataView = new ...

I am attempting to implement an auto-assignment feature in my Discord bot using discord.js, however, it seems to be malfunctioning

Trying to implement an autorole feature for my bot following a tutorial, but encountering an error message when testing it on a secondary account in Discord. The section of code related to autorole in my index.js file: client.on("guildMemberAdd" ...

Tips for pulling information from two different services using an identification number and showcasing the data

Looking for a way to select data in AngularJS (JavaScript) similar to PHP's SELECT "message" FROM "users" WHERE id = $id? Here is my current setup: app.service("users", function() { this.userList = [ { userId: 1, u ...

HTML - Align 3 tables horizontally with text below

I have successfully floated 3 tables next to each other using <table style="float:left;">. However, I am facing an issue where some text is being wrapped to the right side of the right-most table instead of being left-justified at the very ...

Limiting the number of rows in an HTML table: a comprehensive guide

Need help with displaying item names in a table with a limit of three rows. If there are more than 3 items, the 4th item should move to the second column, and if there are more than 6 items, the 7th item should go in the third column. Currently, I am using ...

Tips for implementing JavaScript or jQuery to enable page transitions in a single-page website

Currently, I am in the process of developing a website that encompasses both front-end and back-end elements. One challenge I am facing involves figuring out how to utilize JavaScript/jQuery to transition between pages within a single-page website. The ta ...

The excessive height of a Bootstrap 5 row is causing the image to overflow beyond the container

I am currently working on setting a maximum height for a banner I am designing in bootstrap5. Here is my code snippet: .banner { background: red; max-height: 50px; } <head> <title>Example</title> <meta charset="utf-8" ...

The controls for the Bootstrap carousel component do not have a clear appearance

Trying to implement a Bootstrap carousel component but facing an issue with the controls not appearing transparent like in the example provided in the Bootstrap documentation. Check out the code snippet: <div class="carousel slide" id="s ...

You cannot assign a type in react-chartjs-2

Currently, I am working on a project using React and TypeScript. However, I have encountered an error related to types in my code. Can someone guide me on how to resolve this issue? The 'position' variable is set as a string data type. For more ...

Label for the second text field does not shift with the material

I'm experiencing an issue with the label animation on my second text field not moving when focused. https://i.sstatic.net/Q2gFH.png HTML: <div class="mdc-text-field mdc-text-field--outlined"> <div class="m ...

Nested solution object populated with promises

Looking for a solution similar to the npm libraries p-props and p-all, but with the added functionality of recursively resolving promises. const values = { a: () => Promise.resolve(1), b: [() => Promise.resolve(2)], c: { d: () =&g ...

How to extract information from a JavaScript object array in Node.js?

Currently, I am attempting to extract data from a JavaScript Object array by providing field names and then storing the data in an array. However, my current approach seems to be yielding incorrect results. Whenever I try to log the values stored in ' ...