0

I have a button calling in a lightbox which has script into upload an image currently. However I have edited a script to crop the image, but the Jquery plugin it comes with kills the button to load the light box. I have tried calling it into the lightbox as a seperate php file. The crop only work with the jquery sitting above it too and not in the header, see below:

<script src="js/jquery.min.js"></script>
<script src="js/jquery.Jcrop.min.js"></script>
<script src="js/script.js"></script>
<div id="profile_pic"> <img src="images/pro.jpg" id="thumb"/> </div>
<br/>
<form enctype="multipart/form-data" method="post" action="upload.php" onSubmit="return validateForm();">
    <input type="hidden" id="w" name="w" />
    <input type="hidden" id="h" name="h" />
    <input type="hidden" id="x1" name="x1" />
    <input type="hidden" id="y1" name="y1" />
    <input type="hidden" id="x2" name="x2" />
    <input type="hidden" id="y2" name="y2" /> 
    <h1>Change Profile Picture</h1>
    <div class="file_field"> <strong>Select An Image:
        </strong> <input type="file" style="width:220px;" id="image_file" name="image_file"> <input type="submit" value="Upload"/> 
    </div>
    <br/>
    <div class="error" style="display: none;">
    </div>
    <br/> 
    <div id="image_div" style="display:none;"> <img src="" id="load_img"/> <br/>
    </div>
</form>

All of the above needs to sit in here:

<div id="inline1" style="width:auto;display: none;">
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.Jcrop.min.js"></script>
    <script src="js/script.js"></script>
    <div id="profile_pic">
        <img src="images/pro.jpg" id="thumb"/>
    </div>
    <br/>
    <form enctype="multipart/form-data" method="post" action="upload.php" onSubmit="return validateForm();" 
    <input type="hidden" id="w" name="w" />
    <input type="hidden" id="h" name="h" />
    <input type="hidden" id="x1" name="x1" />
    <input type="hidden" id="y1" name="y1" />
    <input type="hidden" id="x2" name="x2" />
    <input type="hidden" id="y2" name="y2" />
    <h1>Change Profile Picture</h1>
    <div class="file_field">
        <strong>Select An Image: </strong>
        <input type="file" style="width:220px;" id="image_file" name="image_file">
        <input type="submit" value="Upload"/>
    </div>
    <br/>
    <div class="error" style="display: none;">
    </div>
    <br/>
    <div id="image_div" style="display:none;">
        <img src="" id="load_img"/>
        <br/>
    </div>
    </form>
</div>

This is JS I think you need to see:

//Make global variables for selected image for further usage
var selectImgWidth,selectImgHeight,jcrop_api, boundx, boundy,isError=false;
$(document).ready(function(){
    $("#image_file").change(function(){
        var previewId = document.getElementById('load_img');
        var thumbId = document.getElementById('thumb');
        previewId.src = '';
        $('#image_div').hide();
        var flag = 0;

        // Get selected file parameters
        var selectedImg = $('#image_file')[0].files[0];

        //Check the select file is JPG,PNG or GIF image
        var regex = /^(image\/jpeg|image\/png)$/i;
        if (! regex.test(selectedImg.type)) {
            $('.error').html('Please select a valid image file (jpg and png are allowed)').fadeIn(500);
            flag++;
            isError = true;
        }

        // Check the size of selected image if it is greater than 250 kb or not
        else if (selectedImg.size > 250 * 1024) {
            $('.error').html('The file you selected is too big. Max file size limit is 250 KB').fadeIn(500);
            flag++;
            isError = true;
        }

        if(flag==0){
        isError=false;
        $('.error').hide(); //if file is correct then hide the error message


        // Preview the selected image with object of HTML5 FileReader class
        // Make the HTML5 FileReader Object
        var oReader = new FileReader();
            oReader.onload = function(e) {

            // e.target.result is the DataURL (temporary source of the image)
                thumbId.src = previewId.src=e.target.result;

                // FileReader onload event handler
                previewId.onload = function () {

                // display the image with fading effect
                $('#image_div').fadeIn(500);
                selectImgWidth = previewId.naturalWidth; //set the global image width
                selectImgHeight = previewId.naturalHeight;//set the global image height

                // Create variables (in this scope) to hold the Jcrop API and image size

                // destroy Jcrop if it is already existed
                if (typeof jcrop_api != 'undefined') 
                    jcrop_api.destroy();

                // initialize Jcrop Plugin on the selected image
                $('#load_img').Jcrop({
                    minSize: [32, 32], // min crop size
                   // aspectRatio : 1, // keep aspect ratio 1:1
                    bgFade: true, // use fade effect
                    bgOpacity: .3, // fade opacity
                    onChange: showThumbnail,
                    onSelect: showThumbnail
                }, function(){

                    // use the Jcrop API to get the real image size
                    var bounds = this.getBounds();
                    boundx = bounds[0];
                    boundy = bounds[1];

                    // Store the Jcrop API in the jcrop_api variable
                    jcrop_api = this;
                });
            };
        };

        // read selected file as DataURL
        oReader.readAsDataURL(selectedImg);
    }
    })
})

function showThumbnail(e)
{
    var rx = 155 / e.w; //155 is the width of outer div of your profile pic
    var ry = 190 / e.h; //190 is the height of outer div of your profile pic
    $('#w').val(e.w);
    $('#h').val(e.h);
    $('#w1').val(e.w);
    $('#h1').val(e.h);
    $('#x1').val(e.x);
    $('#y1').val(e.y);
    $('#x2').val(e.x2);
    $('#y2').val(e.y2);
    $('#thumb').css({
        width: Math.round(rx * selectImgWidth) + 'px',
        height: Math.round(ry * selectImgHeight) + 'px',
        marginLeft: '-' + Math.round(rx * e.x) + 'px',
        marginTop: '-' + Math.round(ry * e.y) + 'px'
    });
}

function validateForm(){
    if ($('#image_file').val()=='') {
        $('.error').html('Please select an image').fadeIn(500);
        return false;
    }else if(isError){
        return false;
    }else {
        return true;
    }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user2584481
  • 53
  • 1
  • 8
  • I for one, do not see any jQuery scripting in your posted code. All is see is plain **JS** `onSubmit="return validateForm();` – Funk Forty Niner Jul 25 '13 at 00:48
  • Do you have any thought on why this happenning? I have tried numerous variations and cannot understand why js/jquery.min.js & js/script.js kill the button, if I take those away the image tyou upload does not show. – user2584481 Jul 25 '13 at 00:52
  • I'd need to see what's inside your Javascript, the same JS that your `validateForm()` function is in. – Funk Forty Niner Jul 25 '13 at 00:59
  • Without seeing your entire code, your Javascripts need to be inside your ``. If it is inside that, then show us your FULL code. – Funk Forty Niner Jul 25 '13 at 01:01
  • @Fred I have added the JS now for you to see. – user2584481 Jul 25 '13 at 01:13
  • Thanks. Ok, I asked you in an earlier comment, if your Javascript was inside your ``, you didn't respond to that question. – Funk Forty Niner Jul 25 '13 at 01:16
  • For one thing, I would remove the parentheses in `Please select a valid image file (jpg and png are allowed)`. – Funk Forty Niner Jul 25 '13 at 01:19
  • But I stated in my question they are not in the as it would only allow me to use the script if placed directly above the script. – user2584481 Jul 25 '13 at 01:19
  • They MUST reside in your head. `` and the other two. That's the convention. If it doesn't work, it's because you have issues elsewhere. About the **"parentheses"** in `Please select a valid image file (jpg and png are allowed)`, that's a **"no no"**. `()` are reserved for **functions**. – Funk Forty Niner Jul 25 '13 at 01:21
  • I am not too concerned about the parentheses at this current time Fred, it will all be altered accordingly. – user2584481 Jul 25 '13 at 01:21
  • That's a type on there for some bizarre reason :S – user2584481 Jul 25 '13 at 01:32
  • I fixed it for you, you didn't properly indent it, that's why I asked and deleted my question after. – Funk Forty Niner Jul 25 '13 at 01:34
  • Thank you, I am still uncertain as to why this problem is occurring? Very odd, it is obviously something one one of those js files. – user2584481 Jul 25 '13 at 01:36
  • You're welcome. I can't pin down the problem. Have you tried loading 2 of the 3 JS in your ``, while leaving your `` where it presently resides in to test? And what's in your `script.js`, is it your Lightbox script? – Funk Forty Niner Jul 25 '13 at 01:39
  • Another thing you can try is to place your JS just above your `

    ` tag. Could be that something is not fully loading; one never knows. As per http://stackoverflow.com/a/5046472/1415724 **and** http://stackoverflow.com/a/7066620/1415724

    – Funk Forty Niner Jul 25 '13 at 01:43
  • I threw the code underneath everything which is ok to do now. My lightbox script is in it's own file entirely.... – user2584481 Jul 25 '13 at 01:53
  • So, it's working now is it? – Funk Forty Niner Jul 25 '13 at 01:57
  • No, still the same issue, your welcome to check out the files if I give you ftp access – user2584481 Jul 25 '13 at 02:02
  • No it's ok. If I couldn't have figured it out by now, can't spend anymore time, sorry. – Funk Forty Niner Jul 25 '13 at 02:03

0 Answers0