PHP 图片处理类库Grafika详细教程(3):图像属性处理

该文章是接着上篇文章,《PHP极其强大的图片处理库Grafika详细教程(2):图像特效处理模块》,由于grafika功能太多,所以分开写,其他的点击这里

《1、图像基本处理》
《2、图像特效处理模块》
《3、图像属性处理》
《4、图形绘制》

该文章主要写grafika的图像属性处理功能,共7个方法

1、图片格式化为二进制格式输出

该方法的作用是打开一张图片,然后格式化为二进制数据,直接输出到浏览器,而不是传统的src显示图片。

其有一个参数,你可以自定义输出图片的格式,比如png啥的

我们这里打开图片,输出为png

当然你还是要告诉浏览器你需要输出的类型是图片header('Content-type: image/png');

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'yanying-smaller.jpg' );
header('Content-type: image/png'); // Tell the browser we're sending a png image$image->blob('PNG');


图片描述


2、获取图片当前使用的处理库

使用方法可以获取处理当前图片,grafika使用了什么库,是gd还是Imagick

该方法不在editor里面,而是直接在$image里面,没有任何参数

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'yanying-smaller.jpg' );
$result = $image->getCore();
var_dump($result); // resource(12, gd)

3、获取图片高度

我们图片高度为213px

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'yanying-smaller.jpg' );
$result = $image->getHeight();
var_dump($result); // int 213

4、获取图片宽度

我们图片宽度为319px

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'yanying-smaller.jpg' );
$result = $image->getWidth();
var_dump($result); // int 319

5、获取图片名称

图片名称为当前文件名

use Grafika\Grafika;$editor = Grafika::createEditor();$editor->open( $image, 'yanying-smaller.jpg' );$result = $image->getImageFile();
var_dump($result); // string 'yanying-smaller.jpg' (length=19)

6、获取图片类型

这里我们发现是jpg的

use Grafika\Grafika;$editor = Grafika::createEditor();$editor->open( $image, 'yanying-smaller.jpg' );$result = $image->getType();
var_dump($result); // string 'JPEG' (length=4)

7、判断图片是否是动态图片,比如gif

我们这张图片是jpg的,所以不是动态图片,返回值为bool类型,true或者false

use Grafika\Grafika;$editor = Grafika::createEditor();$editor->open( $image, 'yanying-smaller.jpg' );$result = $image->isAnimated();
var_dump($result); // boolean false