Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Resizing images after upload - best method?

Posted on 2007-11-29
13
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

Author Comment

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

Accepted Solution

by:
Rouchie earned 1200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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