站长网 PHP教程 php png失真的原因及解决方法

php png失真的原因及解决方法

1、创建一个PHP示例文件。 2、创建一个和背景图片一样大小的真彩色画布。 3、复制背景图片。 4、通过imagecreatefrompng合成png图片即可。 实例 ?php ob_clean(); $bg = image1.png; $image_1 = imagecreatefrompng($bg); $bgx = imagesx($image_1); $bgy =

1、创建一个PHP示例文件。
 
2、创建一个和背景图片一样大小的真彩色画布。
 
3、复制背景图片。
 
4、通过“imagecreatefrompng”合成png图片即可。
 
实例
<?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解决合并图片失真问题
$ni = imagecreatetruecolor($toW,$toH); //创建真彩色图片
$bg_x = (($toW-$ftoW)/2);
$bg_y = (($toH-$ftoH)/2);
$color=imagecolorallocate($ni,255,255,255); //创建颜色
imagefill($ni, 0, 0, $color); //设置白底
imagecopy($ni,$tm,$bg_x,$bg_y,0,0,$ftoW,$ftoH); //合并图片
imagedestroy($tm);
 

本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/jc/php/2021/1113/24352.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部