Image manipulation in VBScript
I am back on Windows development. It is not what I prefer but still, it is development so I am right here! 😉
My task was to upload an image from a website (nothing difficult there) but the application has to resize and crop the uploaded image before saving it on the hard drive in order to save space.
Oh, and I forgot to mention that the website is built in VBScript (it would have been too easy if it has been built in ASP.Net…). 🙄
I went across a lot of image management libraries which work with VBScript during my search:
- AspImage from ServerObjects Inc;
- ImageGlue from webSupergoo;
- Active Image Processing Component from Ultra Shareware.
But I must admit that the most difficult part of this task was to find a good and FREE library.
The one I finally picked is ImageMagick.
Once the library is installed (click here to go to the download page), you can use the following code to resize the image to a maximum of 800 pixels and crop the white space around it:
Dim imageMagick Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1") imageMagick.Convert "C:/testimage.jpg", "-fuzz", "10%", "-trim", "-resize", "800x800>", "C:/thumb-testimage.jpg"
Obviously, this tool can do much more than that, but this will be subject of another topic.
Or you can simply read the documentation: http://www.imagemagick.org/Usage/
Using the ‘date’ command in your crontab
Crontab, as most people know, enables users to schedule commands or shell scripts to run periodically at certain times or dates.
The other day, this very useful Linux tool gave me a hard time! 🙁
Indeed, one of my commands wasn’t working in cron but was working perfectly fine when written in a shell console.
The faulty command looked like this:
0 5 * * 3 /data/script.sh > /data/script_`date +%y%m%d`.log 2>&1
If I run this command in a shell console, everything works fine and I get a log file containing today’s date in its filename. However, if I set this command line in my crontab, it doesn’t work and no log file is even created!
Reading the documentation of cron, I discovered the following statement:
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Well, this is good to know, isn’t it? 😉
We need to escape the percent-signs on our command line.
So in order to get our ‘faulty’ command to run in cron, it needs to look like the following:
0 5 * * 3 /data/script.sh > /data/script_`date +\%y\%m\%d`.log 2>&1