PHPackages                             craftsmancoding/image - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. craftsmancoding/image

ActiveLibrary[Image &amp; Media](/categories/media)

craftsmancoding/image
=====================

Framework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails

01541PHP

Since Oct 11Pushed 11y ago2 watchersCompare

[ Source](https://github.com/craftsmancoding/image)[ Packagist](https://packagist.org/packages/craftsmancoding/image)[ RSS](/packages/craftsmancoding-image/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Image
=====

[](#image)

Framework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails et al...

This library offers a clean interface for using PHP image manipulation functions. It was created to provide a unified interface between various projects on various platforms and environments (some without Imagick).

### Supported Functions

[](#supported-functions)

- **crop** : crop an image
- **scale** : scale an image to a new height and width.
- **scale2h** : scale an image to a new height while preserving the aspect ratio
- **scale2w** : scale an image to a new width while preserving the aspect ratio
- **thumbnail** : create a thumbnail with specified dimensions, zooming and cropping.
- ... more coming...

---

Usage
-----

[](#usage)

### Composer

[](#composer)

To use this library in your projects, reference the library in your `composer.json`'s "require" node:

```
"require": {
    "craftsmancoding/image": "dev-master"
}

```

The run `composer install` or `composer update`.

Inclusion via Composer is the recommended way of utilizing this library.

### Without Composer

[](#without-composer)

To use this library without composer, download the Image.php class and require it. Call functions statically.

```
require_once 'path/to/Image.php';

\Craftsmancoding\Image::scale2h('/path/to/img.jpg', '/path/to/new.jpg', 100);

```

If you are using namespaces (e.g. inside a class), the calls might look like this:

```
require_once 'path/to/Image.php';
use Craftsmancoding;

class MyClass {
    public function my_method() {
        Image::scale2h('/path/to/img.jpg', '/path/to/new.jpg', 100);
    }
}

```

---

Syntax
------

[](#syntax)

All the functions in this library class are meant to be called statically -- no instantiation is required and no class variables persist. No object oriented stuff here.

---

### Crop (crop)

[](#crop-crop)

**Syntax:** `crop(string $src, string $dst,int $x, int $y,int $w, int $h, number $ratio=1)`

- **$src** : full path to source image
- **$dst** : destination (full path) where you want to create the cropped image
- **$x** : x-coordinate for start of crop area (0 = left)
- **$y** : y-coordinate for start of crop area (0 = top)
- **$w** : width of crop area (in pixels)
- **$h** : height of crop area (in pixels)
- **$ratio** : multiplier of actual-width/displayed-width. Useful if the image was displayed less than actual size.

**Output:** full path to cropped image (i.e. the destination).

The crop area is specified in X-Y coordinates where 0,0 is located at the top left of the image. Use the `$ratio` attribute for compatibility with jCrop or other instances when the image is displayed at a size other than its actual size.

### Example

[](#example)

With cropping you need to adjust the parameters to find the area you are looking for. Libraries like jCrop can be useful for doing this visually.

```
$x = 600; // from left
$y = 300; // from top
$w = 250;
$h = 325;
\Craftsmancoding\Image::crop($src,$dst,$x,$y,$w,$h);

```

Here's our huge image:

[![Regular Image](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/A.jpg?raw=true "Regular Image")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/A.jpg?raw=true)

Cropped to an area of 250x325:

[![Cropped](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/A.crop.jpg?raw=true "Cropped Area")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/A.crop.jpg?raw=true)

### Example: Using a Ratio

[](#example-using-a-ratio)

The exact same crop as above could be performed using a multiplier `$ratio` of 2:

```
$x = 300; // from left
$y = 150; // from top
$w = 125;
$h = 163;
$ratio = 2;
Image::crop($src,$dst,$x,$y,$w,$h,$ratio);

```

The `$ratio` multiplier is useful when the image is not displayed at actual size.

---

### scale (Scale to given dimensions)

[](#scale-scale-to-given-dimensions)

**Syntax:** `scale(string $src, string $dst, int $new_w, int $new_h)`

- **$src** : full path to source image
- **$dst** : destination (full path) to where you want to save the scaled image
- **$new\_w** : desired width of the new destination image (in pixels)
- **$new\_h** : desired height of the new destination image (in pixels)

**Output:** full path to destination.

WARNING: This function may distort the aspect-ratio of the image.

#### Example: Stretch

[](#example-stretch)

```
$src = '/path/to/image.jpg'; // e.g. 300x225
$dst = '/path/to/stretched.jpg';
\Craftsmancoding\Image::scale($src,$dst,400,100);

```

[![Regular Image](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.jpg?raw=true "Regular Image")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.jpg?raw=true)

Becomes stretched if you are not careful!

[![Thumbnail](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.distorted.jpg?raw=true "Distorted Aspect Ratio")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.distorted.jpg?raw=true)

Use `scale2h` or `scale2w` if you are concerned about preserving the aspect ratio.

---

### scale2h (Scale to Height)

[](#scale2h-scale-to-height)

**Syntax:** `scale2h(string $src, string $dst, int $new_h)`

- **$src** : full path to source image
- **$dst** : destination (full path) to where you want to save the scaled image
- **$new\_h** : desired height of the new destination image (in pixels)

**Output:** full path to destination.

The width will be scaled automatically to maintain the original aspect-ratio.

---

### scale2w (Scale to Width)

[](#scale2w-scale-to-width)

**Syntax:** `scale2w(string $src, string $dst, int $new_w)`

- **$src** : full path to source image
- **$dst** : destination (full path) to where you want to save the scaled image
- **$new\_w** : desired width of the new destination image (in pixels)

**Output:** full path to destination.

The height will be scaled automatically to maintain the original aspect-ratio.

```
$src = '/path/to/image.jpg'; // e.g. 300x225
$dst = '/path/to/stretched.jpg';
\Craftsmancoding\Image::scale2w($src,$dst,150);

```

[![Regular Image](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.jpg?raw=true "Regular Image")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.jpg?raw=true)

The height will be calculated automatically in order to preserve the aspect ratio:

[![Width Scaled](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.150x112.jpg?raw=true "Correct Aspect Ratio")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/E.150x112.jpg?raw=true)

---

### thumbnail

[](#thumbnail)

**Syntax:** `thumbnail(string $src, string $dst, int $w, int $h)`

- **$src** : full path to source image
- **$dst** : destination (full path) to where you want to save the thumbnail
- **$w** : width of thumbnail (in pixels)
- **$h** : height of thumbnail (in pixels)

**Output:** full path to destination.

The `thumbnail` function will perform a zoom and crop operation to best fit the thumb. For more control, use the `crop` function.

#### Example: Thumbnail of Wide Image

[](#example-thumbnail-of-wide-image)

```
$src = '/path/to/wide.jpg';
$dst = '/path/to/wide.thumb.jpg';
\Craftsmancoding\Image::thumbnail($src,$dst,200,200);

```

[![Wide Image](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/D.jpg?raw=true "Wide Image")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/D.jpg?raw=true)

Becomes horizontally centered:

[![Thumbnail](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/D.expected_thumb.jpg?raw=true "Thumbnail")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/D.expected_thumb.jpg?raw=true)

#### Example: Thumbnail of Tall Image

[](#example-thumbnail-of-tall-image)

```
$src = '/path/to/tall.jpg';
$dst = '/path/to/tall.thumb.jpg';
\Craftsmancoding\Image::thumbnail($src,$dst,200,200);

```

[![Tall Image](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/C.jpg?raw=true "Wide Image")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/C.jpg?raw=true)

Becomes vertically centered:

[![Thumbnail](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/C.expected_thumb.jpg?raw=true "Thumbnail")](https://raw.githubusercontent.com/craftsmancoding/image/master/tests/assets/C.expected_thumb.jpg?raw=true)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/140c3816138c95afc9685ad0757666f5794ba2ef54958ae534d4c91aed66162d?d=identicon)[craftsmancoding](/maintainers/craftsmancoding)

---

Top Contributors

[![fireproofsocks](https://avatars.githubusercontent.com/u/303735?v=4)](https://github.com/fireproofsocks "fireproofsocks (19 commits)")[![dadaniels](https://avatars.githubusercontent.com/u/8371187?v=4)](https://github.com/dadaniels "dadaniels (2 commits)")

### Embed Badge

![Health badge](/badges/craftsmancoding-image/health.svg)

```
[![Health](https://phpackages.com/badges/craftsmancoding-image/health.svg)](https://phpackages.com/packages/craftsmancoding-image)
```

###  Alternatives

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k23](/packages/bkwld-croppa)[goat1000/svggraph

Generates SVG graphs

132849.6k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)[netresearch/rte-ckeditor-image

Image support in CKEditor for the TYPO3 ecosystem - by Netresearch

63991.3k4](/packages/netresearch-rte-ckeditor-image)[humanmade/tachyon-plugin

Rewrites WordPress image URLs to use Tachyon

87338.5k2](/packages/humanmade-tachyon-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
