0

when cropping and uploading low res images it works fine but medium or large files simply wont upload. Images are uploaded to local server as BLOB (working). both smaller and larger images show the BLOB in the console window upon loading but only the lower resolution images actually display.

I have tried cropping and uploading multiple images but the only correlation I can see between issues is anything over 10kb have an issue. I have also tried this with base64 encoding with the same results

window.onload = function() {
        var options =
        {
            imageBox: '.imageBox',
            thumbBox: '.thumbBox',
            spinner: '.spinner',
            imgSrc: 'avatar.png'
        }
        var cropper;
        document.querySelector('#file').addEventListener('change', function(){
            var reader = new FileReader();
            reader.onload = function(e) {
                options.imgSrc = e.target.result;
                cropper = new cropbox(options);
            }
            reader.readAsDataURL(this.files[0]);
            this.files = [];
        })
    
        document.querySelector('#btnCrop').addEventListener('click', function(){
            var img = cropper.getDataURL()
            document.querySelector('.cropped').innerHTML += '<img src="'+img+'">';
            $("#result").attr("value", img);
        })
        document.querySelector('#btnZoomIn').addEventListener('click', function(){
            cropper.zoomIn();
        })
        document.querySelector('#btnZoomOut').addEventListener('click', function(){
            cropper.zoomOut();
        })
    };
    /**
 * Created by ezgoing on 14/9/2014.
 */
'use strict';
var cropbox = function(options){
    var el = document.querySelector(options.imageBox),
    obj =
    {
        state : {},
        ratio : 1,
        options : options,
        imageBox : el,
        thumbBox : el.querySelector(options.thumbBox),
        spinner : el.querySelector(options.spinner),
        image : new Image(),
        getDataURL: function ()
        {
            var width = this.thumbBox.clientWidth,
                height = this.thumbBox.clientHeight,
                canvas = document.createElement("canvas"),
                dim = el.style.backgroundPosition.split(' '),
                size = el.style.backgroundSize.split(' '),
                dx = parseInt(dim[0]) - el.clientWidth/2 + width/2,
                dy = parseInt(dim[1]) - el.clientHeight/2 + height/2,
                dw = parseInt(size[0]),
                dh = parseInt(size[1]),
                sh = parseInt(this.image.height),
                sw = parseInt(this.image.width);

            canvas.width = width;
            canvas.height = height;
            var context = canvas.getContext("2d");
            context.drawImage(this.image, 0, 0, sw, sh, dx, dy, dw, dh);
            var imageData = canvas.toDataURL('image/png');
            return imageData;
        },
        getBlob: function()
        {
            var imageData = this.getDataURL();
            var b64 = imageData.replace('data:image/png;base64,','');
            var binary = atob(b64);
            var array = [];
            for (var i = 0; i < binary.length; i++) {
                array.push(binary.charCodeAt(i));
            }
            return  new Blob([new Uint8Array(array)], {type: 'image/png'});
        },
        zoomIn: function ()
        {
            this.zoom(this.percent + Math.abs(this.minPercent - 1) / (this.options.zoom - 1 || 1));
            setBackground();
        },
        zoomOut: function ()
        {
            this.zoom(this.percent - Math.abs(this.minPercent - 1) / (this.options.zoom - 1 || 1));
            setBackground();
        }
    },
    attachEvent = function(node, event, cb)
    {
        if (node.attachEvent)
            node.attachEvent('on'+event, cb);
        else if (node.addEventListener)
            node.addEventListener(event, cb);
    },
    detachEvent = function(node, event, cb)
    {
        if(node.detachEvent) {
            node.detachEvent('on'+event, cb);
        }
        else if(node.removeEventListener) {
            node.removeEventListener(event, render);
        }
    },
    stopEvent = function (e) {
        if(window.event) e.cancelBubble = true;
        else e.stopImmediatePropagation();
    },
    setBackground = function()
    {
        var w =  parseInt(obj.image.width)*obj.ratio;
        var h =  parseInt(obj.image.height)*obj.ratio;

        var pw = (el.clientWidth - w) / 2;
        var ph = (el.clientHeight - h) / 2;

        el.setAttribute('style',
                'background-image: url(' + obj.image.src + '); ' +
                'background-size: ' + w +'px ' + h + 'px; ' +
                'background-position: ' + pw + 'px ' + ph + 'px; ' +
                'background-repeat: no-repeat');
    },
    imgMouseDown = function(e)
    {
        stopEvent(e);

        obj.state.dragable = true;
        obj.state.mouseX = e.clientX;
        obj.state.mouseY = e.clientY;
    },
    imgMouseMove = function(e)
    {
        stopEvent(e);

        if (obj.state.dragable)
        {
            var x = e.clientX - obj.state.mouseX;
            var y = e.clientY - obj.state.mouseY;

            var bg = el.style.backgroundPosition.split(' ');

            var bgX = x + parseInt(bg[0]);
            var bgY = y + parseInt(bg[1]);

            el.style.backgroundPosition = bgX +'px ' + bgY + 'px';

            obj.state.mouseX = e.clientX;
            obj.state.mouseY = e.clientY;
        }
    },
    imgMouseUp = function(e)
    {
        stopEvent(e);
        obj.state.dragable = false;
    },
    zoomImage = function(e)
    {
        var evt=window.event || e;
        var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta;
        delta > -120 ? obj.ratio*=1.1 : obj.ratio*=0.9;
        setBackground();
    }

    obj.spinner.style.display = 'block';
    obj.image.onload = function() {
        obj.spinner.style.display = 'none';
        setBackground();

        attachEvent(el, 'mousedown', imgMouseDown);
        attachEvent(el, 'mousemove', imgMouseMove);
        attachEvent(document.body, 'mouseup', imgMouseUp);
        var mousewheel = (/Firefox/i.test(navigator.userAgent))? 'DOMMouseScroll' : 'mousewheel';
        attachEvent(el, mousewheel, zoomImage);
    };
    obj.image.src = options.imgSrc;
    attachEvent(el, 'DOMNodeRemoved', function(){detachEvent(document.body, 'DOMNodeRemoved', imgMouseUp)});

    return obj;
};
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

This issue sometimes occurs when the original files are too large (either in physical dimensions, or in general file size), resulting in hitting an upload size limit (upload_max_filesize or post_max_size in php.ini) on the webserver or causing memory exhaustion on the server during thumbnail creation. Below are some steps to check and attempt to resolve this (on a LAMP stack, but similar logs and settings exist in Nginx and IIS):

  1. Check your apache and php logs on the server itself to identify if you reached a memory limit or a post upload size limit.

  2. If you in fact encounter the memory exhaustion error, try increasing the server's memory limit. See PHP Memory exhausted

  3. If after bumping the memory limit you continue to encounter this error, spot check some of the medium/large images that are breaking on crop/upload- determine the actual pixel dimensions of each image as well as the pixel density and/or dpi. If either is significantly large, you may want to download the original images, batch process/resave at a more reasonable size, and upload back to the server.

  4. If #3 resolves the issue, you may want to apply max width/height to the image upload field in your application to prevent content managers from uploading enormous images in the future too.

  5. In conjunction with the max width & height options, you may also want to look into setting the plugin to save thumbnails as jpg format since it's more compressed than png.

Anson W Han
  • 409
  • 2
  • 7
  • I tried all suggestions but still the same result. I doubt its the memory limit as the most I can upload is about 10-15kb images. Some of the images around the 10-20kb range will upload and display if I zoom out fitting the whole image inside inside the inner cropbox. – Beginner Developer Jan 06 '18 at 21:10
  • are there any errors in your webserver's logs to confirm it's not a memory limit from the image thumbnail generation? – Anson W Han Jan 06 '18 at 21:16
  • It's on XAMPP at the moment. Is there a way to check that? – Beginner Developer Jan 06 '18 at 21:18
  • check out https://stackoverflow.com/questions/3719549/where-does-phps-error-log-reside-in-xampp for the filepath – Anson W Han Jan 06 '18 at 21:32