PHP Classes

Nano PHP Image Resize: Resize local or remote images

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-11-27 (9 days ago) RSS 2.0 feedNot enough user ratingsTotal: 132 This week: 1All time: 9,327 This week: 46Up
Version License PHP version Categories
nano-image 1.0.3GNU General Publi...5PHP 5, Graphics
Description 

Author

This class can resize local or remote images.

It can take an image's path or URL and create a resized copy of the image in a given local directory.

The class can create resized images that preserve the ratio between the width and height of the original image.

Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 29 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 11x

Example

<?php
 
require_once __DIR__ "/autoload.php";
  use
Peterujah\NanoBlock\NanoImage;
  use
Peterujah\NanoBlock\UnsupportedImageException;
  if(isset(
$_FILES['image'])){
   
$ImageTemp = $_FILES['image']['tmp_name'];
   
$fileName = 'old-image.jpg';
   
$filePathAsset = __DIR__ . '/assets/img/nano/';
   
    if(!
is_dir($filePathAsset)){
     
mkdir($urlPathAsset, 0755, true);
    }

    if(
file_exists($filePathAsset . $fileName)){
     
unlink($filePathAsset . $fileName);
    }

    if (@
move_uploaded_file($ImageTemp, $filePathAsset . $fileName)) {
     
$imagine = new NanoImage();
      try{
       
$image = $imagine->open($logoPath . $fileName);
 
       
$image->resize(360, 200, false);
       
$image->save($filePathAsset . $imageName, NanoImage::THUMBNAIL, 80);
 
       
$image->resize(116, 80, false);
       
$image->saveAs($filePathAsset . $imageName, NanoImage::THUMBNAIL, 100, $imagine::JPEG);
 
       
$image->free();
      }catch(
UnsupportedImageException $e){
        echo
$e->getMessage();
      }
    }
  }


Details

Nano Image

NanoImage is a simple PHP image resize class. It can resize images and display them in the browser or save images in a directory

Installation

Installation is super-easy via Composer:

composer require peterujah/nano-image

Basic Usage

$image = new NanoImage();
try{
  $image = $imagine->open("path/to/save/edit-image-size.jpg");

  $image->resize(360, 200, false);
  $image->save("path/to/save/new-image.jpg", NanoImage::THUMBNAIL, 80);

  $image->resize(116, 80, false);
  $image->saveAs("path/to/save/new-image.jpeg", NanoImage::THUMBNAIL, 100, NanoImage::JPEG);

  $image->free();
}catch(UnsupportedImageException $e){
  echo $e->getMessage();
}

$img = new Peterujah\NanoBlock\NanoImage();

Initialize nano image class for use

Open and load any image from a directory path & file name

$img->open(__DIR__ . "/path/to/assets/image.jpg");

Or load string containing the image data.

$img->load($image_data);

Resize an image with the exact width and height passed, for example, 200x200. To resize an image using the aspect ratio set the third parameter to true

$img->resize(200, 200, false||true);

Blur image

$img->blur(20);

Once image manipulation is done, display the output image on the browser. Pass quality of the image

$img->display($quality);

Save an image to a directory, the first parameter specifies the path, the second sets the image naming option, and the third is image quality (90 by default)

$img->save(__DIR__ . "/path/to/assets/new-image.jpg", NanoImage::DEFAULT, $quality);

Save image as

$img->saveAs($to,  NanoImage::DEFAULT || NanoImage::THUMBNAIL || NanoImage::TIMESTAMP, $quality, self::JPEG)

Replace the existing image with a new one

$img->replace($to, $quality)

Remove temp image after editing and free memory

$img->remove()

Free memory

$img->free()

  Files folder image Files (7)  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetest (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file NanoImage.php Class Class source
  Plain text file UnsupportedImageException.php Class Class source

  Files folder image Files (7)  /  test  
File Role Description
  Accessible without login Plain text file upload.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:132
This week:1
All time:9,327
This week:46Up