PHP圖片處理庫(kù)Grafika詳細(xì)教程(3):圖像屬性處理
該文章是接著上篇文章,《PHP極其強(qiáng)大的圖片處理庫(kù)Grafika詳細(xì)教程(2):圖像特效處理模塊》,由于grafika功能太多,所以分開(kāi)寫(xiě),其他的點(diǎn)擊這里
該文章主要寫(xiě)grafika的圖像屬性處理功能,共7個(gè)方法。
1、圖片格式化為二進(jìn)制格式輸出
該方法的作用是打開(kāi)一張圖片,然后格式化為二進(jìn)制數(shù)據(jù),直接輸出到瀏覽器,而不是傳統(tǒng)的src顯示圖片。
其有一個(gè)參數(shù),你可以自定義輸出圖片的格式,比如png啥的
我們這里打開(kāi)圖片,輸出為png
當(dāng)然你還是要告訴瀏覽器你需要輸出的類(lèi)型是圖片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、獲取圖片當(dāng)前使用的處理庫(kù)
使用方法可以獲取處理當(dāng)前圖片,grafika使用了什么庫(kù),是gd還是Imagick
該方法不在editor里面,而是直接在$image里面,沒(méi)有任何參數(shù)
- 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、獲取圖片名稱(chēng)
圖片名稱(chēng)為當(dāng)前文件名
- 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、獲取圖片類(lèi)型
這里我們發(fā)現(xiàn)是jpg的
- use Grafika\Grafika;
- $editor = Grafika::createEditor();
- $editor->open( $image, 'yanying-smaller.jpg' );
- $result = $image->getType();
- var_dump($result); // string 'JPEG' (length=4)
7、判斷圖片是否是動(dòng)態(tài)圖片,比如gif
我們這張圖片是jpg的,所以不是動(dòng)態(tài)圖片,返回值為bool類(lèi)型,true或者false
- use Grafika\Grafika;
- $editor = Grafika::createEditor();
- $editor->open( $image, 'yanying-smaller.jpg' );
- $result = $image->isAnimated();
- var_dump($result); // boolean false