Resizing images after upload - best method?

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

clearchannelAsked:
Who is Participating?
 
RouchieCommented:
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
 
RouchieCommented:
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
 
RouchieCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
clearchannelAuthor Commented:
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
 
clearchannelAuthor Commented:
Increased points as this is a little awkwarder than I had hoped :)
0
 
RouchieCommented:
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
 
clearchannelAuthor Commented:
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
 
clearchannelAuthor Commented:
Do you have anymore answers Rouchie ???
0
 
clearchannelAuthor Commented:
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
 
RouchieCommented:
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
 
clearchannelAuthor Commented:
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
 
clearchannelAuthor Commented:
Thanks again, excellent communication
0
 
RouchieCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.