Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classic ASP - Image rotating

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
0
0
US
One crazy thing I've noticed is that photos coming in from smart phones are often times rotated 90 degrees to the right. That doesn't work when the picture should be in portrait mode and it ends up being displayed horizontally!

Does anyone have a FREE piece of code that can be used to rotate pictures if they need to be!!

First of all, how do you determine that it needs to be rotated? There might be a horizontally created image that shouldn't be rotated, b ut if thewy should be, then they need to be...

Thanks in advance,
Jerry

Jerry Scannell
 
You might want to have a look at Microsoft's Windows Image Acquisition 2.0 (WIA) library, which makes this kind of thing fairly easy, even in VBScript
 
So, something like the following, which is less than a dozen lines of code ...

Code:
[blue]Public Sub RotateImage(strFile, lRotAng)

    Dim img 
    Dim ip 
    Dim orientation
    Dim filename 
    
    Set img = CreateObject("WIA.ImageFile")
    Set ip = CreateObject("WIA.ImageProcess")
    img.LoadFile strFile  
    ip.Filters.Add ip.FilterInfos("RotateFlip").FilterID
    
   [COLOR=green] ' Orientation = img.Properties("Orientation")
    ' Above *may* return value concerning orientation as per this table - but not all images have the info - so could optionally modify lRotAngle accordingly
        '1 Rotated 0
        '2 Rotated 0 And mirrored
        '3 Rotated 180
        '4 Rotated 180 And mirrored
        '5 Rotated 270 And mirrored
        '6 Rotated 270
        '7 Rotated 90 And mirrored
        '8 Rotated 90[/color]
    
    ip.Filters(1).Properties("RotationAngle").Value = lRotAng
    [COLOR=green]' False is default for the following so no real need to set if we dont want to flip
    ' ip.Filters(1).Properties("FlipVertical").Value = False
    ' ip.Filters(1).Properties("FlipHorizontal").Value = False[/color]
    
    Set img = ip.Apply(img) [COLOR=green]' rotateandflip[/color]
    
    [COLOR=green]' Deal with irritating fact that VBS does not have a native  delete file function[/color]
    With CreateObject("Scripting.FileSystemObject")
    	filename=.GetParentFolderName(strFile) & "\" & .getbasename(strFile) &  CStr(lRotAng) & "." & .getextensionname(strFile)
	If .fileexists(filename) Then .deletefile(filename)
    End with
    img.SaveFile filename
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top