简体   繁体   中英

toDataURL() return blank image from iOS devices

i use following code to crop the image and save it

this.crop = function(width, height, type) {
            var imgInfo = this.imgInfo,
                c = this.eles.container,
                img = this.eles.img,
                original = new Image(),
                canvas = document.createElement('canvas');
            original.src = img.attr('src');
            canvas.width = width;
            canvas.height = height;

            var w = Math.round(c.width() * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
                h = Math.round(c.height() * (imgInfo.ah / (imgInfo.h * imgInfo.s))),
                x = Math.round(-parseInt(img.css('left')) * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
                y = Math.round(-parseInt(img.css('top')) * (imgInfo.ah / (imgInfo.h * imgInfo.s)));
            canvas.getContext('2d').drawImage(original, x, y, w, h, 0, 0, width, height);
            return {
                width: width,
                height: height,
                type: type || 'jpeg',
                string: canvas.toDataURL("image/" + type || 'jpeg'),
            };
        };

But when i convert it to image and then upload to the server. uploaded image is blank.

for this i use following code from the save.php file

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
    define('UPLOAD_DIR', 'img/catalog/');

    $base64 = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $_POST['string']));
    $stamp = imagecreatefrompng('img/water.png');
    $im = imagecreatefromstring($base64);
    ob_start();

    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

    $file = UPLOAD_DIR . uniqid() . '.jpg';
    header('Content-type: image/');
    $success = imagejpeg($im, $file, 100);
    imagedestroy($im);
    echo "$file";

    }

?>

From PC everything working fine, but when i upload an image from any mobile devices image is blank. when i zoom it to max size everything working fine.

Can you tell me where i'm wrong?

i have change my script.

this.data = function(width, height, filetype) {

            var self = this,
                imgInfo = self.imgInfo,
                c = self.eles.container,
                img = self.eles.img;

            var original = new Image();
                original.src = img.attr('src');

            // draw image to canvas
            var canvas = document.createElement('canvas');
                canvas.width = width;
                canvas.height = height;

            var w = Math.round((c.width() - (0 * 2)) * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
                h = Math.round((c.height() - (0 * 2)) * (imgInfo.ah / (imgInfo.h * imgInfo.s))),
                x = Math.round(-(parseInt(img.css('left')) - 0) * (imgInfo.aw / (imgInfo.w * imgInfo.s))),
                y = Math.round(-(parseInt(img.css('top')) - 0) * (imgInfo.ah / (imgInfo.h * imgInfo.s)));

            canvas.getContext('2d').drawImage(original, x, y, w, h, 0, 0, width, height);

            data = {
                width: width,
                height: height,
                image: canvas.toDataURL("image/"+filetype),
                filetype: filetype
            };

            return data;
        };

now it works with iOS, but i have another problem. when i upload photo from camera it automatically rotate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM