Resizing JPEGS in Delphi to create Thumbnails for posting to the web

May 12, 2006

Share this:
  • Reddit
  • Digg
  • del.icio.us
  • Tumblr
  • FriendFeed
  • email

I thought I might post this example… It works really well with the Light box java script plugin for WordPress… It is a single file (DPR) Delphi console app that can be used in a batch file to resize jpegs to any percentage of the original size.

I use it with ZTree by tagging all of the JPegs I wish to resize and then hitting CTRL B. Ctrl B will create a batch file out of all of the tagged files with the command line that I enter. So the command line I use is “ResizeJPeg %4.%5″ and 10 seconds later, I have a large set of images and thumbnails I can post to the web.

BTW I use net drive to make drive X: be a my FTP directory. NetDrive is WONDERFUL!!! I dare say it is ALMOST as useful as ZTree…. Almost….

For those who have no care to be interested in compiling Delphi Source code, here is the EXE: ResizeJPEG.exe

[delphi]
// ****************************************************************************
// ResizeJPEG.DPR 01/Jul/2004
// Written by Brian Layman (AKA Capt. Queeg)
// Visit him at http://www.TheCodeCave.com
//
// This routine was written to help resize jpegs for posting to the web
//
//
// Warning: This routine opens your images and replaces files without
// warnings. It is not ever supposed to edit your original image, but
// if something unexpected happens, you could lose some important images.
// Understand every line of this example before you run it. Or better yet,
// do not run this example at all. You should stop right now and erase the
// files. For if it causes blue smoke to be emitted from your network card,
// if it erases all users from your computer, or if it makes your sister
// break up with her lawyer boyfriend and start dating a caver, it is
// not my fault. (Actually that last one might be an improvement, but
// it is still not my fault.) But the fact of the manner is, anything
// could happen when you mess with the registry and this example IS,
// in all seriousness, dangerous.
//
// Usage: ResizeJPEG.exe JPGNAME.JPG <-- Resizes the image to 10% of original
// ResizeJPEG.exe JPEGNAME 50 <-- Resizes the image to 50% of original
//
// License - If this helps you - Great! Use it, modify it share it.
//
// Donations - If this project really helps you out, feel free to make a $5
// (US) token donation via Paypal to Brian@TheCodeCave.com or just leave a
// comment on my blog and include your country of origin.
//
// History:
// 01/Jul/2004 - BL - Created
//
// ****************************************************************************
program ResizeJPEG;

uses
ExceptionLog,
Forms, Classes, JPEG, Graphics, SysUtils;

{$R *.RES}

{******************************************************************************
DoResize
This routine receives two JPEGs and the percentage (as a whole number) of
the original size. It only does aspect ratio resizing.
The creation and freeing of the JPEGs must be done outside this routine.
srcJPEG must contain an image. destJPEG's contents will be overwritten.
Passing a zero or negative percentage will
******************************************************************************}
procedure DoResize(srcJPEG, destJPEG: TJPEGImage; const Pct: Integer; BMPName: String = '');
var
Bitmap: TBitmap;
begin // DoResize

if (not(Assigned(srcJPEG) and Assigned(destJPEG)))
then raise Exception.Create('There has been a programming error. ' +
'The JPEG parameters passed to DoResize have not ' +
'been initialized.');

// Error out if we don't have a valid source image
if (srcJPEG.Empty)
then raise Exception.Create('The parameters passed cannot produce a valid image. ' +
'The source image is blank.');

// Error out if we don't have a valid source image
if (Pct <= 0)
then raise Exception.Create('The parameters passed cannot produce a valid image. ' +
'The percentage (' + IntToStr(Pct) + '%) is invalid.');

Bitmap := TBitmap.Create;
try
Bitmap.Width := Trunc(srcJPEG.Width * (PCT/100));
Bitmap.Height := Trunc(srcJPEG.Height * (PCT/100));
Bitmap.Canvas.StretchDraw(Bitmap.Canvas.ClipRect, srcJPEG);
destJPEG.Assign(Bitmap);
if (BMPName <> ”)
then Bitmap.SaveToFile(BMPName);
finally
Bitmap.Free;
end;
end; // DoResize

var
JPEGOrig, JPEGTemp: TJPEGImage;
ImgName, imgExt: String;
Percentage: Integer;
CreateBMP: Boolean;
begin
Application.Initialize;
// Retreive first parameter and strip off any path info.
// That’s the file name. This now includes the extension.
ImgName := LowerCase(ExtractFileName(ParamStr(1)));

// Determine the extension
ImgExt := ExtractFileExt(ImgName);

// If there is an extension, remove it from the file name.
// If there is no extension, assume ‘.jpg’ for now.
if (ImgExt <> ”)
then ImgName := copy(ImgName, 1, Length(ImgName) – Length(ImgExt))
else ImgExt := ‘.jpg’;

// The third parameter is for the percentage of change.
// If not provided, it defaults to 10%
Percentage := StrToIntDef(ParamStr(2), 10);

CreateBMP := Boolean(StrToIntDef(ParamStr(3), 0));

// This if statement both verifies that the file is there and that it now has
// a .jpg or .jpeg extension. If these conditions are not met, the program
// exits without an error.
if (FileExists(ImgName + ImgExt) and ((ImgExt = ‘.jpg’) or (ImgExt = ‘.jpeg’)))
then begin
// Create the object for manipulating the new image
JPEGTemp := TJPEGImage.Create;
try
// Create the object for storing the original image
JPEGOrig := TJPEGImage.Create;
try
// Load the original
JPEGOrig.LoadFromFile(ImgName + ImgExt);

// Call the subroutine for resizing the original and putting it in the
// temporary object.
if (CreateBMP)
then DoResize( JPEGOrig, JPEGTemp, Percentage, ImgName + ‘_s.bmp’)
else DoResize( JPEGOrig, JPEGTemp, Percentage);

// Save the new image with a “_s” tacked onto the file name.
JPEGTemp.SaveToFile(ImgName + ‘_s’ + ImgExt);
finally // try around JPEGOrig usage
// Free the original object
JPEGOrig.Free;
end; // try around JPEGOrig usage
finally // try around JPEGTemp usage
// Free the new/temp object
JPEGTemp.Free;
end; // try around JPEGTemp usage
end; // if fileexists
end.[/delphi]

Comments

No Comments Yet.

Got something to say?





Who is Brian Layman

I am a WordPress expert living in North East Ohio. I am part of the ever expanding Open Source Internet workforce. I am able to stay at home, with my wife and four home schooled kids, while working as the Senior Developer for b5media - a blogging network that has hosted over 300+

I co-host the NEO WordPress Monthly meetup. I am the board chair of our local church. I host and have provided development services for clients such TV personalities Rhett and Link as well as corporations such as Borland International.

In my spare time I try to sneak out, canoe, mountain bike and camp as often as I can. Sometimes I also defend the earth against zombies and aliens, but usually not during the camping trips.

Services Provided

In providing hosting, email, theme and plugin development to my clients, I function as a single point of contact answering to the needs of their expanding sites.

My service portfolio includes but is not limited to WordPress hosting, optimization, theme development and custom plugin creation. Community creation via vBulletin, Ning and BuddyPress and bbpress

I also am well experienced in site conversion, transition and merges. To clarify this, website technologies change and giving up your data is not an option. I have transitioned literally hundreds of sites from one platform to another.

viagra 50 mg indian version of viagra cialis cheapest viagra india online viagra cost comparison viagra for sale without prescription generic tadalafil online buy viagra in korea indian levitra discount cialis online viagra prescription over the counter vardenafil cialis otc cialis no rx cialis 30 mg viagra ranbaxy buy levitra in uk cialis low price tadalafil tablets 10mg cheap viagra fast shipping cheap generic levitra cialis discount cialis 5mg viagra discount prices buy levitra without prescription vardenafil online generic levitra canada viagra professional price cheapest sildenafil citrate indian version of cialis viagra lowest price viagra online prescriptions tadalafil 10mg levitra over the counter levitra prescriptions online buy viagra without a prescription liquid tadalafil citrate buy viagra prescription online tadalafil 20mg india india viagra generic sildenafil citrate for sale vardenafil hcl 10mg cialis discount coupon buy levitra australia viagra over the counter in canada liquid sildenafil tadalafil price comparison viagra cost in india cialis mail order sildenafil sales buy vardenafil cialis offer cheap vardenafil generic cialis no prescription viagra tabs generic indian names viagra price canada vardenafil hcl 20 mg generic viagra without prescription viagra by scilla biotechnologies buy generic cialis free viagra viagra over the counter viagra pills kamagra 100 mg cialis from india tadalafil australia tadalafil 20mg tablets tadalafil soft tabs sildenafil pills viagra no prescription required generic viagra paypal tadalafil online indian viagra cost tadalafil online pharmacy generic soft viagra sildenafil soft tablets viagra generic names buy viagra in ireland levitra without prescription levitra online purchase cialis pill indian tadalafil levitra 5mg cialis cost per pill tadalafil oral jelly sildenafil no prescription vardenafil price generic cialis 10mg cheap cialis no prescription order sildenafil citrate indian generic viagra blue viagra buy cialis usa apcalis 20mg tablets viagra overnight delivery sildenafil india purchase viagra without a prescription viagra prescriptions order viagra without prescription viagra with no prescription levitra for sale purchase viagra canada discount levitra viagra 200mg cheap viagra 100mg cialis overnight delivery buy sildenafil online viagra made in india cialis tabs 10mg viagra indian pharmacy viagra for sale in ireland viagra uk prices buy viagra in europe generic cialis india levitra online viagra for sale india buy viagra in dublin generic cialis soft tabs viagra 50mg cost generic sildenafil 100mg tadalafil generic viagra super active 100 mg kamagra 100mg sildenafil 100 mg tablets cialis no prescription viagra low price online cialis suhagra tablets buy cialis daily use tadalafil sample cialis prices viagra prescription online buy cialis pill kamagra from india cialis online levitra mg vigora india vardenafil 10 mg sildenafil citrate 100mg buy viagra in india buy cialis professional viagra in india buy viagra in singapore generic revatio viagra substitutes sildenafil canada viagra no script cheap kamagra viagra retail price cheap lovegra order viagra uk buy cialis in mexico viagra prescription price purchase cialis online without prescription online cialis prescription ranbaxy caverta buy viagra in hong kong sildenafil price cialis mastercard buy viagra in england viagra mail order canada cialis tablets for sale order cialis cialis soft tabs generic levitra india tadalafil prices cheap sildenafil citrate tablets cialis online prescriptions cialis 5 mg daily levitra prices prescriptions viagra viagra over the counter alternative cialis 20 mg tablets cialis generic india cialis prescribing cialis 20mg daily sildenafil 50 mg viagra drug prices tadalafil generic india cialis sale viagra prices buy viagra 50 mg levitra pharmacy buy viagra generic viagra prescription drug cialis daily cost vardenafil uk viagra soft tabs online buy viagra super active cialis 10mg price 25mg viagra silagra 100mg online viagra prescriptions cialis prescription cheap cialis india revatio 20 mg indian equivalent of viagra tadalafil india viagra capsules cheapest viagra buy cialis without prescription tadalafil overnight cheap tadalafil online purchase viagra online no prescription