I'm trying to figure out how to dynamically insert a margin into "mainImage". What I'm trying to do is vertically center the image, which has dynamic height and width (and dynamic client viewports). I can't figure out how to add the margins into the images as they're being loaded from javascript...
I have the following HTML:
<div id="content">
<img id="left" onclick="prevImage('images/52/image_','mainImage',52)" src="icons/arrow_left.png" />
<img id="right" onclick="nextImage('images/52/image_','mainImage',52)" src="icons/arrow_right.png"/>
<img id="mainImage" src="images/52/image_0.jpg" onload="addMargins()" />
</div>
with the following CSS:
div#content{
position: absolute;
margin-left: 20%;
margin-right: 5%;
width: 75%;
height: 95%;
text-align: center;
}
div#content img#left{
position: absolute;
left: 0;
top: 50%;
}
div#content img#right{
position: absolute;
right: 0;
top: 50%;
}
div#content img#mainImage{
max-height: 100%;
max-width: 95%;
}
and the javascript:
var key = 1;
function nextImage(thePath, id, max)
{
if(key == max)
{
key = 1;
}else{
key++;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
function prevImage(thePath, id, max)
{
if(key == 1)
{
key = max;
}else{
key--;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
So my idea was to do something in javascript that basically says:
"var viewport= viewport.height;
var mainImage= mainImage.height;
set marginTop to (viewport-mainImage)/2;
set marginBottom to (viewport-mainImage/2;"
and do that for each image as it loads via the "next/prev" buttons.
I wouldn't say this is the best way to do this. You probably want to look at using 'em's in your css to set the margins rather than doing it inline. The em's will ensure that the margins are relative to the size of the containing element.
However, if you want to do it this way, then you are on the right track. You will need to get a reference to the image and then set the marginTop property of the style.
var elem = document.getElementById(id); //reference to image
elem.style.marginTop = (viewport-mainImage)/2;
elem.style.marginBottom = (viewport-mainImage)/2;
I'm not 100% sure that your image will already have a height, so let me know if this works. If not, I'll check it out when I have a moment. Happy coding.
Related
In Wordpress, I wanted to duplicate the images of the posts, so I can set one of them as background, and then blur it, creating a nice effect, without having to change all my html structure.
How can I do that, if possible, with PHP? I tried long time ago to achieve with JQuery, but at that time I didn't manage to make it:
$(".post-cover").each(function(){
var img = $(this).find("img");
$(this).css({
"background-image": "url('"+img.prop("src")+"')",
//Any other CSS background propriety
//If your div don't have a fixed width and height
width: img.width(),
height: img.height()
});
img.remove();
});
If I used Jquery, where should I implement it?
My structure is
<div class="post-cover">
<img src="#"/>
</div>
and the final result should be something like:
For this to work you need to set the img source as the background of the container div and blur it. However, as this will blur all child elements, you will need to move the original img element outside of the .post-cover and position it absolutely so that it is still sharp. Try this:
$(".post-cover").each(function() {
var $cover = $(this);
var $img = $cover.find("img");
$cover.css({
backgroundImage: "url('" + $img.prop("src") + "')",
width: $img.width(),
height: $img.height()
});
$img.insertAfter(this).css({
position: 'absolute',
top: $cover.offset().top,
left: $cover.offset().left
})
});
.post-cover {
-webkit-filter: blur(10px);
filter: blur(15px);
margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post-cover">
<img src="https://i.imgur.com/mE2HyxV.jpg" />
</div>
Could be like this:
$( document ).ready(function() {
$('.post-cover img').each(function() {
$(this).before('<img src="'+ $(this).attr('src')+'" class="blur">');
});
});
.post-cover {
position: relative;
}
.post-cover img {
width: 250px;
height: auto;
display: block;
position: relative;
padding: 50px;
}
.post-cover img.blur {
content: '';
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
-webkit-filter: blur(25px);
filter: blur(25px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post-cover">
<img src="https://placeimg.com/640/480/arch">
</div>
I want to add a text and link to both the sign boards here in this image (separately for both). I am trying to use the <map> <area> rule and I need to place the coordinates of the two rectangle boxes there so that once a user clicks on that board or text he will be redirected to some another page. Now the problem is that I am not sure from where to find the exact coordinates of the image here and how? If anyone can help then please help.
Here is the code I am using
<img src="image link here" usemap="#mapname">
<map name="mapname">
<area shape="rect" coords="" href="http://www.google.com" alt="TEST">
</map>
I'm working on something similar, but I wanted to make it responsive - not if you zoom in, image will be bigger and areas too. I didn't use <map>, because the coords are absolute. I used this:
<div id="mapdiv">
<img src="link" id="imgmap" alt="" />
<div id="box1">Here is the text</div>
<div id="box2" onclick="alert('You can use js too')"></div>
</div>
And CSS:
#imgmap {
width: 100%;
}
div#mapdiv {
position: relative; /* thanks to this... */
}
div#menu div {
position: absolute; /* ...and this are boxes positioned relatively inside the imgdiv */
border: 1px dashed blue; /* for finding coords, remove after you are done */
}
div#box1 {
left: 21%; /* my coords, make your own by trying and trying... */
top: 26.5%;
height: 5%;
width: 6.5%
}
div#box2 {
left: 7.5%;
top: 66.2%;
height: 24.5%;
width: 31.5%;
}
if you want to add text, then you better use real links and set them on top of your areas wich are quiet good rectangle.
example:
.map {
position: relative;
}
.map img {
display: block;
width: 100%;
}
.map a {
position: absolute;
top: 48.6%;
left: 9.118%;
width: 19.8%;
height: 19%;
transform: rotate(-1.375deg);
border-radius: 50% 50% 0px 0 / 0.25vw;
transition: 0.5s;
color:#3F4754;
display:flex;
align-items:center;
justify-content:center;
font-size:4vw;
font-weight:bold;
font-family:courier;
font-variant:small-caps;
text-decoration:none;
text-shadow:-2px -2px 2px black
}
.map a + a {
top: 48%;
left: 70%;
transform: rotate(3deg);
transform-origin: bottom right
}
a:hover {
color: white;
background:linear-gradient(to bottom left, rgba(0, 0, 0, 0.5), transparent);
text-shadow:2px 2px 2px black
}
<div class="map">
<img src="https://i.stack.imgur.com/mDEuy.jpg" />
hover me
or me
</div>
use your own style and ids or class
I use a chrome extension I got called Page Ruler. What I do is use the extension to draw a rectangle from the pixel with the coordinate 0,0 to a target pixel. The bar at the top shows the width and height of the rectangle. There are also other tools, such as Meazure, which can do the same thing.
You can use the mouse events clientX, clientY https://www.w3schools.com/jsref/event_clientx.asp
When you use onmousemove it shows the coordinates
<!DOCTYPE html>
<html>
<body>
<img src="https://i.stack.imgur.com/mDEuy.jpg" usemap="#mapname"
onmousemove="showCoords(event)">
<map name="mapname">
<area shape="rect" coords="" href="http://www.google.com" alt="TEST">
</map>
</img>
<p id="demo"></p>
<script>
function showCoords(event) {
var x = event.clientX;
var y = event.clientY;
var coords = "X coords: " + x + ", Y coords: " + y;
document.getElementById("demo").innerHTML = coords;
}
</script>
</body>
</html>
$(document).ready(function() {
$("img").on("click", function(e) {
bounds=this.getBoundingClientRect();
var l = bounds.left;
var t = bounds.top;
var x = e.pageX - l;
var y = e.pageY - t;
var cw = this.clientWidth;
var ch = this.clientHeight;
var nw = this.naturalWidth;
var nh = this.naturalHeight;
var px = x/cw*iw;
var py = y/ch*ih;
});
});
Background:
I am trying to render a business card at 300dpi using the mPDF library. The document has an image background which should fill the canvas and then have various text elements overlayed.
Dimensions:
The PDF document is set to 91mm x 61mm and is in Landscape format.
$pdf = new mPDF('utf-8', array(91,61), 0, '', 0, 0, 0, 0, 0, 'L');
I have the resolution set in the config as follows;
$this->dpi = 300;
$this->img_dpi = 300
I have create an image in photoshop, also at 300dpi to the same dimensions as the card (91mm x 61mm).
I have tried adding the image to the document inside a div using markup as follows:
$html .= '<div style="position: absolute; left:0; right: 0; top: 0; bottom: 0;width:100%; height:100%"><img style="width:100%; height:100%" src="/assets/images/bg.jpg"></div>';
When the PDF document is displayed, the image seems to be smaller than the document, ie, it is not scaled to fit the page. In fact the image only appears to fill approximately 55-60% of the canvas in X and Y directions.
When I save the PDF document and look at its properties inside Adobe reader it is confirmed to be the correct size of 91x61mm.
Has anyone had a similar problem or understand what is going on here?
I really need to be able to have a 300dpi image which will fill the page exactly.
I look forward to your suggestions.
Regards
James
the css styles you have dont entirely match the ones in the manual's recommendation in the 'size constraint' section
in my case, i have a landscape image that's 11x8.5 inches 150dpi in photoshop, which is matched in mpdf as the page size, i separated the css for cleanliness
just in case, i specified any other mpdf values or css settings
so try something more like this code that is working for me, position the text with css & adjust the dpi/page size as needed:
$html = '
<html>
<head>
<style>
body {
position: relative;
text-align: center;
overflow: hidden;
page-break-inside: avoid;
}
#bg {
position: absolute;
left:0;
right: 0;
top: 0;
bottom: 0;
}
#bg img {
width: 100%;
height: 100%;
margin: 0;
}
#text1 {
top: 20%;
font-size: 20pt;
position: absolute;
width: 100%;
color: #fff;
}
#text2 {
top: 60%;
font-size: 16pt;
position: absolute;
width: 100%;
color: #ddd;
}
</style>
</head>
<body>
<div id="bg">
<img src="bg.png" />
</div>
<div id="text1">John Smith</div>
<div id="text2">555-5555</div>
</body>
</html>';
$mpdf = new mPDF('UTF8','Letter-L',14,'Arial',0,0,0,0,0,0);
$mpdf->dpi = 150;
$mpdf->img_dpi = 150;
$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');
$mpdf->Output();
there is of course an easier way to make a background... tell body or html to have a background image in css, but then you run into this bug so it's not my first recommendation
What I have is a standard form in HTML that allows the user to select a "Width" option and a "Height" option (each with values ranging from 1 to 10). When they send the form, it sends it to a PHP/HTML page where PHP grabs the "Width" and "Height" variables and assigns it to a width and height of a DIV.
But what I'm trying to do is just use the "Width" and "Height" variables to assign an aspect ratio to that DIV, and then have that DIV auto-resize to 100% of the container it is inside, but while keeping that same aspect ratio.
Example:
User selects a Width of 4 and a Height of 2, then sends the form. On the receiving PHP page, that DIV (the one receiving the width and height ratios) is inside a container that's 1000px width and 600px height. So now, that DIV resizes to 1000px wide and 500px tall (that would be the aspect ratio of 4 to 2)
Any ideas, codes, scripts would be extremely helpful, thank you very much!
Aaron
Since percentage values of the padding-* properties are calculated with respect to the width of the generated box's containing block, you could:
Add a dummy element with no content but with a percentage in a vertical padding (padding-top or padding-bottom), corresponding to the desired aspect ratio.
Use absolutely positioning to remove all contents from the normal flow of the element, in order to prevent them from increasing the height. Then, make it grow to fill the container.
This idea is taken from http://ansciath.tumblr.com/post/7347495869/css-aspect-ratio
#container {
position: relative;
width: 50%;
}
#dummy {
padding-top: 75%; /* 4:3 aspect ratio */
}
#element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: silver;
}
<div id="container">
<div id="dummy"></div>
<div id="element">
some text
</div>
</div>
Note vertical margin could be used instead of vertical padding, but then there would be margin collapse. To prevent it, add
#container {
display: inline-block;
}
#container {
display: inline-block;
position: relative;
width: 50%;
}
#dummy {
margin-top: 75%; /* 4:3 aspect ratio */
}
#element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: silver;
}
<div id="container">
<div id="dummy"></div>
<div id="element">
some text
</div>
</div>
Using ::before pseudo element, there's no need to use a dummy element:
#container:before {
padding-top: 75%; /* 4:3 aspect ratio */
content: ''; /* Enable the pseudo-element */
display: block;
}
#container {
position: relative;
width: 50%;
}
#container:before {
padding-top: 75%; /* 4:3 aspect ratio */
content: ''; /* Enable the pseudo-element */
display: block;
}
#element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: silver;
}
<div id="container">
<div id="element">
some text
</div>
</div>
You could take advantage of the fact that replaces elements may have an intrinsic aspect ratio.
According to the spec,
Otherwise, if 'height' has a computed value of 'auto', and the element
has an intrinsic ratio then the used value of 'height' is:
(used width) / (intrinsic ratio)
Therefore, you could
Create a replaced element with the desired intrinsic ratio, and then set width:100% to it.
Use absolutely positioning to remove all contents from the normal flow of the element, in order to prevent them from increasing the height. Then, make it grow to fill the container.
Then, the container container will have the aspect ratio that you want.
The replaced element could be an image. You could create images of the desired aspect ratio in PHP, or using a third party web service like http://placehold.it/
In the following snippet, I use a 2px width and 1px height image ():
.container {
border: 3px solid blue;
position: relative;
}
.container > img {
width: 100%;
display: block;
visibility: hidden;
}
.container > .content {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: auto;
}
<div class="container">
<img src="http://i.stack.imgur.com/Lfmr6.png" />
<div class="content">
<p>01</p><p>02</p><p>03</p><p>04</p><p>05</p>
<p>06</p><p>07</p><p>08</p><p>09</p><p>10</p>
<p>11</p><p>12</p><p>13</p><p>14</p><p>15</p>
<p>16</p><p>17</p><p>18</p><p>19</p><p>20</p>
</div>
</div>
You can also use a <canvas> element instead of an image. This way you don't need to create images, but it doesn't work on old browsers (like IE 8 and earlier):
<div class="container">
<canvas height="1" width="2"></canvas>
<div class="content">...</div>
</div>
.container {
border: 3px solid blue;
position: relative;
}
.container > canvas {
width: 100%;
display: block;
visibility: hidden;
}
.container > .content {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: auto;
}
<div class="container">
<canvas height="1" width="2"></canvas>
<div class="content">
<p>01</p><p>02</p><p>03</p><p>04</p><p>05</p>
<p>06</p><p>07</p><p>08</p><p>09</p><p>10</p>
<p>11</p><p>12</p><p>13</p><p>14</p><p>15</p>
<p>16</p><p>17</p><p>18</p><p>19</p><p>20</p>
</div>
</div>
I want to center an image in an area, without resizing... I am using HTML.
Example:
I have an image <img src='img1.png' width='64' height='64'> - the image is actually 64x64. It displays perfectly.
Now, I have another image <img src='img2.png' width='64' height='64'> however, the image is not as big as it should be, its 32x32 - what happens here is it resizes the image to 64x64 and makes it look like $%^&.
How do I make images smaller then the desired width and height centered in the 'img' area without any resizing what so ever?
What you will need is something like this:
<div class="box">
<img src="whatever size image you'd like" />
</div>
And for the styling (in an external stylesheet, natch) you'd apply:
/* Image centering */
div.box {
border: 1px black solid;
height: 64px;
width: 64px;
background: #444;
display: table-cell;
vertical-align: middle;
}
.box img {
display:block;
margin: 0px auto;
}
This works for images with dimensions <= 64x64px, and is easily modifiable to work with larger pics. The key elements here are
set dimensions on the div
display as a table-cell (allows vertical align)
vertical align (aligns on the Y-axis w/out weird hacks)
display:block on the img element
margin: auto centers the image laterally
Solution without IE-unfriendly display:table-cell:
<!DOCTYPE html>
<style>
div {
line-height:64px; /* that's the secret sauce */
text-align:center;
width:64px; height:64px;
}
img {vertical-align:middle}
</style>
<div><img …></div>
You could try putting the image inside a DIV that is 64x64 and not specifying the image dimensions. Then you could style the div so its contents are centered and any overflow is hidden.
You can dynamically get an image size using the getimagesize() php function:
<?php
$size = getimagesize('imgX.png');
$height = $size[1];
$width = $size[0];
?>
<div style="text-align: center">
<img src="imgX.png" width="<?php print($width) ?>" height="<?php print($height) ?>" />
</div>
I've had to do something similar with 36x36 images. Users were able to upload any size but the thumbnails were only to show the center 36 square pixels.
Markup:
<li><div><span></span>
<img src="_media/objects/jessica-bowman.png" alt="Jessica Bowman" /></div>
<p>Jessica Bowman</p>
</li>
The span was just there to get rounded corners on the image, it's not necessarily needed.
CSS:
ul.recent-list li div {
position: relative;
float: left;
width: 36px;
height: 36px;
overflow: hidden;
}
ul.recent-list li div span {
position: relative;
z-index: 100;
display: block;
width: 36px;
height: 36px;
background: url("../_media/icons/icon-overlay.png") top left no-repeat;
}
ul.recent-list li div img {
position: relative;
top: -36px;
z-index: 0;
float: left;
}
JavaScript:
$(window).load(function() {
$("ul.recent-list div img").each(function() {
var moveX = ($(this).width() / 2 * -1) + 18;
var moveY = ($(this).height() / 2) * -1 - 18; // 18 is 1/2 the default offset of 36px defined in CSS
$(this).css({'top' : moveY, 'left' : moveX});
});
});
The solution is a simple bit of CSS + HMTL
<img src="transparentpixel.gif"
width="64"
height="64"
style="
background-image:url('path/to/image.jpg');
background-repeat:no-repeat;
background-position:center center;
" />
the transparentpixel.gif is a simple 1x1px transparent gif image
An img tag with width and height attributes is saying "stretch or shrink the image to this size regardless of its actual size". use something like:
<div style="text-align:center;">
<img src="x.jpg">
</div>
and no i don't know why text-align would work, but it appears to in my experience.
Use CSS to render the image using background:
<div style="background: url(img1.png) no-repeat center center; height: 64px; width: 64px;"></div>
This will show the image in the center, without scaling it.