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

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]

Add a Comment

Your email address will not be published. Required fields are marked *