90

Is there a way I could add in the source of my image codes that could rotate my image?

Something like this:

<img id="image_canv" src="/image.png" rotate="90">

I'm making my images dynamic, so I was wondering if I could append some extra code to rotate it if I want it to.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
marchemike
  • 3,179
  • 13
  • 53
  • 96
  • Is possible rotate my products image from back end, like i am imported products image as a landscape i need rotate as a portrait. – Gem Jul 18 '17 at 11:43

4 Answers4

118

If your rotation angles are fairly uniform, you can use CSS:

<img id="image_canv" src="/image.png" class="rotate90">

CSS:

.rotate90 {
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
}

Otherwise, you can do this by setting a data attribute in your HTML, then using Javascript to add the necessary styling:

<img id="image_canv" src="/image.png" data-rotate="90">

Sample jQuery:

$('img').each(function() {
    var deg = $(this).data('rotate') || 0;
    var rotate = 'rotate(' + deg + 'deg)';
    $(this).css({ 
        '-webkit-transform': rotate,
        '-moz-transform': rotate,
        '-o-transform': rotate,
        '-ms-transform': rotate,
        'transform': rotate 
    });
});

Demo:

http://jsfiddle.net/verashn/6rRnd/5/

Menuki
  • 3
  • 3
VLS
  • 2,306
  • 4
  • 22
  • 17
  • I was somewhat looking for another way to rotate the image through the server, because I'm trying to crop the image, and when I use webkit transform or jqueryrotate, there seems to be a problem with my cropper, so I was hoping I could load the image rotated. – marchemike Nov 19 '13 at 02:46
  • Can you provide more context and/or an example? – VLS Nov 19 '13 at 03:09
95

You can do this:

<img src="your image" style="transform:rotate(90deg);">

it is much easier.

user9770563
  • 951
  • 6
  • 2
4

This CSS seems to work in Safari and Chrome:

div#div2
{
-webkit-transform:rotate(90deg); /* Chrome, Safari, Opera */
transform:rotate(90deg); /* Standard syntax */
}

and in the body:

<div id="div2"><img src="image.jpg"  ></div>

But this (and the .rotate90 example above) pushes the rotated image higher up on the page than if it were un-rotated. Not sure how to control placement of the image relative to text or other rotated images.

chuckkahn
  • 41
  • 1
  • add "style="z-index: 1" to the object you're rotating and style="z-index: 2" (or any number higher than 1) to the text or whatever you want to display on top. – user3241874 Nov 14 '20 at 14:21
1

This might be your script-free solution: http://davidwalsh.name/css-transform-rotate

It's supported in all browsers prefixed and, in IE10-11 and all still-used Firefox versions, unprefixed.

That means that if you don't care for old IEs (the bane of web designers) you can skip the -ms- and -moz- prefixes to economize space.

However, the Webkit browsers (Chrome, Safari, most mobile navigators) still need -webkit-, and there's a still-big cult following of pre-Next Opera and using -o- is sensate.

gchiconi
  • 624
  • 1
  • 7
  • 17