dev-master
9999999-devFramework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails
The Requires
- php >=5.3.0
image thumbnails cropping resizing
Framework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails
Framework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails et al..., (*1)
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)., (*2)
To use this library in your projects, reference the library in your composer.json
's "require" node:, (*3)
"require": { "craftsmancoding/image": "dev-master" }
The run composer install
or composer update
., (*4)
Inclusion via Composer is the recommended way of utilizing this library., (*5)
To use this library without composer, download the Image.php class and require it. Call functions statically., (*6)
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:, (*7)
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); } }
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., (*8)
Syntax: crop(string $src, string $dst,int $x, int $y,int $w, int $h, number $ratio=1)
, (*9)
Output: full path to cropped image (i.e. the destination)., (*10)
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., (*11)
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., (*12)
$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:, (*13)
, (*14)
Cropped to an area of 250x325:, (*15)
, (*16)
The exact same crop as above could be performed using a multiplier $ratio
of 2:, (*17)
$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., (*18)
Syntax: scale(string $src, string $dst, int $new_w, int $new_h)
, (*19)
Output: full path to destination., (*20)
WARNING: This function may distort the aspect-ratio of the image., (*21)
$src = '/path/to/image.jpg'; // e.g. 300x225 $dst = '/path/to/stretched.jpg'; \Craftsmancoding\Image::scale($src,$dst,400,100);
, (*22)
Becomes stretched if you are not careful!, (*23)
, (*24)
Use scale2h
or scale2w
if you are concerned about preserving the aspect ratio., (*25)
Syntax: scale2h(string $src, string $dst, int $new_h)
, (*26)
Output: full path to destination., (*27)
The width will be scaled automatically to maintain the original aspect-ratio., (*28)
Syntax: scale2w(string $src, string $dst, int $new_w)
, (*29)
Output: full path to destination., (*30)
The height will be scaled automatically to maintain the original aspect-ratio., (*31)
$src = '/path/to/image.jpg'; // e.g. 300x225 $dst = '/path/to/stretched.jpg'; \Craftsmancoding\Image::scale2w($src,$dst,150);
, (*32)
The height will be calculated automatically in order to preserve the aspect ratio:, (*33)
, (*34)
Syntax: thumbnail(string $src, string $dst, int $w, int $h)
, (*35)
Output: full path to destination., (*36)
The thumbnail
function will perform a zoom and crop operation to best fit the thumb. For more control, use the crop
function., (*37)
$src = '/path/to/wide.jpg'; $dst = '/path/to/wide.thumb.jpg'; \Craftsmancoding\Image::thumbnail($src,$dst,200,200);
, (*38)
Becomes horizontally centered:, (*39)
, (*40)
$src = '/path/to/tall.jpg'; $dst = '/path/to/tall.thumb.jpg'; \Craftsmancoding\Image::thumbnail($src,$dst,200,200);
, (*41)
Becomes vertically centered:, (*42)
, (*43)
Framework agnostic image manipulation library, Imagick not required. Scale and crop images, create thumbnails
image thumbnails cropping resizing