php合成png图片失真及文字居中解决办法

1,060次阅读
没有评论

共计 962 个字符,预计需要花费 3 分钟才能阅读完成。

<?php
    ob_clean();
    $bg = "image1.png";
    $image_1 = imagecreatefrompng($bg);
    $bgx = imagesx($image_1);
    $bgy = imagesy($image_1);
    // 创建一个和背景图片一样大小的真彩色画布(ps:只有这样才能保证后面 copy 图片的时候不会失真)$bgimage = imageCreatetruecolor($bgx,$bgy);
    imagesavealpha($bgimage, true);// 保持透明
    imagealphablending($bgimage, true);// 混色模式
    $alpha = imagecolorallocatealpha($bgimage, 0, 0, 0, 127);// 透明
    imagefill($bgimage, 0, 0, $alpha);
    //copy 背景图片
    imagecopyresampled($bgimage,$image_1,0,0,0,0,$bgx,$bgy,$bgx,$bgy);
    $fontColor = imagecolorallocate($bgimage,0x33,0x33,0x33);
    $image_2 = imagecreatefrompng("image2.png");
    // 合成图片 2
    imagecopyresampled($bgimage, $image_2, 100, 100, 0, 0, 40, 40, imagesx($image_2) , imagesy($image_2));

    // 文字
    $textLen = mb_strlen($text1);
    $fontSize  = 20;
    $fontWidth = imagefontwidth($fontSize)*3;// 不知为什么,实测如此
    $textWidth = $fontWidth * mb_strlen($text1);
    $textx = ceil (($bgx - $textWidth) / 2 );
    imageTTFText($bgimage, $fontSize, 0, $textx, 450, $fontColor, $font , $text1);
    $result = imagepng($bgimage,"newimage.png");
    imagedestroy($bgimage);
    imagedestroy($qrcode);

 

打赏小哥

php 合成 png 图片失真及文字居中解决办法 微信打赏 php 合成 png 图片失真及文字居中解决办法 支付宝打赏

正文完
 0
评论(没有评论)