...

View Full Version : code logic / layout question - could I do this better?



Spudhead
08-16-2005, 10:56 AM
I'm using Imagemagick (http://www.imagemagick.org) to let users do some basic optimisation on uploaded images.

The API gives me a "Convert" function, into which I pass my list of actions. I need to enable 3 actions, all of which are optional: set the compression quality, set the rotation, set the image sharpness.

Because the "Convert" function is set up to take a list of parameters, I can't just loop through values, building up a string of commands that I can then pass to the function. I've got to pass in each parameter individually, if it's been set.

The way I've done it is below. I think it's inelegant and far too much code (especially as adding further potential 'actions' is going to require exponentially more and more code), but I can't figure out a way of reducing it.

Any suggestions?



set objImageEditor = server.createobject("ImageMagickObject.MagickImage.1")
if strImageQuality <> "" then
if strImageRotation <> "" then
if strImageUnsharp <> "" then
strMsg = objImageEditor.Convert(strImagePath, "-quality", strImageQuality, "-rotate", strImageRotation, "-unsharp", strImageUnsharp, strEditedImagePath)
else
strMsg = objImageEditor.Convert(strImagePath, "-quality", strImageQuality, "-rotate", strImageRotation, strEditedImagePath)
end if
else
if strImageUnsharp <> "" then
strMsg = objImageEditor.Convert(strImagePath, "-quality", strImageQuality, "-unsharp", strImageUnsharp, strEditedImagePath)
else
strMsg = objImageEditor.Convert(strImagePath, "-quality", strImageQuality, strEditedImagePath)
end if
end if
else
if strImageRotation <> "" then
if strImageUnsharp <> "" then
strMsg = objImageEditor.Convert(strImagePath,"-rotate", strImageRotation, "-unsharp", strImageUnsharp, strEditedImagePath)
else
strMsg = objImageEditor.Convert(strImagePath, "-rotate", strImageRotation, strEditedImagePath)
end if
else
if strImageUnsharp <> "" then
strMsg = objImageEditor.Convert(strImagePath, "-unsharp", strImageUnsharp, strEditedImagePath)
else
'no changes
end if
end if
end if
set objImageEditor = nothing

neocool00
08-16-2005, 03:14 PM
What happens if you call this: objImageEditor.Convert(strImagePath, "-quality", strImageQuality, "-rotate", strImageRotation, "-unsharp", strImageUnsharp, strEditedImagePath) and the variables are null? Some components are designed to ignore them if they are optional. I was just curious if you could just call the Convert method and pass everything in and if it was null that the object would just ignore it.

Spudhead
08-16-2005, 03:44 PM
Not a bad idea, I'll have a go. Cheers :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum