Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d81c85d

Browse files
Add support for TextFit in text layers
1 parent 090cbe1 commit d81c85d

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

.code-generation/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
closeQualifiersChar: '',
1010
closeTransformationChar: '',
1111
unsupportedTxParams: [],
12-
unsupportedSyntaxList: ['->textFit(', ],
12+
unsupportedSyntaxList: [],
1313
mainTransformationString: {
1414
openSyntaxString: {
1515
image: '(new ImageTag(\'#publicID\'))',

src/Transformation/Layer/TextSource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class TextSource extends BaseSource implements ImageTransformationInterface
3131
use BackgroundColorTrait {
3232
BackgroundColorTrait::backgroundColor insteadof ImageTransformationTrait;
3333
}
34+
use TextFitTrait;
3435

3536
/**
3637
* TextLayer constructor.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* This file is part of the Cloudinary PHP package.
4+
*
5+
* (c) Cloudinary
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Cloudinary\Transformation;
12+
13+
use Cloudinary\ClassUtils;
14+
use Cloudinary\Transformation\Expression\Expression;
15+
use Cloudinary\Transformation\Qualifier\Dimensions\Height;
16+
use Cloudinary\Transformation\Qualifier\Dimensions\Width;
17+
18+
/**
19+
* Class TextFit
20+
*/
21+
class TextFit extends BaseAction
22+
{
23+
/**
24+
* TextFit constructor.
25+
*
26+
* @param int|string|Expression $width The width in pixels.
27+
* @param int|string|Expression $height The height in pixels.
28+
*/
29+
public function __construct($width, $height = null)
30+
{
31+
parent::__construct(CropMode::fit());
32+
33+
$this->width($width);
34+
$this->height($height);
35+
}
36+
37+
/**
38+
* TextFit named constructor.
39+
*
40+
* @param int|string|Expression $width The width in pixels.
41+
* @param int|string|Expression $height The height in pixels.
42+
*
43+
* @return $this
44+
*/
45+
public static function size($width, $height = null)
46+
{
47+
return new TextFit($width, $height);
48+
}
49+
50+
/**
51+
* Sets the width of the text.
52+
*
53+
* @param int|string|Expression $width The width in pixels.
54+
*
55+
* @return static
56+
*/
57+
public function width($width)
58+
{
59+
$this->setDimension(ClassUtils::verifyInstance($width, Width::class));
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* Sets the height of the text.
66+
*
67+
* @param int|string|Expression $height The height in pixels.
68+
*
69+
* @return static
70+
*/
71+
public function height($height)
72+
{
73+
$this->setDimension(ClassUtils::verifyInstance($height, Height::class));
74+
75+
return $this;
76+
}
77+
78+
/**
79+
* Internal setter for the dimensions.
80+
*
81+
* @param mixed $value The dimension.
82+
*
83+
* @return static
84+
*
85+
* @internal
86+
*/
87+
protected function setDimension($value = null)
88+
{
89+
$this->addQualifier($value);
90+
91+
return $this;
92+
}
93+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* This file is part of the Cloudinary PHP package.
4+
*
5+
* (c) Cloudinary
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Cloudinary\Transformation;
12+
13+
use Cloudinary\ClassUtils;
14+
use Cloudinary\Transformation\Expression\Expression;
15+
16+
/**
17+
* Trait TextFitTrait
18+
*/
19+
trait TextFitTrait
20+
{
21+
/**
22+
* Sets the text fit.
23+
*
24+
* @param int|string|Expression|TextFit $widthOrTextFit The width in pixels or the TextFit class.
25+
* @param int|string|Expression $height The height in pixels.
26+
*
27+
* @return $this
28+
*/
29+
public function textFit($widthOrTextFit, $height = null)
30+
{
31+
$this->addQualifier(ClassUtils::verifyInstance($widthOrTextFit, TextFit::class, null, $height));
32+
33+
return $this;
34+
}
35+
}

tests/Unit/Transformation/Common/LayerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Cloudinary\Transformation\MediaOverlay;
2929
use Cloudinary\Transformation\Position;
3030
use Cloudinary\Transformation\SubtitlesSource;
31+
use Cloudinary\Transformation\TextFit;
3132
use Cloudinary\Transformation\TextSource;
3233
use Cloudinary\Transformation\TextSourceQualifier;
3334
use Cloudinary\Transformation\TextStyle;
@@ -120,6 +121,39 @@ public function testTextLayer()
120121
);
121122
}
122123

124+
public function testTextLayerTextFit()
125+
{
126+
$textSource = ImageSource::text('Your Logo Here', new TextStyle(FontFamily::IMPACT, 100));
127+
128+
self::assertStrEquals(
129+
'c_fit,l_text:Impact_100:Your%20Logo%20Here,w_200/fl_layer_apply',
130+
$textSource->textFit(TextFit::size(200))
131+
132+
);
133+
134+
self::assertStrEquals(
135+
'c_fit,h_301,l_text:Impact_100:Your%20Logo%20Here,w_201/fl_layer_apply',
136+
$textSource->textFit(TextFit::size(201, 301))
137+
138+
);
139+
140+
self::assertStrEquals(
141+
'c_fit,h_302,l_text:Impact_100:Your%20Logo%20Here,w_202/fl_layer_apply',
142+
$textSource->textFit(TextFit::size(1)->width(202)->height(302))
143+
144+
);
145+
146+
self::assertStrEquals(
147+
'c_fit,l_text:Impact_100:Your%20Logo%20Here,w_203/fl_layer_apply',
148+
$textSource->textFit(203)
149+
);
150+
151+
self::assertStrEquals(
152+
'c_fit,h_304,l_text:Impact_100:Your%20Logo%20Here,w_204/fl_layer_apply',
153+
$textSource->textFit(204, 304)
154+
);
155+
}
156+
123157
public function testSubtitlesLayer()
124158
{
125159
self::assertEquals(

0 commit comments

Comments
 (0)