What is causing these images to appear larger than their original sizes?

My issue involves extracting multiple images from Facebook and displaying them in a scroller. When clicked, a dialog box appears with the full-size image (the images initially displayed are thumbnails obtained from a Facebook query using `src_small`).

The problem is that I am unable to determine the size of the images before retrieval. Some images turn out huge while others are very small. To address this discrepancy and ensure that all images fit properly within the dialog box at a reasonable size, I attempted the following CSS adjustments:

/*
 * Styling for large images in the dialog
 */
 .DialogImagesBig
 {
     position: relative;
     width: 95%;
     top: 0px;
     left: 10px;
 }
 /*
  * Firefox specific styling
  */
 @-moz-document url-prefix() 
 {
     /*
      * Additional adjustments for Firefox
      */
     .DialogImagesBig
     {
         height: 95% !important;
         width: 95% !important;
         position: relative;
         top: 0px;
         left: 10px;
     }
}

However, this approach seems to enlarge some images beyond their original size (larger images appear smaller, while smaller ones become pixelated). What might be causing this issue? How can I rectify it to ensure that all images fit appropriately within the dialog box without losing quality?

Edit: I have been advised to utilize Javascript (or jQuery) to resolve this. How should I proceed with implementing this solution?

Answer №1

When setting a width/height value to 95%, it will represent 95% of the parent container's width/height, not based on the original size of the element.

Answer №2

To retrieve the image dimensions, follow these steps:

var img = new Image();
img.src = _image_src_

img.width // provides the width
img.height // gives the height

img // displays <img src="_image_src_" />

You can then compare these values with the dimensions of your dialog box and resize accordingly. Hopefully, this will be useful to you.

For instance:

if (img.width > 100) 
  img.width = 100

$("#image_container").html(img)

Answer №3

Here is a possible solution for resizing the image:


h = $('#theimage').height();
w = $('#theimage').width();

if(h > 400 && w < 500) {
    $('#theimage').height(400);
    $('#theimage').width = w / (h / 400);
}
...
...

You can apply similar comparisons to adjust the size accordingly. The mathematical calculation seems correct in this case...

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

Strange behavior has been observed when loading Jquery in Zend Framework

I've been working on creating a dynamic form for my Zend Framework application using JQuery and Ajax, but I've encountered a strange issue. It seems that JQuery is not functioning properly on my page unless I include a standard JQuery object like ...

"Techniques for incorporating a screen in Angular application (Switching between Edit and View modes) upon user interaction

We are currently working on a screen that requires the following development: This screen will have the following features: When clicking on a button, the fields should become editable. In the image provided, there is some repeated data, but in our case, ...

What is the method for setting the content-type in an AJAX request for Android browsers?

I am facing an issue with my ajax call to the Rails server. The response from the server varies between HTML and JSON based on the content type. Surprisingly, this works smoothly on iPhone and desktop browsers like Chrome, but I am encountering a problem o ...

What is the best way to display items stored in MongoDB?

I'm encountering an issue while trying to display data from my MongoDB database in a dropdown menu. The error message "listName is not defined" keeps popping up, even though I have already declared it in the app.js file. How can I resolve this problem ...

align the p tag vertically next to the image

I'm struggling to get a p tag vertically aligned next to an image. The issue is that only the first line of text aligns properly. How can I make the text in the p tag wrap around the image while maintaining vertical alignment? Here is the HTML code: ...

How can you make a checkbox function like a select in AngularJS?

Is it possible for AngularJS to automatically bind all values (options) when a user selects something from a list, populating them via the model? How could I achieve a similar functionality with checkboxes? I want a select for multiple values and a checkbo ...

Next.js appending [object%20Object] to the URL's endpoint

I encountered an error when launching my next app using "npm run dev". The error occurred during the pre-render attempt: GET http://localhost:3000/aave/fundamentals/economics/[object Object] [HTTP/1.1 404 Not Found 434ms] The issue is not specific to thi ...

The Sequelize error message states: TypeError: an array or iterable object was expected, but instead [object Null] was received

I am encountering an issue with the findOne method from sequelize in my model. The error message I am getting states that the table referenced by the model is empty. How can I resolve this? Unhandled rejection TypeError: expecting an array or an iterable ...

Unit Testing JWT in Angular 2

Using JWT for authentication in my API calls. I am in the process of coding a service method. An interceptor is applied to all requests: public interceptBefore(request: InterceptedRequest): InterceptedRequest { // Modify or obtain information from ...

Django is experiencing difficulties loading CSS on certain template files

Working on my page design using twitter-bootstrap, I've created a base.html file that serves as the global base for all templates to extend. However, I'm facing an issue where upon extending the base.html in all templates, the CSS doesn't d ...

Sort by label using the pipe operator in RxJS with Angular

I have a situation where I am using an observable in my HTML code with the async pipe. I want to sort the observable by the 'label' property, but I'm not sure how to correctly implement this sorting logic within the pipe. The labels can be e ...

Issue: The element [undefined] is not recognized as a valid child of the <Routes> component. Only <Route> or <React.Fragment> components are allowed as children of the <Routes

I am facing an issue while trying to upgrade react-router-dom from v5 to v6. The error message I receive is as follows: Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragm ...

My Angular Router is creating duplicate instances of my route components

I have captured screenshots of the application: https://ibb.co/NmnSPNr and https://ibb.co/C0nwG4D info.component.ts / The Info component is a child component of the Item component, displayed when a specific link is routed to. export class InfoComponent imp ...

Menu with a full-width fluid input field using semantic-ui

I am trying to create a fixed top menu with an image on the left, an input to its right, and another input on the far right. My goal is to have the center input stretch to fill all remaining space between the image and the other input. Although I would no ...

Incorporate Angular directives within a tailor-made directive

I just started using a fantastic autocomplete directive called Almighty-Autocomplete. However, I feel like it's missing some features. The basic structure of the directive is as follows: .directive('autocomplete', function () { var index ...

What is the reason for Cypress choosing to omit specific commands?

The test below aims to scan and authenticate a QR code, then utilize the received authentication token. However, for some reason, the last two commands (.type) are not being executed. I've been stuck at this point for quite some time now. Any insights ...

Determine if a webpage forwards to a new link with the help of phantomjs, flagging any issues exclusively for designated websites

As a beginner in frontend development, I have a question that may seem simple. Please be patient with me. var page = require('webpage').create(), system = require('system'); if (system.args.length === 1) { console.log('Usage: ...

Passing a pair of variables through an onclick event back to the original page

Is there a way for me to click on a list element and have it display 2 variables on the same page depending on which one I click? Currently, I have two variables defined in the HTML code (although this could be changed as I am hardcoding them into the HTM ...

Revert animation back to its initial position with the help of JavaScript

After implementing the script below, I successfully managed to shift my image to the right upon clicking: <script> var myTimer = null; function move(){ document.getElementById("fish").style.left = parseInt(document.getElementById("fish ...

Combining text shapes in Three.js without losing their distinct material colors

Currently experimenting with merging text geometries in Three.js (r84) and looking to achieve this using multiMaterial while preserving individual colors for each text object. Check out the live demo here: https://jsfiddle.net/5oydk6nL/ Appreciate any ins ...