1. Go to this page and download the library: Download peterujah/nano-image library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
peterujah / nano-image example snippets
use Peterujah\Media\NanoImage;
use Peterujah\Media\Exceptions\UnsupportedImageException;
try {
$image = new NanoImage('path/to/photo.jpg');
// or: (new NanoImage())->open('path/to/photo.jpg')
// Fit inside 360x200 keeping the aspect ratio, save as photo-360x200.jpg
$image->resize(360, 200, true);
$image->save('path/to/out/photo.jpg', NanoImage::THUMBNAIL, 80);
// The instance is still usable: make another size and a WebP copy
$image->resize(116, 80);
$image->saveAs('path/to/out/photo.jpg', NanoImage::THUMBNAIL, 90, NanoImage::WEBP);
$image->free();
} catch (UnsupportedImageException $e) {
echo $e->getMessage(); // the file is missing, corrupt, unsupported, or GD is not installed
}
$image = new NanoImage('photo.jpg'); // a file path (the class checks it is a real image)
$image = new NanoImage($binaryImageData); // raw image data also works
$image = new NanoImage(); // nothing loaded yet
$image->open('photo.jpg'); // from a file or URL readable by getimagesize()
$image->load(file_get_contents('photo.png')); // from a string
$image->getWidth(); // int, pixels of the image as currently held
$image->getHeight(); // int
$image->getMimeType(); // e.g. "image/jpeg"
$image->getExifData(); // e.g. "image/jpeg"
$image->resize(200, 200); // exactly 200x200 (may distort)
$image->resize(200, 200, true); // fit inside a 200x200 box, keep proportions (e.g. 200x133)
$image->scale(0.5); // half the size, keeping proportions
$image->setWidth(640)->setHeight(480)->aspectRatio(true); // the same idea, step by step
$image->crop(10, 20, 300, 200); // x, y, width, height; throws if outside the image
$image->rotate(90); // counter-clockwise (GD convention); -90 is clockwise
$image->rotate(30, '#ffffff'); // corners filled white ("#rgb", "#rrggbb", "#" optional)
$image->flip(); // NanoImage::FLIP_HORIZONTAL (default)
$image->flip(NanoImage::FLIP_VERTICAL); // or FLIP_BOTH
$image->grayscale();
$image->brightness(30); // -255..255
$image->contrast(20); // -100..100, positive = more contrast
$image->blur(5); // immediate; see the note below
// Any GD filter, queued until the image is written
$image->filter(IMG_FILTER_COLORIZE, 100, 50, 0);
$image->filters([
[IMG_FILTER_CONTRAST, -50], // GD's own contrast sign: negative = MORE contrast
[IMG_FILTER_GAUSSIAN_BLUR],
]);
// 1. Send to the browser (sets the Content-Type header, nothing is sent if encoding fails)
$image->display(90);
// 2. Get the encoded bytes
$bytes = $image->toString(90); // '' on failure
file_put_contents('copy.jpg', $bytes);
// 3. Write the bytes to the current output without a header
$image->get(90);
// 4. Save to disk
$image->save('out/photo.jpg', NanoImage::DEFAULT, 90);
echo $image->getSavedPath(); // the file that was actually written
$image->save('out/photo.jpg', NanoImage::TIMESTAMP, 85);
$image->saveAs('out/photo.png', NanoImage::DEFAULT, 90, NanoImage::WEBP); // writes out/photo.webp
$image->replace('out/photo.jpg', 90); // overwrite that file
$image->removeExif('out/clean.jpg'); // see the metadata note below
$image->remove(); // delete the file the image was OPENED from (not the saves)
$image->free(); // release memory and reset the instance
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.