NetTalk Central

NetTalk Web Server => Web Server - Ask For Help => Topic started by: ianburgess on July 17, 2012, 08:05:49 AM

Title: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 17, 2012, 08:05:49 AM
I have a form and a browse, both of which display a thumbnal of a much larger photo. The photos all have different different proportions. The problem is that when I specify the size of the thumbnail image, I have to specify both horizontal and vertical size, but I don't know the proportions and therefore have to specify an arbitrary size that most often results in the image neing squased or stretched.

How can I specify one dimension and have the image automatically resize in proportion to original?

Thanks

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: bshields on July 17, 2012, 06:04:47 PM
Hi Ian,

Due to an old bug in IE (only IE6 and earlier from memory), Bruce's code forces both the width and the height, and if you don't specify one, NT will guess the other from the parameter you did provide.

You have two choices:

1. determine the width and height of your actual image in question (various methods exist for this) and set the values in the correct aspect ratio, or
2. Locate the function NetWebServerWorker.CreateImage in NetWeb.CLW in the LIBSRC folder and comment out these two lines:

  !if loc:height and not loc:width then loc:width = loc:height.
  !if loc:width and not loc:height then loc:height = loc:width.


Apologies to Bruce for my hack.

If you do #2, don't specify either the width or height on your images and for all browsers (except IE6 and maybe IE7) your images will be fine.

Regards
Bill
Title: Re: Photo Thumbnail - resize proportionally
Post by: Bruce on July 17, 2012, 09:22:23 PM
good point Bill - it's probably worth killing those lines of code at some point.
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 12:03:40 AM
Thanks - that did the trick.

An allied question....
By specifying the image size, the "full size" image is still loaded, even though it is displayed as a thumbnail. If the image is very large, then this may cause the page to load slowly, especially if the image is on a browse with a number of lines displayed.

These images are user uploaded, so there is only one version (full size) - is there a way of resizing the image on the server side?
Title: Re: Photo Thumbnail - resize proportionally
Post by: bshields on July 18, 2012, 12:14:56 AM
Yes, but you must use some tool to do it. Personally I use ImageEx (http://www.solidsoftware.de) (but its not reentrant so it must be protected by a critical section). Before ImageEx I used LeadTools.

Basically you are on your own. Its the same a resizing images in a desktop app once you are on the server with the exception of the NT server being heavily threaded.
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 12:21:04 AM
Yes, but you must use some tool to do it. Personally I use ImageEx (http://www.solidsoftware.de) (but its not reentrant so it must be protected by a critical section). Before ImageEx I used LeadTools.

Basically you are on your own. Its the same a resizing images in a desktop app once you are on the server with the exception of the NT server being heavily threaded.

Thanks for the recommendation. Presumably you build in some code that creates a thumbnail version when the main image is uploaded...and I just use the thumbnail image when needed in browses etc?
Title: Re: Photo Thumbnail - resize proportionally
Post by: bshields on July 18, 2012, 12:25:41 AM
yeah, exactly. I create thumbnails after upload and put them in sensible places so i can easily use the appropriate image.

I have a wrapper function for ImageEx so its a one liner to create the thumbnail.
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 12:28:05 AM
yeah, exactly. I create thumbnails after upload and put them in sensible places so i can easily use the appropriate image.

I have a wrapper function for ImageEx so its a one liner to create the thumbnail.

OK great - looks like it's worthwhile investing in ImageEx. Any chance of "borrowing" your wrapper function?

Cheers

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: Bruce on July 18, 2012, 12:43:04 AM
you don't need ImageEx to create thumbnails.
you can use ClarionFreeImage.

Example 26 (File Uploading) contains an example of this - including the necessary FreeImage code to create thumbnails.

see www.clarionfreeimage.com for more information on that side of things.

cheers
Bruce
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 01:32:06 AM
you don't need ImageEx to create thumbnails.
you can use ClarionFreeImage.

Example 26 (File Uploading) contains an example of this - including the necessary FreeImage code to create thumbnails.

see www.clarionfreeimage.com for more information on that side of things.

cheers
Bruce


Thanks Bruce. I have looked at Example 26 and cannot see reference to FreeImage?
Title: Re: Photo Thumbnail - resize proportionally
Post by: bshields on July 18, 2012, 02:08:57 AM
Forgot about clarionfreeimage. I'm sure it works great.
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 03:27:37 AM
Bruce

Since I cannot find reference to Freeimage code in example, I looked at embedding the following (slightly changed for my filenames) in the WebHandler procedure:

 If ThumbImage.iImage.Load('C:\images\ImageFile.jpg')
    ThumbImage.iImage.Thumbnail(64, FILTER_BSPLINE)
    ThumbImage.iImage.SaveAs('C:\images\ImageFileThumb.jpg')
  End

However, If I embed in the "RenameFile" point, it is too early and the uploaded file has not yet been uploaded - where should I embed so that it is after the file has been uploaded?

Thanks

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: Bruce on July 18, 2012, 08:02:51 AM
you could put it in webHandler, but I think it's better in the Form itself.
See the MailboxesFormControl in the example including;

ThumbImage FreeImageClass

and

  If p_web.GetValue('MAI:MailBoxPicture')
    If ThumbImage.iImage.Load(clip(p_web.site.WebFolderPath) & '\' & p_web.GetValue('MAI:MailBoxPicture'))
      ThumbImage.iImage.Thumbnail(64, FILTER_BSPLINE)
      ThumbImage.iImage.SaveAs(clip(p_web.site.WebFolderPath) & '\' & p_web.GetValue('MAI:MailBoxPicture') & '.thumb.jpg')
      p_web.SetValue('mai:mailboxthumbnail',p_web.GetValue('MAI:MailBoxPicture') & '.thumb.jpg')
      mai:mailboxthumbnail = p_web.GetValue('mai:mailboxthumbnail')
    End
  end


cheers
Bruce
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 18, 2012, 08:30:53 AM
Thanks Bruce.

My example 26 seems not to include the Freeimage stuff. Would you be able to upload the latest example (for C6) to the newsgroup?

Thanks

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: bshields on July 18, 2012, 03:59:21 PM
Hi Ian.,

Left me know how you get on with ClarionFreeImage. If it is reentrant (ie. thread safe), unlike ImageEx i might swap over and use it for my thumbnails.

Also, and you probably thought of this. I too have many occasions where a customer may be uploading images. As our customers wouldn't know a megapixel from latte I also accept their original image and if it is above a certain arbitrary size (say 3MP) i'll thumbnail their crazy 10MP image down to 3MP or similar so I don't have to keep shuffling around some huge file just because they bought a new camera and don't know how to use it.

Regards
Bill
Title: Re: Photo Thumbnail - resize proportionally
Post by: bruce2 on July 18, 2012, 09:37:24 PM
what build of NetTalk are you on Ian?
the current build of the example is attached, but it's been in NetTalk a while, so if you don't have it then either you're using a very old build, or you're not looking for your examples in the right place.

Cheers
Bruce


[attachment deleted by admin]
Title: Re: Photo Thumbnail - resize proportionally
Post by: Larry Sand on July 19, 2012, 06:55:06 AM
Left me know how you get on with ClarionFreeImage. If it is reentrant (ie. thread safe), unlike ImageEx i might swap over and use it for my thumbnails.

Bill,

It is thread safe.  I use it to process product images in our server.  We have a rules based system that fetches product images from vendors that we have agreements with and makes a number of rescaled images to match the needs of the server.  The original images isn't even written to disk, the class can read the image from the buffer NetTalk receives it into, then make the rescaled images and discard the original too large image. The FreeImageClass contains simple methods to rescale, thumbnail, crop, rotate and grayscale.   

We did find a problem with some images that had invalid metadata would gpf the freeimage.dll, they fixed the bug and if you have the latest version of the dll from sourceforge it won't be a problem.

Larry Sand

Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 19, 2012, 07:02:44 AM
Larry

Is there any documentation anywhere on the various methods in Freeimage?

Thanks

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: Larry Sand on July 19, 2012, 07:57:50 AM
Ian,

The FreeImageClass is a wrapper around the functions in the FreeImage.dll and those are documented in the FreeImage docs on sourceforge.  Open FreeImCl.inc/clw and look at the iImage interface definition for many of the things you can do without the image control.  The code for the image control class is in cfiImgCt.inc/clw but you wouldn't use that when you only want to manipulate an image without displaying it.

For instance, here are the rescale methods:
Rescale             Procedure(*Real fPercentX, *Real fPercentY, UNSIGNED fiFilter),BOOL,Proc
Rescale             Procedure(*Real fPercent, UNSIGNED fiFilter),BOOL,Proc
Rescale             Procedure(UNSIGNED nDstWidth, UNSIGNED nDstHeight, UNSIGNED fiFilter),BOOL,Proc
Rescale             Procedure(*iImage dstImage, Real fPercent, UNSIGNED fiFilter),BOOL,Proc
Rescale             Procedure(*iImage dstImage, UNSIGNED nDstWidth, UNSIGNED nDstHeight, UNSIGNED fiFilter),BOOL,Proc


if you look at the implementation in FreeImCl.clw you'll find that four of the methods all call the fifth one using different arguments.  And that fifth method calls FreeImage_Rescale() the dll function.  You can find the freeimage api prototyped in FreeImg.inc. 

You'll notice that a number of methods take or return iImage interfaces.  To use these you'll need more than one freeimageclass object to pass as an argument.  As an example the Thumbnail method declares a temp freeimage class to pass the interface (eventually) to the rescale method like this:

!--------------------------------------------------------------------------
FreeImageClass.iImage.Thumbnail           Procedure(UNSIGNED nDstWidth, FREE_IMAGE_FILTER fiFilter)
!--------------------------------------------------------------------------
dstImageCl    FreeImageClass
  Code
  dstImageCl.bRetainImage = True
  Self.iImage.Thumbnail(dstImageCl.iImage, nDstWidth, fiFilter)
  Return Self.iImage.ReplaceImage(dstImageCl.pImage)



The methods work like this because many of the freeimage functions return a new image, setting the destination image's bRetainImage property ensures the temp object does not destroy the image while it's cleaning things up.

So in general, if you want to change the image you loaded, use the methods that don't have an iImage interface as a parameter.  And declare another freeimage object to pass to the ones that do.

There are some example apps and prjs installed too.

If you have questions, just shout.

Larry
Title: Re: Photo Thumbnail - resize proportionally
Post by: ianburgess on July 19, 2012, 08:19:23 AM
Hi Larry

Many thanks for your comprehensive answer.

I have started to experiment with FreeImage and have successfully created scaled copies of uploaded photos. I will delve into the clw/inc files and see what else is possible.

I am using "FILTER_BSPLINE" option with the Thumbnail method, but see that there are other possibilities - is FILTER_BSPLINE the best choice and what are the merits of the others?

Thanks again.

Ian
Title: Re: Photo Thumbnail - resize proportionally
Post by: Larry Sand on July 19, 2012, 08:52:53 AM
Ian,

You're welcome.  There's a discussion of the resampling filters in the appendix of the freeimage documentation.  Though you'll find that almost any of them will work fine for general down-sampling.  It does depend somewhat on the image and I assume you won't know anything about them.  Some of the filters are faster than others with box (FILTER_BOX) being the fastest.

Larry