imageareaselect: crop the image with the defined height and width of image

452 Views Asked by At

i am using a jquery plugin for preview crop the image. the problem is when i upload the image it show the image with full length of 1280* 1024. i want that whatever the size of image is it shows with the fix length of 300 * 300 .

Aspx code without giving the size works fine:

 <img id="Img_UploadLogo" runat="server" src=""/>

and jquery is:

<script type="text/javascript">
    function preview(img, selection) {
        if (!selection.width || !selection.height)           
            return;

        var scaleX = 175 / (selection.width || 1); // width height of preview image div
        var scaleY = 85 / (selection.height || 1);
        var Width = $('#<%=Img_UploadLogo.ClientID %>').width(); // width height of preview image div
        var Height = $('#<%=Img_UploadLogo.ClientID %>').height();

        $('#preview img').css({

            width: Math.round(scaleX *Width ) + 'px', // width of orgnal image div
            height: Math.round(scaleY *Height ) + 'px',
            marginLeft: -Math.round(scaleX * selection.x1),
            marginTop: -Math.round(scaleY * selection.y1)            

        });



        $('#<%=x1.ClientID %>').val(selection.x1);
        $('#<%=y1.ClientID %>').val(selection.y1);
        $('#<%=x2.ClientID %>').val(selection.x2);
        $('#<%=y2.ClientID %>').val(selection.y2);
        $('#<%=w.ClientID %>').val(selection.width);
        $('#<%=h.ClientID %>').val(selection.height);

    }

    $(function () {
        $('#<%=Img_UploadLogo.ClientID %>').imgAreaSelect({aspectRatio: '1:1', x1: 120, y1: 90, x2: 280, y2: 210, show: false, handles: true,
            fadeSpeed: 200, onSelectChange: preview,
        });
    });

</script>

thge above code is woking fine for me. it shows the correct preview and correct cropped image. but when i give the fix size to the image like below , it will show the correct preview but the crop image is incorrect.

not crop the image correctly when i give the fix size to it: Aspx code with giving the size works not fine with the croping image:

 <img id="Img_UploadLogo" runat="server" src="" style="width: 300px; height: 300px;"/>

please suggest me the solution so that it will crop the image perfectly.

0

There are 0 best solutions below