>  기사  >  백엔드 개발  >  PHP 문자열을 이용한 이미지 생성 예제에 대한 자세한 설명

PHP 문자열을 이용한 이미지 생성 예제에 대한 자세한 설명

小云云
小云云원래의
2018-03-03 14:24:181952검색

이 기사는 주로 PHP 문자열에서 이미지를 생성하는 자세한 예를 공유합니다. 도움이 되기를 바랍니다.

function generateImg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') {    $date = '' . date ( 'Ymd' ) . '/';    $img = $date . md5 ( $source . $text1 . $text2 . $text3 ) . '.jpg';    if (file_exists ( './' . $img )) {        return $img;
    } 
    $main = imagecreatefromjpeg ( $source ); 
    $width = imagesx ( $main );    $height = imagesy ( $main ); 
    $target = imagecreatetruecolor ( $width, $height ); 
    $white = imagecolorallocate ( $target, 255, 255, 255 );
    imagefill ( $target, 0, 0, $white );
 
    imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height ); 
    $fontSize = 18;//像素字体
    $fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB颜色
    $fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 190, $fontColor, $font, $text1 ); 
    $fontBox = imagettfbbox($fontSize, 0, $font, $text2);
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 370, $fontColor, $font, $text2 ); 
    $fontBox = imagettfbbox($fontSize, 0, $font, $text3);
    imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 560, $fontColor, $font, $text3 ); 
    //imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用 
    imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
    imageline($target, 100, 200, 20, 142, $fontColor);//画线
    imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形
 
    //bof of 合成图片
    $child1 = imagecreatefromjpeg ( 'http://gtms01.alicdn.com/tps/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
    imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );    //eof of 合成图片 
    @mkdir ( './' . $date );
    imagejpeg ( $target, './' . $img, 95 );
 
    imagedestroy ( $main );
    imagedestroy ( $target );
    imagedestroy ( $child1 );    return $img;
}generateImg ( 'https://ssl.picture.qingger.com/Fi8zMItS5c_Uvm7WueZ5z_0VsDg_', 'aaaaa', 'PHP文字水平居中', '3个字' );exit ();

관련 권장 사항:

PHP는 이미지 썸네일 생성 기능을 구현합니다.

노드는 텍스트 생성 이미지 코드 공유를 구현합니다.

PHP 내장 함수를 사용하여 이미지를 생성하는 방법에 대한 자세한 예

위 내용은 PHP 문자열을 이용한 이미지 생성 예제에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.