PHP - 圖片處理 - 圖片寫字

getTablePicture 方法爲圖片寫字:

private function getTablePicture($file,$koubei,$src='',$size=[])
    {
        $src = $src?:$file;
        $stage = new ZStage(680,680); // 初始化圖片對象
        $bitmap_data = new ZBitmapData($src); // 定義圖片內容,格式,尺寸等
        $bitmap = new ZBitmap($bitmap_data,0,110); // 圖片對象賦值
        $bitmap->center();  // 圖片對象居中
        if($size){
            $bitmap->setSize($size[0],$size[1]);
        }
        $type = TableType::find($koubei['type_id']);
        $type_name = $type?$type['name']:'普通桌';
        $text_type = new ZText('桌位類型:'.$type_name);
        $text_type->left();
        $text_type->setY(60);
        $text_type->offsetX(60);

        $text_code = new ZText('桌號:'.$koubei['name']);
        $text_code->right();
        $text_code->setY(60);
        $text_code->offsetX(-60);

        $store = \App\Models\Store::find($koubei['store_id']);
        $store_name = $store?$store->name:'總店';
        if($koubei['multi_store_id']){
            $multi = StoreMulti::find($koubei['multi_store_id']);
            if($multi){
                $store_name = $multi->name;
            }
        }
        $text_shop = new ZText($store_name);
        $text_shop->center();
        $text_shop->setY(640);

        $stage->add_child($bitmap);
        $stage->add_child($text_type);
        $stage->add_child($text_code);
        $stage->add_child($text_shop);
        $stage->save($file);
    }

ZStage類,初始化一個圖片:

<?php
namespace App\Common\Zcglegend;

class ZStage
{
    public $children = [];
    public $width;
    public $height;
    public $context;

    public function __construct($width,$height,$background='white')
    {
        $this->width = $width;
        $this->height = $height;
        $this->context = imagecreatetruecolor($width, $height);
        imagefill($this->context,0,0,ZColor::getColor($this->context,$background));
    }

    public function add_child($child)
    {
        $this->children[] = $child;
    }

    public function save($filename)
    {
        foreach($this->children as $child){
            $child->show($this);
        }
        imagepng($this->context,$filename);
    }

    public function show()
    {
        header("Content-Type:image/png;charset=utf-8");
        foreach($this->children as $child){
            $child->show($this);
        }
        imagepng($this->context);
        imagedestroy($this->context);
    }
}

ZBitmap類,定義圖片尺寸、位置:

class ZBitmap extends ZDisplay
{
    public function __construct($bitmap_data,$x=0,$y=0,$w=0,$h=0)
    {
        $this->content = $bitmap_data;
        $this->x = $x;
        $this->y = $y;
        $this->w = $w?:$bitmap_data->w;
        $this->h = $h?:$bitmap_data->h;
    }

    public function show($stage)
    {
        if($this->align == 'center'){
            $this->x = ceil(($stage->width - $this->w) / 2);
        }
        if($this->align == 'right'){
            $this->x = $stage->width - $this->w;
        }
        imagecopyresized($stage->context,$this->content->content,$this->x,$this->y,$this->content->x + $this->offset_x,$this->content->y + $this->offset_y,$this->w,$this->h,$this->content->w,$this->content->h);
    }
}

ZText 類,爲圖片寫字:

class ZText extends ZDisplay
{
    public $size;
    public $angle;
    public $color;
    public $font;

    public function __construct($content,$color='black',$size = 24,$angle=0)
    {
        $this->font = resource_path('fonts/simhei.ttf');//dirname(__FILE__).'/simhei.ttf';
        $this->size = $size;
        $this->angle = $angle;
        $this->content = $content;
        $this->color = $color;
        $this->box = imagettfbbox($this->size, $this->angle, $this->font, $this->content);
    }

    public function setColor($color)
    {
        $this->color = $color;
    }

    public function center()
    {
        $this->x = $this->box[2];
        $this->align = 'center';
    }

    public function right()
    {
        $this->x = $this->box[2];
        $this->align = 'right';
    }

    public function show($stage)
    {
        if($this->align == 'center'){
            $this->x = ceil(($stage->width - $this->x) / 2);
        }
        if($this->align == 'right'){
            $this->x = $stage->width - $this->x;
        }
        imagettftext($stage->context,$this->size,$this->angle,$this->x + $this->offset_x,$this->y + $this->offset_y,ZColor::getColor($stage->context,$this->color),$this->font,$this->content);
    }
}

ZBitmapData 爲圖片對象定義內容,類型,尺寸等

class ZBitmapData
{
    public $path;
    public $width;
    public $height;
    public $content;
    public $type = 'png';

    //計算屬性
    public $x = 0;
    public $y = 0;
    public $w;
    public $h;

    private $allow_mime_types = ['jpeg','png','bmp','gif'];

    public function __construct($path,$x=0,$y=0,$w=0,$h=0)
    {
        $this->path = $path;
        $arr = explode('.',$path);
        $this->type = $arr[count($arr)-1];
	    if(!in_array($this->type,$this->allow_mime_types)){
		    $this->type = 'string';
	    }
        if($this->type == 'jpg'){
            $this->type = 'jpeg';
        }
        $func = 'imagecreatefrom'.$this->type;
        if ($this->type == 'string') {
            $this->content = $func(file_get_contents($this->path));
        } else {
            $this->content = $func($this->path);
        }
        $this->width = imagesx($this->content);
        $this->height = imagesy($this->content);
        $this->x = $x;
        $this->y = $y;
        $this->w = $w?:$this->width;
        $this->h = $h?:$this->height;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章