Solved

Resizing images after upload - best method?

Posted on 2007-11-29
13
351 Views
Last Modified: 2012-05-05
I have the following code to upoad a selection of images to a folder on ym server and it also inserts a record for each image uploaded.

What I would like to do is add in 2 aspjpeg calls to resize the original image and also create a 120 wide thumbnail keeping all proportions contraint.

I know the aspjpeg code should be  placed in the loop and should look like this

      'Create resized image
      Set Jpeg = Server.CreateObject("Persits.Jpeg")
      Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName
      L = 800
      Jpeg.Width = L
      Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth
      Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName
      
      'Create Thumbnail
      Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName
      L = 120
      Jpeg.Width = L
      Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth
      Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & REPLACE(myfile.FileName,".jpg","_thumb.jpg")

'Create Thumbnails, resize and insert into db

for each myfile in Upload.Files

	'Response.write("File " & myfile.Path & " uploaded success.<br>")

	Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

	MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

	set adoCn = Server.CreateObject("ADODB.Connection")

	adoCn.Open MM_TaxiMediaPOP_STRING

	arrFileParts = SPLIT(myfile.FileName,"_")

	strCity = arrFileParts(2)

	strRegistration = arrFileParts(3)

	strView = REPLACE(arrFileParts(4),".jpg","")

	strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & myfile.FileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

	adoCn.Execute strSQL

	adoCn.close

next

Open in new window

0
Comment
Question by:clearchannel
  • 7
  • 6
13 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 20372454
Here is the code as part of the loop.  Note that you might receive an error because you are trying the save the resized file over the top of the original, which might cause file sharing/permission problems...


Dim Jpeg

	for each myfile in Upload.Files

	Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

	MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

	set adoCn = Server.CreateObject("ADODB.Connection")

	adoCn.Open MM_TaxiMediaPOP_STRING

	arrFileParts = SPLIT(myfile.FileName,"_")

	strCity = arrFileParts(2)

	strRegistration = arrFileParts(3)

	strView = REPLACE(arrFileParts(4),".jpg","")

	strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & myfile.FileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

	adoCn.Execute strSQL

	adoCn.close

	' Resize

	Set Jpeg = Server.CreateObject("Persits.Jpeg")

	Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Jpeg.Width = 800

	Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth

	Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Set Jpeg = Nothing

	Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Jpeg.Width = 120

	Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth

	Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & REPLACE(myfile.FileName,".jpg","_thumb.jpg")

	Set Jpeg = Nothing

next

Open in new window

0
 
LVL 25

Expert Comment

by:Rouchie
ID: 20372456
Sorry I've spotted an error...
Dim Jpeg

	for each myfile in Upload.Files

	Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

	MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

	set adoCn = Server.CreateObject("ADODB.Connection")

	adoCn.Open MM_TaxiMediaPOP_STRING

	arrFileParts = SPLIT(myfile.FileName,"_")

	strCity = arrFileParts(2)

	strRegistration = arrFileParts(3)

	strView = REPLACE(arrFileParts(4),".jpg","")

	strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & myfile.FileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

	adoCn.Execute strSQL

	adoCn.close

	' Resize

	Set Jpeg = Server.CreateObject("Persits.Jpeg")

	Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Jpeg.Width = 800

	Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth

	Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Set Jpeg = Nothing

	Set Jpeg = Server.CreateObject("Persits.Jpeg")

	Jpeg.Open Server.MapPath("/") & "/imgs/campaigns" & myfile.FileName

	Jpeg.Width = 120

	Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth

	Jpeg.Save Server.MapPath("/") & "/imgs/campaigns" & REPLACE(myfile.FileName,".jpg","_thumb.jpg")

	Set Jpeg = Nothing

next

Open in new window

0
 

Author Comment

by:clearchannel
ID: 20372721
This is close but I think there is some issue relating to called aspjpeg from the loop/upload object I am using as I keep receiving errors telling me there's been a server 500 error. :(


dim uploadsDirVar, Upload, myfile, campaignFolder, strFileName

Dim arrFileParts, strCity, strRegistration, strView

uploadsDirVar = Server.MapPath("/") & "/imgs/campaigns"

Set Upload = New FreeASPUpload

Upload.Save(uploadsDirVar)

Dim Jpeg

	for each myfile in Upload.Files

	strFileName = myfile.FileName

	Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

	MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

	set adoCn = Server.CreateObject("ADODB.Connection")

	adoCn.Open MM_TaxiMediaPOP_STRING

	arrFileParts = SPLIT(strFileName,"_")

	strCity = arrFileParts(2)

	strRegistration = arrFileParts(3)

	strView = REPLACE(arrFileParts(4),".jpg","")

	strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & strFileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

	adoCn.Execute strSQL

	adoCn.close

	

	Dim Jpeg

	Set Jpeg = Server.CreateObject("Persits.Jpeg")

	jpeg.PreserveAspectRatio = True

	Jpeg.Open Server.MapPath("/") & Replace("/imgs/campaigns/","/","\")  & strFileName

	L = 800

	If jpeg.OriginalWidth > jpeg.OriginalHeight Then

	   jpeg.Width = L

	Else

	   jpeg.Height = L

	End If

	Jpeg.Save Server.MapPath("/") & Replace("/imgs/campaigns/","/","\")  & strFileName

	Jpeg.Open Server.MapPath("/") & Replace("/imgs/campaigns/","/","\")  & strFileName

	L = 150

	If jpeg.OriginalWidth > jpeg.OriginalHeight Then

	   jpeg.Width = L

	Else

	   jpeg.Height = L

	End If

	Jpeg.Save Server.MapPath("/") & Replace("/imgs/campaigns/","/","\")  & REPLACE(strFileName,".jpg","_thumb.jpg")

	Set Jpeg = Nothing

next

set adoCn = Nothing

Open in new window

0
 

Author Comment

by:clearchannel
ID: 20372726
Increased points as this is a little awkwarder than I had hoped :)
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 20372776
Let's simplify the code a bit to make it easier to read.  The Replace / \ part isn't necessary if we wrap the mapPath around the whole lot.  Second, the code seems to be trying to save the image while it is being modified (ASP won't create a temporary version to work with like PhotoShop etc).  I think that is the cause of the error, because ASP cannot overwrite the current file.

So let's try saving to a new directory as a temporary workaround.  In /imgs/campaigns/ create 2 subdirectories, so you have:

   /imgs/campaigns/
   /imgs/campaigns/resized/
   /imgs/campaigns/thumbs/

Grant ASP write permission to all 3 directories.

Then try this:



Dim Jpeg

for each myfile in Upload.Files

        strFileName = myfile.FileName

        Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

        MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

        set adoCn = Server.CreateObject("ADODB.Connection")

        adoCn.Open MM_TaxiMediaPOP_STRING

        arrFileParts = SPLIT(strFileName,"_")

        strCity = arrFileParts(2)

        strRegistration = arrFileParts(3)

        strView = REPLACE(arrFileParts(4),".jpg","")

        strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & strFileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

        adoCn.Execute strSQL

        adoCn.close

        

        Set Jpeg = Server.CreateObject("Persits.Jpeg")

        jpeg.PreserveAspectRatio = True

        Jpeg.Open Server.MapPath("/imgs/campaigns/" & strFileName)

        L = 800

        If jpeg.OriginalWidth > jpeg.OriginalHeight Then

           jpeg.Width = L

        Else

           jpeg.Height = L

        End If

        Jpeg.Save Server.MapPath("/imgs/campaigns/resized/" & strFileName)

        Set Jpeg = Nothing

        Set Jpeg = Server.CreateObject("Persits.Jpeg")

        Jpeg.Open Server.MapPath("/imgs/campaigns/" & strFileName)

        L = 150

        If jpeg.OriginalWidth > jpeg.OriginalHeight Then

           jpeg.Width = L

        Else

           jpeg.Height = L

        End If

        Jpeg.Save Server.MapPath("/imgs/campaigns/thumbs/" & strFileName)

        Set Jpeg = Nothing

next

set adoCn = Nothing

Open in new window

0
 

Author Comment

by:clearchannel
ID: 20372879
Still have the same 500 error ocurring as though it can not find the images. Now it could be the MultiPowUpload flash tool I am using is causing problems but as far as I am aware it just targets an ASP upload script and then that does all the work i.e. the script I originally showed you.

When I run the code below to manually resize images ASPJpeg works fine and does it job!

Also the redirect is a bit of a pain too as it wont redirect as expected after upload. :(


Set Jpeg = Server.CreateObject("Persits.Jpeg")

Jpeg.PreserveAspectRatio = True

Jpeg.Open Server.MapPath("/imgs/campaigns/" & "TRANSIT__Birmingham_S42FTR_front.jpg")

L = 800

If Jpeg.OriginalWidth > Jpeg.OriginalHeight Then

   Jpeg.Width = L

Else

   Jpeg.Height = L

End If

Jpeg.Save Server.MapPath("/imgs/campaigns/resized/" & "TRANSIT__Birmingham_S42FTR_front.jpg")
 

Jpeg.Open Server.MapPath("/imgs/campaigns/" & "TRANSIT__Birmingham_S42FTR_front.jpg")

L = 150

If Jpeg.OriginalWidth > Jpeg.OriginalHeight Then

   Jpeg.Width = L

Else

   Jpeg.Height = L

End If

Jpeg.Save Server.MapPath("/imgs/campaigns/thumbs/" & "TRANSIT__Birmingham_S42FTR_front.jpg")

Set Jpeg = Nothing

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:clearchannel
ID: 20373540
Do you have anymore answers Rouchie ???
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 300 total points
ID: 20374062
Double check that the browser has "friendly errors" turned off.  Then try running this lot and tell me:

 - if the database gets updated?
 - if the images are saved to /imgs/campaigns/


dim uploadsDirVar, Upload, myfile, campaignFolder, strFileName

Dim arrFileParts, strCity, strRegistration, strView

uploadsDirVar = Server.MapPath("/imgs/campaigns")

Set Upload = New FreeASPUpload

Upload.Save(uploadsDirVar)

Dim Jpeg

for each myfile in Upload.Files

        strFileName = myfile.FileName

        Dim adoCn, strSQL, MM_TaxiMediaPOP_STRING

        MM_TaxiMediaPOP_STRING = "Provider=SQLOLEDB;Server=ukccitest01gsq;User id=WEB_user;Password=Internet;Initial Catalog=TaxiMediaPOP;"

        set adoCn = Server.CreateObject("ADODB.Connection")

        adoCn.Open MM_TaxiMediaPOP_STRING

        arrFileParts = SPLIT(strFileName,"_")

        strCity = arrFileParts(2)

        strRegistration = arrFileParts(3)

        strView = REPLACE(arrFileParts(4),".jpg","")

        strSQL = "INSERT INTO CampaignImages (CampaignID, FileName, City, VehicleRegistration, VehicleView) VALUES ('" & Upload.Form("CampaignID") & "', '" & strFileName & "', '" & strCity& "', '" & strRegistration & "', '" & strView & "')"

        adoCn.Execute strSQL

        adoCn.close

        

        Set Jpeg = Server.CreateObject("Persits.Jpeg")

        jpeg.PreserveAspectRatio = True

        Jpeg.Open Server.MapPath("/imgs/campaigns/" & strFileName)

        L = 800

        If jpeg.OriginalWidth > jpeg.OriginalHeight Then

           jpeg.Width = L

        Else

           jpeg.Height = L

        End If

        Jpeg.Save Server.MapPath("/imgs/campaigns/resized/" & strFileName)

        Set Jpeg = Nothing

        Set Jpeg = Server.CreateObject("Persits.Jpeg")

        Jpeg.Open Server.MapPath("/imgs/campaigns/" & strFileName)

        L = 150

        If jpeg.OriginalWidth > jpeg.OriginalHeight Then

           jpeg.Width = L

        Else

           jpeg.Height = L

        End If

        Jpeg.Save Server.MapPath("/imgs/campaigns/thumbs/" & strFileName)

        Set Jpeg = Nothing

next

set adoCn = Nothing

Open in new window

0
 

Author Comment

by:clearchannel
ID: 20374272
No files uploaded and no resize :( It is definately the call to ASPJpeg that is causing the issues.
For some reason it does not understand the call to the object I don't think.

Here is a screenshot of the error that appears...

http://pop.taximedia.com/screengrab.jpg
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 20374842
Can you create a html page with a few file upload fields that submits to the ASP page above?  That way we can see how far a raw submit handles, taking Flash out of the equation for now.
0
 

Author Comment

by:clearchannel
ID: 20374856
Right I now know what the issue is! hahahahahahaha

There was no declarartion for the variable 'L' - can't believed I missed it from the start!!!!

Just need to work out how to redirect the user now!
0
 

Author Closing Comment

by:clearchannel
ID: 31411626
Thanks again, excellent communication
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 20374914
That has to be done in Flash and not the ASP page.  I didn't see that before without the screen grab.

You need to do a getUrl('somepage.html'); call in Flash after the files are posted.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update field in order 21 122
asp syntax 3 37
Query not working correctly? (or how i want it to... 6 48
Display first 3 lines of text from database field, vbscript asp 4 47
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now