我正在使用TCPDF生成PDF。
我需要的是能够通过它构建具有2色感的rect的功能。使用TCPDF LinearGradient()
函数
$pdf->LinearGradient(0, $pdf->getY(), 10, 10, $gradientSpots[0]['color'], $gradientSpots[1]['color'], $array(0,1,1,1));
处绘制一个渐变框,其中颜色从左到右均匀分布。
我想要的是设置色标的功能。所以我修改了LinearGradient函数
public function TTCLinearGradient($x, $y, $w, $h, $colourStops = array(), $coords=array(0,0,1,0), $clip = true) {
if($clip){
$this->Clip($x, $y, $w, $h);
}
$stops = array();
$total_stops = count($colourStops);
foreach($colourStops as $key=>$stop){
if(is_object($stop)) {
$stop = json_decode(json_encode($stop), true);
}
if(isset($stop["offset"])){
$offset = floatval($stop["offset"]);
}else{
$offset = floatval($key / $total_stops); //incase offset is omitted
}
$stops[] = array('color' => $stop["color"], 'offset' => $offset, 'exponent' => 1);
}
$this->Gradient(2, $coords, $stops, array(), false);
}
新功能可以正确绘制渐变。但是颜色停止似乎被忽略了。
如果我print_r结果我得到
[type] => 2
[coords] => Array
(
[0] => 0
[1] => 1
[2] => 1
[3] => 1
)
[antialias] =>
[colors] => Array
(
[0] => Array
(
[offset] => 0.17
[opacity] => 1
[exponent] => 1
[color] => 0.129412 0.588235 0.952941
)
[1] => Array
(
[offset] => 0.18
[opacity] => 1
[exponent] => 1
[color] => 1.000000 0.921569 0.231373
)
)
[transparency] =>
[colspace] => DeviceRGB
这是TCPDF中实际上输出梯度的函数。
/**
* Output gradient.
* @param $type (int) type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
* @param $coords (array) array of coordinates.
* @param $stops (array) array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
* @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
* @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
* @author Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @public
*/
public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
if ($this->pdfa_mode OR ($this->state != 2)) {
return;
}
$n = count($this->gradients) + 1;
$this->gradients[$n] = array();
$this->gradients[$n]['type'] = $type;
$this->gradients[$n]['coords'] = $coords;
$this->gradients[$n]['antialias'] = $antialias;
$this->gradients[$n]['colors'] = array();
$this->gradients[$n]['transparency'] = false;
// color space
$numcolspace = count($stops[0]['color']);
$bcolor = array_values($background);
switch($numcolspace) {
case 5: // SPOT
case 4: { // CMYK
$this->gradients[$n]['colspace'] = 'DeviceCMYK';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F %F %F %F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
}
break;
}
case 3: { // RGB
$this->gradients[$n]['colspace'] = 'DeviceRGB';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F %F %F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
}
break;
}
case 1: { // GRAY SCALE
$this->gradients[$n]['colspace'] = 'DeviceGray';
if (!empty($background)) {
$this->gradients[$n]['background'] = sprintf('%F', $bcolor[0]/255);
}
break;
}
}
$num_stops = count($stops);
$last_stop_id = $num_stops - 1;
foreach ($stops as $key => $stop) {
$this->gradients[$n]['colors'][$key] = array();
// offset represents a location along the gradient vector
if (isset($stop['offset'])) {
error_log("offset is set! ".print_r($stop, true));
$this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
} else {
if ($key == 0) {
$this->gradients[$n]['colors'][$key]['offset'] = 0;
} elseif ($key == $last_stop_id) {
$this->gradients[$n]['colors'][$key]['offset'] = 1;
} else {
$offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
$this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
}
}
if (isset($stop['opacity'])) {
$this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
$this->gradients[$n]['transparency'] = true;
}
} else {
$this->gradients[$n]['colors'][$key]['opacity'] = 1;
}
// exponent for the exponential interpolation function
if (isset($stop['exponent'])) {
$this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
} else {
$this->gradients[$n]['colors'][$key]['exponent'] = 1;
}
// set colors
$color = array_values($stop['color']);
switch($numcolspace) {
case 5: // SPOT
case 4: { // CMYK
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F %F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
break;
}
case 3: { // RGB
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F', $color[0]/255, $color[1]/255, $color[2]/255);
break;
}
case 1: { // GRAY SCALE
$this->gradients[$n]['colors'][$key]['color'] = sprintf('%F', $color[0]/255);
break;
}
}
}
if ($this->gradients[$n]['transparency']) {
// paint luminosity gradient
$this->_out('/TGS'.$n.' gs');
}
//paint the gradient
$this->_out('/Sh'.$n.' sh');
//restore previous Graphic State
$this->_outRestoreGraphicsState();
if ($this->inxobj) {
// we are inside an XObject template
$this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
}
}
我一直在尝试做一个Coons网格渐变的想法。但是我吓到我了。如果有人熟练些,可以让我目瞪口呆,让我知道是否可以输出带有色标的渐变。如果有帮助,则附上jpg of the generated pdf