关于php:php-png失真的原因及解决办法

10次阅读

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

在本篇文章里小编给大家整顿的是一篇对于 php png 失真的起因及解决办法,有须要的敌人们能够跟着学习参考下。

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);

正文完
 0