webForumDet fria alternativet

bild storlek

ASPur ASP

11 svar · 839 visningar · startad av jns

jnsMedlem sedan dec. 2001132 inlägg
#1

hur kan man få reda på storleken på en bild? Alltså hur många pixlar hög/bred bilden är.

voigtann1Medlem sedan juni 20019 519 inlägg
#2
<%
if Request.Querystring.count = 0 then
response.write "fel"
else

Dim Bild,text
Bild = Request.QueryString("bild")
text = Request.QueryString("text")
response.write "text: "&text &"<br>"
response.write "Bild namn: "&bild &"<br>"

function GetBytes(flnm, offset, bytes)
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize

on error resume next

Set objFSO = CreateObject("Scripting.FileSystemObject")

' First, we get the filesize
Set objFTemp = objFSO.GetFile(flnm)
lngSize = objFTemp.Size
set objFTemp = nothing

fsoForReading = 1
Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)

if offset > 0 then
strBuff = objTextStream.Read(offset - 1)
end if

if bytes = -1 then ' Get All!

GetBytes = objTextStream.Read(lngSize) 'ReadAll

else

GetBytes = objTextStream.Read(bytes)

end if

objTextStream.Close
set objTextStream = nothing
set objFSO = nothing

end function

function lngConvert(strTemp)
lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
end function

function lngConvert2(strTemp)
lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
end function

function gfxSpex(flnm, width, height, depth, strImageType)

dim strPNG 
dim strGIF
dim strBMP
dim strType
strType = ""
strImageType = "(unknown)"

gfxSpex = False

strPNG = chr(137) & chr(80) & chr(78)
strGIF = "GIF"
strBMP = chr(66) & chr(77)

strType = GetBytes(flnm, 0, 3)

if strType = strGIF then 

strImageType = "GIF"
Width = lngConvert(GetBytes(flnm, 7, 2))
Height = lngConvert(GetBytes(flnm, 9, 2))
Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
gfxSpex = True

else

strBuff = GetBytes(flnm, 0, -1) 
lngSize = len(strBuff)
flgFound = 0

strTarget = chr(255) & chr(216) & chr(255)
flgFound = instr(strBuff, strTarget)

if flgFound = 0 then
exit function
end if

strImageType = "GIF"
lngPos = flgFound + 2
ExitLoop = false

do while ExitLoop = False and lngPos < lngSize

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
lngPos = lngPos + 1
loop

if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if

loop
'
if ExitLoop = False then

Width = -1
Height = -1
Depth = -1

else

Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
gfxSpex = True

end if

end if

end function

sub test(flnm)

if right(flnm, 3)="gif" then
Width = lngConvert(GetBytes(flnm, 7, 2))
Height = lngConvert(GetBytes(flnm, 9, 2))
Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)

else

strBuff = GetBytes(flnm, 0, -1) 
lngSize = len(strBuff)
flgFound = 0

strTarget = chr(255) & chr(216) & chr(255)
flgFound = instr(strBuff, strTarget)

strImageType = "JPG"
lngPos = flgFound + 2
ExitLoop = false

do while ExitLoop = False and lngPos < lngSize

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
lngPos = lngPos + 1
loop

if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if

loop

if ExitLoop = "False" then
Width = -1
Height = -1
Depth = -1
else
Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
end if

end if
a=width
b=height
response.write("Mått:<br>" &a &"*" &b &"<br>")
end sub

call test(server.mappath("bilder/" &Bild& ""))

end if
%>
<%
response.write "<img border=""0"" src=""bilder/" &Bild& """><br>"
%>
<html>
<body>

</body>
</html>

men det går att lösa med JavaScript med.. och det e mindre kod med JavaScript... bästa är PHP

voigtann1Medlem sedan juni 20019 519 inlägg
#3

<%
Dim Bild,text,rubrik
Bild = Request.QueryString("bild")
rubrik = Request.QueryString("rubrik")
text = Request.QueryString("text")
%>

<img src="bilder/<%=bild%>" name="bilden"><br>
<%
response.write "Rubrik: <b>"&rubrik &"</b><br>"
response.write "text: "&text &"<br>"
response.write "Bild namn: "&bild &"<br>"
%>
<script language="javascript">
document.write(" Mått: " +document.bilden.width + " * " + document.bilden.height);
</script>

jnsMedlem sedan dec. 2001132 inlägg
#4

nu förstår jag nästan ingenting :q... hur kan man göra med javascript då? jag har en form där man fyller i adressen till bilden... typ http://www.kallessida.com/bild.gif ?

jnsMedlem sedan dec. 2001132 inlägg
#5

nu förstår jag nästan ingenting :q... hur kan man göra med javascript då? jag har en form där man fyller i adressen till bilden... typ http://www.kallessida.com/bild.gif ?

solbulleMedlem sedan mars 20015 287 inlägg
#6

Red. Å jag håller klaffen för voigtan verkar fixa biffen... :r

voigtann1Medlem sedan juni 20019 519 inlägg
#7

om vi spara denna fil som bilder.asp
så anropar du med formen:
ifylnningsformuläret döper vi "show_this_pic"
så här:

<% 
Dim Bild

Bild = Request.Form("show_this_pic")

%> 

<img src="<%=bild%>" name="bilden"><br> 
<% 
response.write "Bild namn: "&bild &"<br>" 
%> 
<script language="javascript"> 
document.write(" Mått: " +document.bilden.width + " * " + document.bilden.height); 
</script>
jnsMedlem sedan dec. 2001132 inlägg
#8

ok nu förstår jag.. tack... men kan man göra detta utan att visa bilden... och kan man få över dessa värden till asp:n?

voigtann1Medlem sedan juni 20019 519 inlägg
#9

du vill att dom ska sparas i en databas???
hmm.. okey... låt mej tänka lite... gör så här:
skapar en databas:

ID
bildnamn
height
width

om du har gjort ett formulär som jag sa.. så gör du så att den ska köra på add.asp (eller vad du vill)

i add.asp:


<% 
Dim Bild

Bild = Request.Form("show_this_pic")

%> 

<img src="<%=bild%>" name="bilden"><br> 
<% 
response.write "Bild namn: "&bild &"<br>" 
%> 
<script language="javascript"> 
document.write(" Mått: " +document.bilden.width + " * " + document.bilden.height); 
</script>

<%
Set Connect = Server.CreateObject("ADODB.Connection")
connect.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("db/databas.mdb")Set RecSet = Server.CreateObject("ADODB.Recordset")

Addera = "SELECT * FROM pcs ORDER BY id DESC"
RecSet.Open Addera, Connect, 3, 3

RecSet.AddNew
RecSet("bildnamn") = Bild
RecSet("width") = document.bilden.width 
RecSet("height") = document.bilden.height 

RecSet.Update
RecSet.Close
Connect.Close
Response.Redirect "/voigtan/olis/admin/Chaform.asp"
%>

eller något.. du kanske måste göra document.bilden.width och height till någon verbial.. du får testa lite

easMedlem sedan sep. 2001269 inlägg
#10
<body onload="document.hbForm.bredd=document.bilden.width; document.hbForm.hojd=document.bilden.height">

<% 
Dim Bild
IF Request.Form("bredd") <> "" THEN
      Bild = Request.Form("show_this_pic")
ELSE
      Bild = Request.Quertstring("bild")
END IF
%>

<form name=hbForm action="denHärFilen.asp?bild=<%=Request.Form("show_this_pic")%>" method=post>
<inut type=hidden name=bredd>
<inut type=hidden name=hojd>
</form>

<img src="<%=bild%>" name="bilden"><br> 
<% 
response.write "Bild namn: "&bild &"<br>" 
%> 
<script language="javascript"> 
document.write(" Mått: " +document.bilden.width+ " * " + document.bilden.height); 
</script>

<a href="#" onclick="document.hbForm.submit()">Spara i databasen</a>

<%
IF Request.Form("bredd") <> "" THEN
Set Connect = Server.CreateObject("ADODB.Connection")
connect.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("db/databas.mdb")Set RecSet = Server.CreateObject("ADODB.Recordset")

Addera = "SELECT * FROM pcs ORDER BY id DESC"
RecSet.Open Addera, Connect, 3, 3

RecSet.AddNew
RecSet("bildnamn") = Bild
RecSet("width") = Request.Form("bredd")
RecSet("height") = Request.Form("hojd")

RecSet.Update

RecSet.Close
Connect.Close
Response.Redirect "enSida.asp"
END IF
%>
</body>

Koden kanske inte funkar helt perfekt och får gjärna förbättras.

PatrikBMedlem sedan mars 20002 836 inlägg
#11

med javascript på klientsidan så kan du kolla hur bred bilden är, hur hög den är och hur många byte den är på.
Du behöver alltså inte ladda upp hela bilden för att kolla detta.

Testa denna:

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT language="javascript">
function imgSize(obj, w, h, fs){
	var imgpath = obj.value;
	var imgTemp = new Image();
	imgTemp.src = imgpath;

	// -- kontrollera hur bred bilden är --
	if (imgTemp.width >= w){
		alert('Bilden är för bred.\nDen får max. vara ' + w + ' pixlar bred.\n\nVälj en annan bild.');
		document.location.reload();
	
	// -- kontrollera bildens höjd --
	}else if(imgTemp.height >= h){
		alert('Bilden är för hög.\nDen får max. vara ' + h + ' pixlar hög.\n\nVälj en annan bild.');
		document.location.reload();
	
	// -- kontrollera hur stor bilden är i bytes --
	}else if(imgTemp.fileSize > fs){
		alert('Bilden får inte vara mer än ' + fs + ' byte stor.\n\nVälj en annan bild.');
		document.location.reload();
	}
	imgTemp.src = '';
}
</SCRIPT>
</HEAD>

<BODY bgcolor="#FFFFFF" text="#000000">
<FORM name="form1" method="post" action="test.asp" enctype="multipart/form-data">
  <INPUT type="file" name="file" onChange="imgSize(this, 100, 50, 100);">
  <INPUT type="submit" name="Submit" value="Submit">
</FORM>
</BODY>
</HTML>

anropas så här imgSize(this, bildensbredd, bildenshöjd, bildensstorlekibyte)

finns en hel del "luckor" i koden ..... men, så är jag ingen javascriptare. ta denna kod in i det forumet så hjälper dem dig vidare för att få den att känna av bilden i eventet onSubmit istället + att den ska kika efter filändelsen.

ändrade så att den reloadade sidan och då blir det ingen bild som kan laddas upp :e

cya,
PatrikB

WickmarkMedlem sedan juli 2003162 inlägg
#12

voigtann1 skrev:

<%
if Request.Querystring.count = 0 then
response.write "fel"
else

Dim Bild,text
Bild = Request.QueryString("bild")
text = Request.QueryString("text")
response.write "text: "&text &"<br>"
response.write "Bild namn: "&bild &"<br>"




function GetBytes(flnm, offset, bytes)
Dim objFSO
Dim objFTemp
Dim objTextStream
Dim lngSize

on error resume next

Set objFSO = CreateObject("Scripting.FileSystemObject")

' First, we get the filesize
Set objFTemp = objFSO.GetFile(flnm)
lngSize = objFTemp.Size
set objFTemp = nothing

fsoForReading = 1
Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)

if offset > 0 then
strBuff = objTextStream.Read(offset - 1)
end if

if bytes = -1 then ' Get All!

GetBytes = objTextStream.Read(lngSize) 'ReadAll

else

GetBytes = objTextStream.Read(bytes)

end if

objTextStream.Close
set objTextStream = nothing
set objFSO = nothing

end function

function lngConvert(strTemp)
lngConvert = clng(asc(left(strTemp, 1)) + ((asc(right(strTemp, 1)) * 256)))
end function

function lngConvert2(strTemp)
lngConvert2 = clng(asc(right(strTemp, 1)) + ((asc(left(strTemp, 1)) * 256)))
end function


function gfxSpex(flnm, width, height, depth, strImageType)

dim strPNG 
dim strGIF
dim strBMP
dim strType
strType = ""
strImageType = "(unknown)"

gfxSpex = False

strPNG = chr(137) & chr(80) & chr(78)
strGIF = "GIF"
strBMP = chr(66) & chr(77)

strType = GetBytes(flnm, 0, 3)

if strType = strGIF then 

strImageType = "GIF"
Width = lngConvert(GetBytes(flnm, 7, 2))
Height = lngConvert(GetBytes(flnm, 9, 2))
Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)
gfxSpex = True


else

strBuff = GetBytes(flnm, 0, -1) 
lngSize = len(strBuff)
flgFound = 0

strTarget = chr(255) & chr(216) & chr(255)
flgFound = instr(strBuff, strTarget)

if flgFound = 0 then
exit function
end if

strImageType = "GIF"
lngPos = flgFound + 2
ExitLoop = false

do while ExitLoop = False and lngPos < lngSize

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
lngPos = lngPos + 1
loop

if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if

loop
'
if ExitLoop = False then

Width = -1
Height = -1
Depth = -1

else

Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
gfxSpex = True

end if

end if

end function

sub test(flnm)

if right(flnm, 3)="gif" then
Width = lngConvert(GetBytes(flnm, 7, 2))
Height = lngConvert(GetBytes(flnm, 9, 2))
Depth = 2 ^ ((asc(GetBytes(flnm, 11, 1)) and 7) + 1)


else

strBuff = GetBytes(flnm, 0, -1) 
lngSize = len(strBuff)
flgFound = 0

strTarget = chr(255) & chr(216) & chr(255)
flgFound = instr(strBuff, strTarget)

strImageType = "JPG"
lngPos = flgFound + 2
ExitLoop = false

do while ExitLoop = False and lngPos < lngSize

do while asc(mid(strBuff, lngPos, 1)) = 255 and lngPos < lngSize
lngPos = lngPos + 1
loop

if asc(mid(strBuff, lngPos, 1)) < 192 or asc(mid(strBuff, lngPos, 1)) > 195 then
lngMarkerSize = lngConvert2(mid(strBuff, lngPos + 1, 2))
lngPos = lngPos + lngMarkerSize + 1
else
ExitLoop = True
end if

loop

if ExitLoop = "False" then
Width = -1
Height = -1
Depth = -1
else
Height = lngConvert2(mid(strBuff, lngPos + 4, 2))
Width = lngConvert2(mid(strBuff, lngPos + 6, 2))
Depth = 2 ^ (asc(mid(strBuff, lngPos + 8, 1)) * 8)
end if



end if
a=width
b=height
response.write("Mått:<br>" &a &"*" &b &"<br>")
end sub


call test(server.mappath("bilder/" &Bild& ""))







end if
%>
<%
response.write "<img border=""0"" src=""bilder/" &Bild& """><br>"
%>
<html>
<body>

</body>
</html>

men det går att lösa med JavaScript med.. och det e mindre kod med JavaScript... bästa är PHP

Hur gör jag om jag vill få precis alla filerna i en speciell mapp att bli uppradade med både höjd och bredd utskrivet ?
Tex:
< a href="LaggTillIDatabas.asp?hojd=[HÖJD]&bredd=[BREDD]" >

149 ms totalt · 3 externa anrop · v20260731065814-full.29ac60f6
141 ms — hämta forumlista (db)
0 ms — hämta statistik (cache)
146 ms — hämta tråd, inlägg och bilagor (db)