Download the PHP package rkr/image without Composer
On this page you can find all versions of the php package rkr/image. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package image
Short Description A simple image manipulation library using gd-lib
License MIT
Informations about the package image
php-image-manipulation
A simple image manipulation library using gd-lib
Installation
Usage (PHP 8.1+)
More information is also available in the generated docs.
Resample proportionally
Enlarge canvas
Auto crop image with optional border
The crop function is solved via a separate algorithm. If a border width is specified as a percentage, but too much of the original graphic was cut away during the actual cropping, then this image portion is retained in the border area.
Text rendering (TrueType)
Requires GD with FreeType support and a .ttf/.otf font file.
Image API reference (all methods)
All methods throw Kir\Image\ImageRuntimeException on invalid input / unsupported operations.
Static methods
public static function getImageType(string $filename): int
Detects the image type constant (e.g.IMAGETYPE_PNG) for a file.public static function getDefaultImageExtension(string $filename): ?string
Returns a default extension (e.g."png") detected viamime_content_type.public static function supportsFormat(int|string $format, bool $forWriting = false): bool: Checks runtime read support byIMAGETYPE_*or extension; set$forWritingfor the library’s output formats.public static function getSupportedFormats(bool $forWriting = false): array: Returns canonical names for the formats supported by the current GD build and this library.public static function loadFromString(string $data): Image
Loads an image from raw bytes.public static function loadFromFile(string $filename): Image
Loads AVIF, BMP, GIF, JPEG, PNG, WBMP, WebP, XBM, XPM, TGA, GD and GD2 images from a file path. Optional formats must be enabled in the installed GD build.public static function create(int $width, int $height, ?Color $color = null, ?int $imageType = null): Image
Creates a new blank image (defaults to a transparent white background).
Object lifecycle
public function __construct(\GdImage $resource, ?int $type = null)
Wraps an existing GD image object (preferloadFromFile()/create()if possible).
Basic information
public function getGdImage(): \GdImage
Returns the underlying GD image.public function getWidth(): int
Returns the image width in pixels.public function getHeight(): int
Returns the image height in pixels.public function getFileType(): ?int
Returns the last knownIMAGETYPE_*(if available).public function getMimeType(): ?string
Returns a MIME type like"image/png"if the file type is known.public function getCopy(): self
Creates a full copy of the image.
Compositing / paste
public function placeImageOn(Image|\GdImage $targetImage, int $offsetX = 0, int $offsetY = 0): self
Deprecated alias forpasteOn().public function pasteOn(Image|\GdImage $targetImage, int $offsetX = 0, int $offsetY = 0): self
Copies this image onto the given target.
Pixel inspection
public function getRedColorAt(int $x, int $y): intpublic function getGreenColorAt(int $x, int $y): intpublic function getBlueColorAt(int $x, int $y): intpublic function getAlphaAt(int $x, int $y): int
Returns alpha in 0..255 (0 transparent, 255 opaque).public function getChannelColorAt(int $x, int $y, int $bitMask): int
Low-level helper used by the channel getters.
Filters / color operations
public function greyscale(): self
AppliesIMG_FILTER_GRAYSCALE.public function applyAlphaMaskFromGreyscaleImage(Image $mask): self
Uses a same-sized greyscale mask’s channel value as alpha for the current image.public function adjustColors(): self
Stretches colors to fill the full 0..255 range; constant-color images remain unchanged.
Geometric transformations
public function rotate(float $angle, ?Color $backgroundColor = null): self: Rotates anticlockwise, expands the canvas and uses transparent white for uncovered areas by default.public function flip(int $mode = IMG_FLIP_HORIZONTAL): self: Flips usingIMG_FLIP_HORIZONTAL,IMG_FLIP_VERTICALorIMG_FLIP_BOTH.
Auto-crop / content detection
public function crop(int $threshold = 15, int $borderPercent = 0, ?Color $backgroundColor = null): self
Removes excess whitespace around the detected inner object and optionally adds a border.public function detectInnerObject(int $threshold = 15): array
Returns measures:left,top,right,bottom,width,height.
Canvas resizing (crop/pad)
public function resizeCanvas(int $width, int $height, ?int $offsetX = null, ?int $offsetY = null, ?Color $backgroundColor = null): self
Changes canvas size; crops or pads with background color (offset defaults to centered).public function resizeCanvasCentered(int $width, int $height, ?Color $backgroundColor = null): self
Centered convenience wrapper forresizeCanvas().
Resampling / proportional resizing
public function resize(?int $width = null, ?int $height = null): self
Resamples to exact size (non-proportional); omitted dimensions keep current size.public function cropToCover(int $width, int $height): self: Resamples and center-crops to cover the exact target dimensions.public function shrinkProportional(?int $width = null, ?int $height = null): self
Only shrinks if the image is larger than the target rectangle.public function resizeProportional(?int $width = null, ?int $height = null): self
Resizes proportionally to fit inside the target rectangle.public function enlargeProportional(?int $width = null, ?int $height = null): self
Resizes proportionally aiming to enlarge to the target rectangle.
Drawing
public function fill(int $x, int $y, Color $color): self
Flood-fill starting at the given pixel.public function rectangle(int $x, int $y, int $width, int $height, Color $color): self
Draws a filled rectangle.
Text rendering (TrueType)
Requires GD with FreeType support.
public function measureText(string $text, string $fontFile, float $fontSize, float $angle = 0.0): array
Returnswidth,height,offsetX,offsetYand the rawbboxarray (fromimagettfbbox()).public function text(string $text, int $x, int $y, string $fontFile, float $fontSize, Color $color, float $angle = 0.0, string $anchor = 'top-left'): self
Draws TTF text; anchors includetop-left,center,bottom-right,baseline-left, etc.
Alpha/background
public function removeAlphaBackground(?Color $color = null): self
Replaces transparency with an opaque background color (default: white).
Saving / output
public function saveAs(?string $filename, ?int $explicitType = null): self
Saves based on extension (or forced type) and defaults to the last known file type.public function saveAsPng(?string $filename = null): selfpublic function saveAsJpeg(?string $filename = null, int $quality = 100): selfpublic function saveAsGif(?string $filename = null): selfpublic function saveAsWebP(?string $filename = null, int $quality = 100): selfpublic function saveAsBmp(?string $filename = null): selfpublic function saveAsString(?int $imageType = null, int $quality = 100): string
Encodes the image into bytes (useful for HTTP responses or storage).
Internal helpers (private, not part of the public API)
private static function temporaryRemoveAlphaChannel(Image $srcIm, \Closure $fn): void
Used by JPEG/GIF/BMP saving to replace alpha with an opaque background.private static function createResource(int $width, int $height, ?Color $color = null): \GdImage
Creates a new\GdImageand initializes alpha handling.private static function createGdColorFromColor(\GdImage $resource, Color $color): int
ConvertsColorinto a GD color index for the given image resource.
All versions of image with dependencies
ext-gd Version *