【问题标题】:PHP - Image editing with jQuery ajax not workingPHP - 使用 jQuery ajax 进行图像编辑不起作用
【发布时间】:2016-01-19 09:14:27
【问题描述】:

我正在尝试使用 jQuery ajax 在我的图像上添加一些文本,但是在 ajax 响应中得到一些奇怪的文本并且没有得到正确的图像。

jQuery Ajax 代码:

$(document.body).on('click', '.certificate_tab .tab-content li a' ,function(){
    var data_did = $(this).attr("data-diplomaidx");
    if(data_did != ""){
        $.ajax({
            url: "diploma/certificate",
            type: 'POST',
            data: "id=" + data_did,
            success: function (html){
                if(html != ""){
                    $("#CertificateModel .modal-content").append(html);
                    $('#CertificateModel').modal('show');
                }else{

                }
            }
        });
        return false;
    }
});

并在 ajax 上调用此函数:

public function certificate()
{
    if (Auth::check()) {
        $ScratchProject = new ScratchProject();

        $GetMyDiploma = $ScratchProject->GetMyDiploma();

        $ImagePath = url().'/public/upload/certificate/'.$GetMyDiploma[0]->CertificateTemplate;

        $text = "Hello Admin";
        $source_file = $ImagePath;
        //saveImageWithText("",  $image_filepath);

        //Set the Content Type
        header('Content-type: image/jpeg');

        // Copy and resample the imag
        list($width, $height) = getimagesize($source_file);
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($source_file);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);

        // Prepare font size and colors
        $text_color = imagecolorallocate($image_p, 0, 0, 0);
        $bg_color = imagecolorallocate($image_p, 255, 255, 255);
        $font = 'resources/assets/front/fonts/FontAwesome.otf';
        $font_size = 12;

        // Set the offset x and y for the text position
        $offset_x = 0;
        $offset_y = 20;

        // Get the size of the text area
        $dims = imagettfbbox($font_size, 0, $font, $text);
        $text_width = $dims[4] - $dims[6] + $offset_x;
        $text_height = $dims[3] - $dims[5] + $offset_y;

        // Add text background
        imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);

        // Add text
        //imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
        imagettftext($image_p, 20, 0, 100, 220, $text_color, $font, $text);

        // Save the picture
        imagejpeg($image_p);

        // Clear
        imagedestroy($image);
        imagedestroy($image_p);
    }
}

知道我的代码有什么问题吗?

谢谢。

【问题讨论】:

  • 这只是模糊的。但是它可能是方法名称吗? Cer1ficate ?
  • 我已经更新了我的问题。对不起,这是我的错误。

标签: php jquery ajax php-gd


【解决方案1】:

首先,您不需要发送图像响应中发送图像URI,然后使用js 在前端加载/附加它。

接下来,如果您仍想继续,您需要发送在json_encoded() 对象中编码的图像base64。然后将img 更新为src 设置,如下所示。

<img src="data:image/png;base64,<encoded image data goes here>" alt="Red dot">

【讨论】:

  • 但是我正在从certificate 方法获取图像路径。
  • 如果您不知道绝对路径与 URL 根 $_SERVER['HTTP_HOST'] 的关系,您将不得不尝试一下,但是一旦您弄清楚了,它将适用于所有情况。顺便提一句。它很容易@大多数它应该需要你很少的试错回声。
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2019-06-06
相关资源
最近更新 更多