twicpics / url
用于构建 TwicPics URL 的库
Requires
- php: >=5.3.0
Requires (Dev)
- php: >=7.1.0
- php-coveralls/php-coveralls: 2.1.0
- phpunit/phpunit: 7.3.0
- squizlabs/php_codesniffer: 3.3.1
This package is not auto-updated.
Last update: 2024-09-29 05:42:07 UTC
README
twicpics/url
提供了一个简单而表达力强的流畅 API,用于生成 TwicPics URL。
以下是一些它能够实现的功能示例
$builder = new TwicPics\URL(); // Create a url in one pass $onePassUrl = $builder->cover("1:1")->resize(700)->src(SRC_URL)->url(); // Pre-crop an image then apply different transformations to it $precrop = $builder->src(SRC_URL)->focus("25p", "71p")->crop(560, 280); $squareUrl = $precrop->cover("1:1")->url(); $landscapeUrl = $precrop->cover("16:9")->url(); // Prepare manipulations to be applied to different sources $square = $builder->cover("1:1")->resize(300); $landscape = $builder->cover("1:1")->resize(300); $squaredUrl = $square->src(SRC_URL)->url(); $squaredPrecrop = $square->src($precrop)->url(); $landscapedUrl = $landscape->src(SRC_URL)->url(); $landscapedPrecrop = $landscape->src($precrop)->url();
安装
使用 composer
php composer.phar require twicpics/url
用法
twicpics/url
导出一个单一的类(TwicPics\URL
),该类将被自动加载。只需创建该类的实例,然后即可使用
// Get the builder $builder = new TwicPics\URL(); // Use the builder $myFirstUrl = $builder->src(MY_IMAGE_URL)->resize( 300 )->url();
构建器的 API 是流畅的,每个方法调用都返回一个新不可变的对象。因此,您可以重用现有的对象并创建一个全新的独立 URL
$authorizedAndSquared = $builder->auth(MY_TOKEN)->cover("1:1"); $url1 = $authorizedAndSquared->src(MY_IMAGE_URL_1)->url(); $url2 = $authorizedAndSquared->src(MY_IMAGE_URL_2)->url();
最后,但同样重要的是,任何构建器对象都可以作为另一个构建器对象的源图像使用。因此,您可以创建应用于不同图像的通用操作,这些图像可能是预先转换的
$square500 = $builder->cover(500, 500); // Use authentication for an image I don't own $external = $builder->auth(MY_TOKEN)->src(URL_TO_IMAGE_I_DONT_OWN); // Precrop an image I own $precrop = $builder->src(URL_TO_IMAGE_I_OWN)->crop( [ "x" => 150, "y" => 256, "width" => 700, "height" => 889 ] ); // square the image I don't own $square500->src(external)->url(); // square the image I own $square500->src(precop)->url();
API
认证
auth(认证令牌)
添加认证令牌。
$builder->auth("aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaaaa");
包含
contain(<表达式>)
contain(<宽度>[, <高度>])
contain({ width, height })
添加一个 contain
转换。
// These four lines are strictly equivalent $builder->contain("500x400"); $builder->contain(500, 400); $builder->contain( [ "width" => 500, "height" => 400 ] ); $builder->contain( json_decode( '{ "width": 500, "height": 400 }' ) );
包含最大值
containMax(<表达式>)
containMax(<宽度>[, <高度>])
containMax({ width, height })
添加一个 contain-max
转换。
// These four lines are strictly equivalent $builder->containMax("500x400"); $builder->containMax(500, 400); $builder->containMax( [ "width" => 500, "height" => 400 ] ); $builder->containMax( json_decode( '{ "width": 500, "height": 400 }' ) );
包含最小值
containMin(<表达式>)
containMin(<宽度>[, <高度>])
containMin({ width, height })
添加一个 contain-min
转换。
// These four lines are strictly equivalent $builder->containMin("500x400"); $builder->containMin(500, 400); $builder->containMin( [ "width" => 500, "height" => 400 ] ); $builder->containMin( json_decode( '{ "width": 500, "height": 400 }' ) );
覆盖
cover(<表达式>)
cover(<宽度>[, <高度>])
cover({ width, height })
添加一个 cover
转换。
// These four lines are strictly equivalent $builder->cover("500x400"); $builder->cover(500, 400); $builder->cover( [ "width" => 500, "height" => 400 ] ); $builder->cover( json_decode( '{ "width": 500, "height": 400 }' ) );
覆盖最大值
coverMax(<表达式>)
coverMax(<宽度>[, <高度>])
coverMax({ width, height })
添加一个 cover-max
转换。
// These four lines are strictly equivalent $builder->coverMax("500x400"); $builder->coverMax(500, 400); $builder->coverMax( [ "width" => 500, "height" => 400 ] ); $builder->coverMax( json_decode( '{ "width": 500, "height": 400 }' ) );
覆盖最小值
coverMin(<表达式>)
coverMin(<宽度>[, <高度>])
coverMin({ width, height })
添加一个 cover-min
转换。
// These four lines are strictly equivalent $builder->coverMin("500x400"); $builder->coverMin(500, 400); $builder->coverMin( [ "width" => 500, "height" => 400 ] ); $builder->coverMin( json_decode( '{ "width": 500, "height": 400 }' ) );
裁剪
crop(<表达式>)
crop(<宽度>[, <高度>[, <X>[, <Y>]])
crop({ x, y, width, height })
添加一个裁剪转换。
// The following four lines create the same crop without origin $builder->crop("500x400"); $builder->crop(500, 400); $builder->crop( [ "width" => 500, "height" => 400 ] ); $builder->crop( json_decode( '{ "width": 500, "height": 400 }' ) ); // The following four lines create the same crop with origin $builder->crop("500x400@15x20"); $builder->crop(500, 400, 15, 20); $builder->crop( [ "x" => 15, "y" => 20, "width" => 500, "height" => 400 ] ); $builder->crop( json_decode( '{ "x": 15, "y": 20, "width": 500, "height": 400 }' ) );
焦点
focus(<表达式>)
focus(<X>[, <Y>])
focus({ x, y })
设置焦点点。
// These four lines set the exact same focus point $builder->focus("67x987"); $builder->focus(67, 987); $builder->focus( [ "x" => 67, "y" => 987 ] ); $builder->focus( json_decode( '{ "x": 67, "y": 987 }' ) );
格式
format(<类型>[, <质量>])
format({ type, quality })
设置图像格式。
接受的类型是 "jpeg"
,"png"
和 "webp"
。只有 jpeg
和 webp
接受质量值。
$builder->format( "jpeg", 45 ); $builder->format( [ "type" => "jpeg", "quality" => 62 ] ); $builder->format( "png" ); $builder->format( json_decode( '{ "type": "webp", "quality": 80, }' ) );
jpeg
jpeg([<质量>])
format("jpeg", $quality)
的快捷方式。
最大值
max(<表达式>)
max(<宽度>[, <高度>])
max({ width, height })
添加一个 max
转换。
// These four lines are strictly equivalent $builder->max("500x400"); $builder->max(500, 400); $builder->max( [ "width" => 500, "height" => 400 ] ); $builder->max( json_decode( '{ "width": 500, "height": 400 }' ) );
最小值
min(<表达式>)
min(<宽度>[, <高度>])
min({ width, height })
添加一个 min
转换。
// These four lines are strictly equivalent $builder->min("500x400"); $builder->min(500, 400); $builder->min( [ "width" => 500, "height" => 400 ] ); $builder->min( json_decode( '{ "width": 500, "height": 400 }' ) );
png
png()
format("png")
的快捷方式。
调整大小
resize(<表达式>)
resize(<宽度>[, <高度>])
resize({ width, height })
添加一个 resize
转换。
// These four lines are strictly equivalent $builder->resize("500x400"); $builder->resize(500, 400); $builder->resize( [ "width" => 500, "height" => 400 ] ); $builder->resize( json_decode( '{ "width": 500, "height": 400 }' ) );
调整大小最大值
resizeMax(<表达式>)
resizeMax( <width> [, <height> ] )
resizeMax( { width, height } )
添加一个 resize-max
变换。
// These four lines are strictly equivalent $builder->resizeMax("500x400"); $builder->resizeMax(500, 400); $builder->resizeMax( [ "width" => 500, "height" => 400 ] ); $builder->resizeMax( json_decode( '{ "width": 500, "height": 400 }' ) );
resizeMin
resizeMin( <expr> )
resizeMin( <width> [, <height> ] )
resizeMin( { width, height } )
添加一个 resize-min
变换。
// These four lines are strictly equivalent $builder->resizeMin("500x400"); $builder->resizeMin(500, 400); $builder->resizeMin( [ "width" => 500, "height" => 400 ] ); $builder->resizeMin( json_decode( '{ "width": 500, "height": 400 }' ) );
src
src( <url> )
src( <builder object> )
设置当前操作需要执行其上的源图像。
如果提供了URL,则将其用作变换的主图像。
$builder->resize(300)->src(MY_IMAGE); // generated a 300 pixels-wide version of MY_IMAGE
如果提供了builder对象,则其源将被用作新变换的源,同时其变换将添加到当前的变换之前。
$precrop = $builder->src(MY_IMAGE)->crop( [ "x": 150, "y": 256, "width": 700, "height": 889 ] ); // This will first crop MY_IMAGE then apply a cover=500x500 $builder->cover(500, 500)->src($precop);
step
step( <expr> )
step( <width> [, <height> ] )
step( { width, height } )
添加一个 step
变换。
// These four lines are strictly equivalent $builder->step("10x10"); $builder->step(10, 10); $builder->step( [ "width" => 10, "height" => 10 ] ); $builder->step( json_decode( '{ "width": 10, "height": 10 }' ) );
__toString
__toString()
生成URL字符串。注意,在这次调用之前必须使用 .src()
提供了一个图像URL,否则将抛出异常。
$builder->__toString(); // throws an exception $builder->src(MY_IMAGE_URL)->__toString(); // works
url
url()
是 __toString
的别名。
webp
webp( [ <quality> ] )
是 format("webp", $quality)
的快捷方式。