Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

MBS - Image reduction


Recommended Posts

Good morning,

I am having an issue with image reduction. 

Below is a snippet of code being used on PSOS.  This is one image of 12 in a record that the image is reduced enough for it to be emailed out.

If [ MBS( "Container.GetTotalSize" ; Job::Photo_Image_01 ) > $_size_limit ]
Set Variable [ $Image ; Value: MBS("GMImage.NewFromContainer"; Job::Photo_Image_01) ]
Set Variable [ $r ; Value: MBS ("GMImage.AutoOrient"; $Image ) ]
Set Variable [ $r ; Value: MBS( "GMImage.Scale" ; $Image ; GetAsText($_reduction) & "%" ) ]
Set Field [ Job::Photo_Image_temp Job::Photo_Image_temp ; MBS("GMImage.WriteToPNGContainer"; $Image) MBS("GMImage.WriteToPNGContainer"; $Image) ]
Commit Records/Requests [ With dialog: Off Off ]
Set Variable [ $_new_image ; Value: Job::Photo_Image_temp ]
Set Variable [ $r ; Value:  MBS("GMImage.Free";$Image) ]
Set Field [ Job::Photo_Image_01 Job::Photo_Image_01 ; $_new_image $_new_image ]
Commit Records/Requests [ With dialog: Off Off ]
End If

Using the variables:
$_size_limit = 1500000
$_reduction = 75 

Images being supplied are from iPhone or iPad and come through as 4000x3000 pixel JPEG format files.  Camera format is set to "Most Compatible" because HEIC have me a bit of grief.

Now, 99% of the time it works very well, and there are 12 images in this record. If I change any of the images, then I have to run the reduction script again then some of the images (unable to find any consistency yet as to which ones) become corrupt and MBS reports a error with the file reference (in the image container).

Process is that the image starts in Job::Photo_Image_01, gets reduced into the temporary field of Job::Photo_Image_temp, then put back into the original field.  I thought it may not be completing the write in time, so added Commits - it didn't help. I have not tried adding a small delay into the script yet - perhaps to give it time to complete perhaps?

I have it loop through this process several times until the file size is achieved.  When I tried making it a specific resolution, it often just corrupted the photo - possibly something I am doing wrong there - but I know would be less steps that the above. 

My question, has anybody got a better way to do the above?  As I say, the process works well for the vast majority of the time, but occasionally breaks the image, but I'm not sure where and why it is breaking.

Cheers,

Greg

 

 

 

 

 

Link to comment
Share on other sites

Is that an older plug-in? I thought we fixed the bug where it copied things twice when copying script text.

 

Your script should check with MBS("IsError") at least after GMImage.WriteToPNGContainer to only use the result if it is not an error.

To find the issue, also use MBS("Trace"; $path) to write a log file with the plugin calls.

 

Another thing is that you save as PNG. You may read a JPEG and save as PNG, which makes it bigger. You could check if GMImage.GetMagick returns JPEG and then save as JPEG.

Link to comment
Share on other sites

Hi MBS,

Thanks for getting back to me.

It's version 14.2.0.06 (May 2024) so is that recent enough?
Copying script text? Do you mean the block of code I submitted? Or?...

Despite making the data larger as PNG, would I still be best off using WriteToPNGContainer or perhaps using WriteToJPEGContainer? Happy to stay with JPG.
Do you think it may be the change of format causing this problem?
They are photos being used, and as I'm already reducing the size, I'm reducing the quality I suppose. Having them reduced from around 2MB down to 600K or so is fine for this task, so they don't need to be high resolution photos. 

Cheers,

Greg

 

Link to comment
Share on other sites

Yes, the version of the plugin you used to copy text has a bug and duplicates some text. That was fixed in 14.3.

 

Please make a trace log file to record calls and let us know what it says.

I would assume you get sometimes an error for loading the image. So if you check for an error, you could avoid overwriting your image in the container with an error message.

You could check if GMImage.GetMagick returns JPEG, and then do GMImage.WriteToJPEGContainer otherwise GMImage.WriteToPNGContainer function.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.