开发者

Using image to set textured background using imagettftext

I can create an image using GD with PHP using the imagettftext() function. The colour of the text is specified using imagecolorall开发者_JAVA百科ocate() but this takes colour in the format RGB.

I have a range of textured images - each 10 x 10 pixels. I would like to use these textured images as the colour for the text rather than a single RBG colour.

I can't see how to achieve this, is it possible?


If you can, use ImageMagick. It can do this out of the box.

Example from the examples:

   convert -size 800x120 xc:black -font Corsiva -pointsize 100 \
      -tile tile_disks.jpg   -annotate +20+80 'Psychedelic!' \
      -trim +repage  -bordercolor black  -border 10   funfont_groovy.jpg

Using image to set textured background using imagettftext


I made this function which replaces pixel by pixel based on a defined color

function imagettftexture(&$im,$textureimage, $size, $angle, $_x, $_y, $font, $text){
        $w = imagesx($im);
        $h = imagesy($im);
        $sz = getimagesize($textureimage);
        $tx = null;
        switch($sz['mime']){
            case 'image/png':
                $tx = imagecreatefrompng($textureimage);
                break;
            case 'image/jpeg':
                $tx = imagecreatefromjpeg($textureimage);
                break;
            case 'image/gif':
                $tx = imagecreatefromgif($textureimage);
                break;
        }
        //same size as $im
        $tmp = imagecreatetruecolor($w,$h);
        //fill with texture
        imagesettile($tmp,$tx);
        imagefilledrectangle($tmp, 0, 0, $w, $h, IMG_COLOR_TILED);

        //a weird color
        $pink = imagecolorclosest($im,255,0,255);
        $rect = imagettftext($im,$size,$angle,$_x,$_y,-$pink,$font,$text); // "minus" to draw without aliasing

        for($x=0;$x<$w;$x++){
            $x = $x;
            for($y=0;$y<$h;$y++){
                $tmpcolor = imagecolorat($tmp,$x,$y);
                $color = imagecolorat($im,$x,$y);
                if($color == $pink)
                    imagesetpixel($im,$x,$y,$tmpcolor);
            }
        }

        //useful to return same value as imagettftext
        return $rect;
    }

It's pretty slow, it took
100ms to process a 350x127 image
760ms to process a 1024x342 image

There are other solutions here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜