a-blog cms PHP API Reference

ImageEngine
in package

AbstractYes

Table of Contents

Properties

$imageQuality  : int
画質
$mimeTypeMap  : array<string|int, mixed>
MIME Type テーブル
$optimizer  : ImagerOptimizer|null

Methods

__construct()  : mixed
Image Helper constructor.
computeEditMetrics()  : array<string|int, mixed>
画像編集をするための数値を計算
copyImage()  : void
画像を複製
copyImageAsWebp()  : void
WebP画像として画像を複製
detectImageExtenstion()  : string
MIMEタイプから画像拡張子を検出する
editImage()  : void
画像を編集(リサイズ・回転)して書き出す
getImageQuality()  : int
画質を取得
getImageSize()  : array{0: int, 1: int, 2: int, 3: string, bits: int, channels: int, mime: string}|false
画像パスから画像サイズを取得
getMimeType()  : string|null
画像パスからMIMETypeを取得
getOrientedSize()  : array{0: int, 1: int}
EXIF Orientation を適用した状態の画像サイズを取得する
getSize()  : array{0: int, 1: int}
画像サイズを取得
isSupportedImageFormat()  : bool
指定のMIMEタイプの画像形式がこのエンジンで実際に処理可能か判定する
isWebpSupported()  : bool
WebP画像がサポートされているか判定
optimize()  : void
ロスレス圧縮を実行
resizeImage()  : void
画像をリサイズ
setImageQuality()  : void
画質をセット
setOptimizer()  : void
画像オプティマイザーを設定
shouldReadFromLocalStorage()  : bool
指定のパスを LocalStorage(ローカルファイルシステム)から直接読むべきか判定する
supportsExifOrientation()  : bool
この環境で EXIF Orientation による向き補正がサポートされているか判定
copyAnimatedGif()  : void
アニメーション GIF を再エンコードせずに複製する
copyAnimatedWebp()  : void
アニメーション WebP を再エンコードせずに複製する
copyWithoutReencode()  : void
再エンコードせずに複製する(出力形式の設定 convert_2webp による変換だけは行う)
isAnimatedGif()  : bool
GIF の画像ブロックを数え、複数フレームか判定する
isAnimatedWebp()  : bool
WebP の VP8X ヘッダーでアニメーションを判定する
isIdentityTransform()  : bool
出力が元画像と同一になる変換か(拡大縮小・クロップ・回転・余白の合成のいずれも無いか)を判定する
mimeTypeToExtension()  : string|T
画像パスから画像拡張子を取得
resolveJpegQuality()  : int
JPEG を再エンコードするときの品質を決める。設定の品質を、元の JPEG の品質までに抑える
copyAnimatedImage()  : void
estimateSourceJpegQuality()  : int|null
元の JPEG の品質を、量子化テーブルから推定する(量子化テーブルは先頭付近にあるため、読むのはヘッダー部分だけ)
readGifBytes()  : string|null
skipGifSubBlocks()  : bool

Properties

$imageQuality

画質

private int $imageQuality = 75

$mimeTypeMap

MIME Type テーブル

private array<string|int, mixed> $mimeTypeMap = ['image/gif' => 'gif', 'image/png' => 'png', 'image/vnd.wap.wbmp' => 'bmp', 'image/xbm' => 'xbm', 'image/jpeg' => 'jpg', 'image/webp' => 'webp']

Methods

__construct()

Image Helper constructor.

public __construct() : mixed

computeEditMetrics()

画像編集をするための数値を計算

public computeEditMetrics(int $srcWidth, int $srcHeight[, int|null $width = null ][, int|null $height = null ][, int|null $size = null ]) : array<string|int, mixed>
Parameters
$srcWidth : int
$srcHeight : int
$width : int|null = null
$height : int|null = null
$size : int|null = null
Return values
array<string|int, mixed> —

{ width: int, height: int, x: int, y: int, columns: int, rows: int }

copyImage()

画像を複製

public abstract copyImage(string|TemporaryFile $srcPath, string $distPath[, string|null $format = null ]) : void
Parameters
$srcPath : string|TemporaryFile
$distPath : string
$format : string|null = null

copyImageAsWebp()

WebP画像として画像を複製

public abstract copyImageAsWebp(string $srcPath, string $distPath) : void
Parameters
$srcPath : string
$distPath : string

detectImageExtenstion()

MIMEタイプから画像拡張子を検出する

public detectImageExtenstion(string $mimeType) : string
Parameters
$mimeType : string
Return values
string

editImage()

画像を編集(リサイズ・回転)して書き出す

public abstract editImage(string|TemporaryFile $srcPath, string $destPath[, int|null $width = null ][, int|null $height = null ][, int|null $size = null ][, int|null $angle = null ]) : void
Parameters
$srcPath : string|TemporaryFile
$destPath : string
$width : int|null = null
$height : int|null = null
$size : int|null = null
$angle : int|null = null

getImageQuality()

画質を取得

public getImageQuality() : int
Return values
int

getImageSize()

画像パスから画像サイズを取得

public getImageSize(string|TemporaryFile $path) : array{0: int, 1: int, 2: int, 3: string, bits: int, channels: int, mime: string}|false
Parameters
$path : string|TemporaryFile
Return values
array{0: int, 1: int, 2: int, 3: string, bits: int, channels: int, mime: string}|false

getOrientedSize()

EXIF Orientation を適用した状態の画像サイズを取得する

public abstract getOrientedSize(string $path) : array{0: int, 1: int}

Orientation 5〜8(90度/270度回転)の画像は表示上の幅・高さが格納ピクセルと 入れ替わる。resizeImage() を直接呼ぶ側(ImageResize 等)はこのメソッドで 取得した寸法を基準に src/dest/canvas を計算すること。resizeImage() 自身は 呼び出し元の寸法計算と整合させるため、内部で EXIF Orientation を適用する。

Parameters
$path : string
Return values
array{0: int, 1: int}

getSize()

画像サイズを取得

public abstract getSize(string $path) : array{0: int, 1: int}
Parameters
$path : string
Return values
array{0: int, 1: int}

isSupportedImageFormat()

指定のMIMEタイプの画像形式がこのエンジンで実際に処理可能か判定する

public isSupportedImageFormat(string $mimeType) : bool

gif/png/jpeg/bmp/xbm など mimeTypeMap に登録済みの形式は true を返す。 WebP のように環境(GD/Imagick・設定)によって対応有無が変わる形式のみ 個別に判定する。mimeTypeMap に無い形式(AVIF・HEIC等、現時点で未対応の 画像形式)は false を返す。将来対応する際は、対象形式を mimeTypeMap に 追加するか、この match 式に判定を追加すればよい(呼び出し側の変更は不要)。

「そもそも画像として扱うファイルかどうか」(SVGや非画像ファイルの除外)は このメソッドの責務ではなく、呼び出し側(Media::isImageFile() 等)が判定する。

Parameters
$mimeType : string
Return values
bool

isWebpSupported()

WebP画像がサポートされているか判定

public abstract isWebpSupported() : bool
Return values
bool

optimize()

ロスレス圧縮を実行

public optimize(string $path) : void
Parameters
$path : string

resizeImage()

画像をリサイズ

public abstract resizeImage(string $srcPath, string $destPath, int $srcWidth, int $srcHeight, int $srcX, int $srcY, int $destWidth, int $destHeight, int $destX, int $destY, int $canvasWidth, int $canvasHeight, array{0: int, 1: int, 2: int} $color[, int|null $angle = null ]) : void
Parameters
$srcPath : string
$destPath : string
$srcWidth : int
$srcHeight : int
$srcX : int
$srcY : int
$destWidth : int
$destHeight : int
$destX : int
$destY : int
$canvasWidth : int
$canvasHeight : int
$color : array{0: int, 1: int, 2: int}
$angle : int|null = null

setImageQuality()

画質をセット

public setImageQuality(int $quality) : void
Parameters
$quality : int

shouldReadFromLocalStorage()

指定のパスを LocalStorage(ローカルファイルシステム)から直接読むべきか判定する

public shouldReadFromLocalStorage(string|TemporaryFile $path) : bool

TemporaryFile(呼び出し側が「これはローカル一時ファイルだ」と明示した値オブジェクト)を 渡された場合のみ true を返す。string で渡されたパスは、PublicStorage (ローカル/クラウドの永続化ストレージ抽象化レイヤー)が管理しうる領域として扱う。

Why not: 以前は is_uploaded_file() / php:// プレフィックス / CMS 設置ディレクトリ外の 絶対パスという 3 種のヒューリスティックでパスの特徴から出自を推測していたが、新しい 登録経路(CSV インポート等)が増えるたびに推測ロジックを継ぎ足す必要があり、実際に CMS-7715 で「CMS 設置ディレクトリ外の絶対パス」判定が漏れて tiny サムネイル生成だけが 失敗する不具合を生んだ。呼び出し側(Media\Helper::uploadImage()/registerLocalFile() 等)が TemporaryFile で明示することで、推測を排除する。

string でも、3.2.35 以前から判定していたアップロード一時ファイルと php:// ストリームだけは ローカルとして扱う。拡張アプリが $_FILES[...]['tmp_name'] をそのまま Image::copyImage() 等へ 渡す使い方は公開 API として動いていたため(Why not 設置ディレクトリ外の絶対パス判定まで戻さないか: それが上記の不具合の原因であり、3.2.35 以前にも無かった)。

Parameters
$path : string|TemporaryFile
Return values
bool

supportsExifOrientation()

この環境で EXIF Orientation による向き補正がサポートされているか判定

public abstract supportsExifOrientation() : bool

Imagick は autoOrient がネイティブに EXIF を扱うため常に true。GD は exif_read_data(ext-exif)に依存するため、拡張の有無で結果が変わる。

Return values
bool

copyAnimatedGif()

アニメーション GIF を再エンコードせずに複製する

protected copyAnimatedGif(string|TemporaryFile $srcPath, string $destPath) : void
Parameters
$srcPath : string|TemporaryFile
$destPath : string

copyAnimatedWebp()

アニメーション WebP を再エンコードせずに複製する

protected copyAnimatedWebp(string|TemporaryFile $srcPath, string $destPath) : void
Parameters
$srcPath : string|TemporaryFile
$destPath : string

copyWithoutReencode()

再エンコードせずに複製する(出力形式の設定 convert_2webp による変換だけは行う)

protected copyWithoutReencode(string|TemporaryFile $srcPath, string $destPath) : void
Parameters
$srcPath : string|TemporaryFile
$destPath : string

isAnimatedGif()

GIF の画像ブロックを数え、複数フレームか判定する

protected isAnimatedGif(string|TemporaryFile $path) : bool
Parameters
$path : string|TemporaryFile
Return values
bool

isAnimatedWebp()

WebP の VP8X ヘッダーでアニメーションを判定する

protected isAnimatedWebp(string|TemporaryFile $path) : bool
Parameters
$path : string|TemporaryFile
Return values
bool

isIdentityTransform()

出力が元画像と同一になる変換か(拡大縮小・クロップ・回転・余白の合成のいずれも無いか)を判定する

protected isIdentityTransform(int $sourceWidth, int $sourceHeight, int $srcWidth, int $srcHeight, int $srcX, int $srcY, int $destWidth, int $destHeight, int $destX, int $destY, int $canvasWidth, int $canvasHeight, int|null $angle) : bool

同一なら再エンコードせずに無劣化コピーできる。再エンコードすると、Squoosh などで最適化済みの 画像は品質の設定次第で元よりファイルサイズが大きくなるため。

Parameters
$sourceWidth : int

向き補正後の元画像の幅

$sourceHeight : int

向き補正後の元画像の高さ

$srcWidth : int
$srcHeight : int
$srcX : int
$srcY : int
$destWidth : int
$destHeight : int
$destX : int
$destY : int
$canvasWidth : int
$canvasHeight : int
$angle : int|null
Return values
bool

mimeTypeToExtension()

画像パスから画像拡張子を取得

protected mimeTypeToExtension(string $mimeType[, T $fallback = 'jpg' ]) : string|T
Parameters
$mimeType : string
$fallback : T = 'jpg'
Tags
template
Return values
string|T

resolveJpegQuality()

JPEG を再エンコードするときの品質を決める。設定の品質を、元の JPEG の品質までに抑える

protected resolveJpegQuality(string|TemporaryFile $srcPath, string|null $mimeType) : int

元が低い品質で圧縮されていると、それより高い品質で再エンコードしても失われた情報は戻らず、 ファイルサイズだけが増える。元の品質が分からない(JPEG 以外、推定できない)ときは設定の品質を使う。

Parameters
$srcPath : string|TemporaryFile
$mimeType : string|null

元画像の MIME タイプ

Return values
int

copyAnimatedImage()

private copyAnimatedImage(string|TemporaryFile $srcPath, string $destPath, string $extension) : void
Parameters
$srcPath : string|TemporaryFile
$destPath : string
$extension : string

estimateSourceJpegQuality()

元の JPEG の品質を、量子化テーブルから推定する(量子化テーブルは先頭付近にあるため、読むのはヘッダー部分だけ)

private estimateSourceJpegQuality(string|TemporaryFile $srcPath) : int|null
Parameters
$srcPath : string|TemporaryFile
Return values
int|null —

推定できなければ null

readGifBytes()

private readGifBytes(resource $stream, int $length) : string|null
Parameters
$stream : resource
$length : int
Tags
phpstan-impure
Return values
string|null

skipGifSubBlocks()

private skipGifSubBlocks(resource $stream) : bool
Parameters
$stream : resource
Tags
phpstan-impure
Return values
bool
On this page

Search results