0

Whenever Image is uploaded to the Stock item page, I have to resize the image to thump size and upload the copy of the same.

How do I override the upload function to add my logic?

1 Answers1

1

The upload function is HandleUpload of the PXImageUploader control in PX.Web.UI. You can try to overwrite that function and then replace the control on the page with yours.

Another way will be to handle the resizing inside the InventoryItemMaint graph. You can check the ImageUrl field update and do the resize there. Any time you upload a new picture the URL is basically updated. Please don't use the example below in production as it was never fully tested.

// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class InventoryItemMaintExt : PXGraphExtension<InventoryItemMaint>
{
    public PXSelect<UploadFileRevision> uploadFileRevisions;
    protected virtual void InventoryItem_ImageUrl_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e, PXFieldUpdated baseMethod)
    {
        baseMethod?.Invoke(sender, e);
        if(e.Row is InventoryItem row)
        {
            if ((string)e.OldValue != row.ImageUrl) //ADD conditions so that this doesn't work any time user change the image if there are multiple attached
            {
                UpdateImageFileRevisionToResizedImage(sender, row);
            }
        }
    }

    private void UpdateImageFileRevisionToResizedImage(PXCache sender, InventoryItem row)
    {
        var fileNotes = PXNoteAttribute.GetFileNotes(sender, row);
        UploadFileRevision uploadedFile = GetFile(sender.Graph, fileNotes, row.ImageUrl);
        if (uploadedFile != null)
        {
            var data = ResizeImage(uploadedFile.Data);
            uploadedFile.Data = data;
            uploadFileRevisions.Update(uploadedFile);
        }
    }

    //WARNING: DON'T USE THIS METHOD IN PRODUCTION.
    // USE ANY OTHER RECOMMENDED METHOD TO RESIZE IMAGES
    private static byte[] ResizeImage(byte[] data)
    {
        System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(data);
        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
        System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(200, 200, null, IntPtr.Zero);
        System.IO.MemoryStream myResult = new System.IO.MemoryStream();
        newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg);  //Or whatever format you want.
        return myResult.ToArray();  //Returns a new byte array.
    }

    private static UploadFileRevision GetFile(PXGraph graph, Guid[] fileIds,string fileUrl)
    {
        return (UploadFileRevision)PXSelectBase<UploadFileRevision, 
            PXSelectJoin< UploadFileRevision, 
                InnerJoin <UploadFile,
                    On<UploadFile.fileID, Equal<UploadFileRevision.fileID>,
                    And<UploadFile.lastRevisionID, Equal<UploadFileRevision.fileRevisionID>>>>,
                Where<UploadFile.fileID, In<Required<UploadFile.fileID>>,
                And<UploadFile.name,Equal<Required<UploadFile.name>>>>>.Config>.Select(graph, new object[]
        {
            fileIds,
            fileUrl
        });
    }
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46