php png失真的原因及解決辦法
1、創(chuàng)建一個PHP示例文件。
2、創(chuàng)建一個和背景圖片一樣大小的真彩色畫布。
3、復(fù)制背景圖片。
4、通過“imagecreatefrompng”合成png圖片即可。
實例
<?php ob_clean(); $bg = "image1.png"; $image_1 = imagecreatefrompng($bg); $bgx = imagesx($image_1); $bgy = imagesy($image_1); //創(chuàng)建一個和背景圖片一樣大小的真彩色畫布(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);
更多相關(guān)解決方法
PHP解決合并圖片失真問題
$ni = imagecreatetruecolor($toW,$toH); //創(chuàng)建真彩色圖片 $bg_x = (($toW-$ftoW)/2); $bg_y = (($toH-$ftoH)/2); $color=imagecolorallocate($ni,255,255,255); //創(chuàng)建顏色 imagefill($ni, 0, 0, $color); //設(shè)置白底 imagecopy($ni,$tm,$bg_x,$bg_y,0,0,$ftoW,$ftoH); //合并圖片 imagedestroy($tm);
到此這篇關(guān)于php png失真的原因及解決辦法的文章就介紹到這了,更多相關(guān)php png失真的解決方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Laravel5.3+框架定義API路徑取消CSRF保護(hù)方法詳解
這篇文章主要介紹了Laravel框架定義API路徑取消CSRF保護(hù),需要的朋友可以參考下2020-04-04PHP操作MySQL的mysql_fetch_* 函數(shù)的常見用法教程
這篇文章主要介紹了PHP中操作MySQL的mysql_fetch函數(shù)的常見用法教程,文中提到了其下fetch_array和mysql_fetch_row以及mysql_fetch_object函數(shù)的使用,需要的朋友可以參考下2015-12-12PHP生成各種隨機(jī)驗證碼的方法總結(jié)【附demo源碼】
這篇文章主要介紹了PHP生成各種隨機(jī)驗證碼的方法,結(jié)合具體實例形式總結(jié)分析了php常用的生成驗證碼操作相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-06-06php set_magic_quotes_runtime() 函數(shù)過時解決方法
PHP5.3中 bool set_magic_quotes_runtime ( bool $new_setting )函數(shù)過時.2010-07-07