Solved

Resizing images after upload - best method?

Posted on 2007-11-29
13
352 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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