PHP Classes

SAAD Image: Laravel service provider to create image thumbnail

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 48 All time: 10,739 This week: 77Up
Version License PHP version Categories
saad-image 1.0GNU General Publi...5PHP 5, Graphics, Libraries
Description 

Author

This package is a Laravel service provider to create image thumbnail.

It takes an uploaded image and validates it to check if it matches certain types of allowed MIME types and file name extensions.

The class can create a thumbnail with a given width and height, optionally preserving the original image aspect.

Picture of Ahmed Saad
  Performance   Level  
Name: Ahmed Saad <contact>
Classes: 10 packages by
Country: Egypt Egypt
Age: ???
All time rank: 213721 in Egypt Egypt
Week rank: 53 Up2 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 7x

Documentation

Image Library, used to manipulate images like crop, resize and writing texts on images

Install

You can pull in the package via composer:

$ composer require saad/image

Register Package Service Provider for Laravel

> Laravel 5.5 The package will automatically register itself.

> Laravel < 5.5

> add the following to your providers in config\app.php > > `php > 'providers' => [ > ..... > Saad\Image\ImageServiceProvider::class, > ] > `

Publish package aconfiguration and assets

	php artisan vendor:publish --provider="Saad\Image\ImageServiceProvider"

use

to use Core Image Library

	<?php
	
	use Saad\Image\Image;
	
	$image = new Image( $image_src );
	
	// you can then manipulate image
	
	// Set Save Output Format
	$image->setOutputFormat('png');
	
	// Set Save Options
	$image->setSaveOptions('image_name', 'Save_Path');
	
	// Create Thumbnail
	$image->createThumbnail(100, 100);
	
	
	// Save and destroy resource from memory
	$image->export();
	
	// Save and keep resource to continue manipulating same resource
	$image->export(true);
	
	// Get Image as data-url string
	$image->embed();

Laravel Eloquent Traits

this package ships with two traits to make it easy to save images on eloquent models

EloquentImageSaverTrait used to dynamically save uploaded images and creating thumbnails

EloquentPublicImageTrait used to get public url for saved images and it's created thumbnails

__Full Example__

assume we have a user Model which has image column to store user profile image

	<?php
	
	....
	use Saad\Image\Traits\EloquentImageSaverTrait;
	use Saad\Image\Traits\EloquentPublicImageTrait;
	
	class User extends Model
	{
		use EloquentImageSaverTrait, EloquentPublicImageTrait;
		
		protected static $profile_image_sizes = [
	        	[ 256, 256 ],  	// Default image size
	        	[ 100, 100 ], 	// Thumbnail
	        	[ 46, 46 ],		// Thumbnail
	        	[ 26, 26 ],		// Thumbnail
	        ];
	
		/
	     * Get Image
	     * @return String       Image Url
	     */
	    public function getImage()
	    {
	        return $this->getImagePublicLink( 'image', 'images/profiles/' );
	    }

	    /
	     * Get Image Thumbnail
	     * @param  String $size Thumbnail size in format '46x46'
	     * @return String       Image Thumbnail Url
	     */
	    public function getImageThumb( $size )
	    {
	        return $this->getImagePublicLink( 'image', 'images/profiles/thumb/', $size );
	    }
	    
	    
		
		/
	     * Mutator To Save and Set Image
	     *
	     * Save Image and create thumbnails, and set image name attribute to model
	     */
	    public function setImageAttribute( $file )
	    {
	        $path = public_path( 'images/profiles/' );        

	        if($file instanceof \Illuminate\Http\UploadedFile) {

	            $this->attributes['image'] = $this->saveImage( $file, $path, null, static::$profile_image_sizes, function( $object, $save_name ) use($path){
	                /
	                 * Delete Old Images
	                 */
	                $this->deleteOldFor( $object->image, $path );
	                
	            } );

	        } else {
	            $file = realpath(public_path('/images/temp/'.$file));
	            $this->attributes['image'] = $this->saveLocalImage( $file, $path, null, static::$profile_image_sizes, function( $object, $save_name ) use($path){
	                /
	                 * Delete Old Images
	                 */
	                $this->deleteOldFor( $object->image, $path );
	            });
	        }
    		}
    }

  Files folder image Files (13)  
File Role Description
Files folder imageconfig (1 file)
Files folder imagesrc (2 files, 3 directories)
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 README.md Doc. Documentation

  Files folder image Files (13)  /  config  
File Role Description
  Accessible without login Plain text file saad-image.php Aux. Auxiliary script

  Files folder image Files (13)  /  src  
File Role Description
Files folder imageCaptcha (1 file)
Files folder imageExceptions (3 files)
Files folder imageTraits (3 files)
  Plain text file Image.php Class Class source
  Plain text file ImageServiceProvider.php Class Class source

  Files folder image Files (13)  /  src  /  Captcha  
File Role Description
  Plain text file Captcha.php Class Class source

  Files folder image Files (13)  /  src  /  Exceptions  
File Role Description
  Plain text file CaptchaException.php Class Class source
  Plain text file ImageException.php Class Class source
  Plain text file NotSupportedImageFormatException.php Class Class source

  Files folder image Files (13)  /  src  /  Traits  
File Role Description
  Plain text file CopyrightTrait.php Class Class source
  Plain text file EloquentImageSaverTrait.php Class Class source
  Plain text file EloquentPublicImageTrait.php Class Class source

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:48
This week:0
All time:10,739
This week:77Up