簡體   English   中英

Base64剪輯圖像

[英]Base64 clipping images

下面的代碼使用一個名為cropit的插件提供的base64字符串,並將其轉換為圖像。

list($type, $base64) = explode(';', $base64);
list(, $base64)      = explode(',', $base64);
$base64 = str_replace("data:image/jpeg;base64,", "", $base64);
$base64 = base64_decode($base64);

file_put_contents($directory, $base64);

我還將提供我的javascript,它通過使用輸入將base64發送到php函數。 我知道這個問題是由PHP造成的,因為當我將imageData發送到一個新窗口時,圖像將完美顯示,沒有任何問題。

$('.export_upload').click(function() {
    $("#upload_form_identifier").val("upload_form");

    var imageData = $('.image-editor-upload').cropit('export', {
        type: 'image/jpeg',
        quality: 0.3,
        originalSize: true
    });

    //Set value of hidden input to base64
    $("#hidden_base64_upload").val(imageData);

    //Pause form submission until input is populated
    window.setTimeout(function() {
        document.upload_form.submit();
    }, 1000);
});

我遇到的問題是,如果我輸入圖像,它會將其剪輯為隨機點。 PHP可能會耗盡內存嗎? 我對base64的使用不是很好,所以我真的不知道是什么導致這個問題。 任何幫助都會很棒。

原始圖片: 原始圖像

在PHP處理base64之后: PHP處理完base64之后

雖然這不是最適合我的解決方案,但可能會滿足您的需求。 我發現的問題是使用originalSize: true這將導出圖像的裁剪部分而不進行任何壓縮,從而產生非常大的base64。 我解決它的方法是將originalSize設置為false,然后將預覽大小調整為我將使用的大小。 下面的代碼應該有效。

$('.export_upload').click(function() {
            $("#upload_form_identifier").val("upload_form");

            $('.image-editor-upload').cropit('previewSize', {width:1024, height:1024});

            var imageData = $('.image-editor-upload').cropit('export', {
                type: 'image/jpeg',
                quality: .75,
                originalSize: false
            });



            //Set value of hidden input to base64
            $("#hidden_base64_upload").val(imageData);

            //Pause form submission until input is populated
            window.setTimeout(function() {
                window.open(imageData);
                document.upload_form.submit();
            }, 1000);
        });

關鍵是$('.image-editor-upload').cropit('previewSize', {width:1024, height:1024}); 這會在將圖像發送到php函數之前調整圖像大小。 唯一真正的問題是,如果用戶修改了javascript,他們將能夠更改圖像的輸出大小,但如果您使用php驗證上傳以確保寬度和高度與您的匹配,這不應該是一個問題放在括號內。

我今天想出了一個基本的驗證功能。 它將根據圖像的尺寸是否正確返回true或false。 您可以將此應用於圖像設置的初始表單,並檢查它是否匹配,並相應地拋出錯誤。

/**
 * Checks the dimensions of the provided image
 * @param  string $base64_image Base64 string of the image
 * @param  string $width Desired width of the image
 * @param  string $height Desired height of the image
 * @return bool True if dimensions match, false if dimensions do not match
 */
public function checkImageDimensions ($base64_image, $width, $height) {
    list($type, $base64_image) = explode(';', $base64_image);
    list(, $base64_image)      = explode(',', $base64_image);
    $base64_image = base64_decode($base64_image);

    $dimensions = getimagesizefromstring($base64_image);

    if ($dimensions[0] == $width && $dimensions[1] == $height) {
        return true;
    } else if ($dimensions[0] !== $width && $dimensions[1] !== $height) {
        return false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM