Create bulk image thumbnails or scale to eaxct size instantly with PHP and the awesome GD library.
GD library is builtin with most PHP build. To make sure, use phpinfo()
.
This library will create thumbnails of all images from the given directory path and store them wherever you want. You can just resize proportionally, crop to exact dimension after resizing proportionally and compress to reduce image size keeping good quality.
This library comes with default HTML resized image list.
Require the class.imageresizer.php
from your file.
require_once ('class.imageresizer.php');
Now pass an argument associative array with options
// Create thumbnails
$args = array(
'height' => 975,
'width' => 650,
'is_crop_hard' => 1
);
$img = new ImageResizer($args);
$img->create();
You can just use default properties by just:
$img = new ImageResizer();
$img->create();
create()
function will resize all image files from the folder. To resize only one image, use createThumbnail()
. You have to pass the image filename with extension, width in pixel and height in pixel as arguments:
$args = array(
'compress' => 0.8,
'is_crop_hard' => 1
);
$img = new ImageResizer($args);
$img->createThumbnail('Desires_LB_MF16_7290.jpg',300,450);
To prevent the class from printing image resize list as HTML, use resize_list
property:
$args = array(
'height' => 400,
'width' => 270,
'is_crop_hard' => 1,
'resize_list' => false
);
$img = new ImageResizer($args);
$msg = $img->create();
// Now we can do whatever we want, maybe JSON
print_r(json_encode($msg));
Key | Type | Value | Default |
---|---|---|---|
height | int/float | Thumbnail height in px | 200 |
width | int/float | Thumbnail width in px | 200 |
img_dir | string | Full size image directory path | '/img' |
thumb_dir | string | Thumbnail image directory path (Remember to add extra backslash after. You can use file name prefix :) ) | '/thumb/' |
compress | int/float | Image compression (0~1, 0.15 is 15% ) | 0.8 |
is_crop_hard | boolean | Crops the image with exact height & width proportionally from the center of the image | false |
resize_list | boolean | Prevents default HTML resized image list | true |
This is how it works:
require_once ('class.imageresizer.php');
// Create thumbnails
$args = array(
'height' => 975,
'width' => 650,
'is_crop_hard' => 1
);
$img = new ImageResizer($args);
$img->create();
Md. Hasan Shahriar
Github: https://github.com/hsleonis
Email: hsleonis2@gmail.com
2016
Copyright (c) 2016 Md. Hasan Shahriar Licensed under the The MIT License (MIT).