Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added the start.php and resizer.php files. updated readme

  • Loading branch information...
commit b4ee44b62f6e7d5d962bb508a1d09e7c9ea25f27 1 parent 33813c0
@maikeldaloo authored
Showing with 392 additions and 3 deletions.
  1. +44 −3 README.md
  2. +343 −0 resizer.php
  3. +5 −0 start.php
View
47 README.md
@@ -1,4 +1,45 @@
-Resizer
-=======
+# Resizer
-Simple image resizing bundle for Laravel PHP framework
+Simple image resizing bundle for Laravel PHP framework.
+
+This bundle will allow you to resize an uploaded image or any image from the file system with ease.
+
+### Installation
+
+Install Resizer using artisan:
+
+ php artisan bundle:install resizer
+
+Then in your *application/bundles.php* file, add the following line to load Resizer automatically:
+
+ return array(
+ 'resizer' => array( 'auto' => true )
+ );
+
+Or without the `'auto' => true` to load it on demand:
+
+ return array(
+ 'resizer'
+ );
+
+### Usage
+
+In your view files, you'd add a file input element.
+
+ <?php Input::file('picture') ?>
+
+In your routes.php file or in any of your controller files, you can start the Resizer bundle if you haven't set it to auto-load by calling:
+
+ Bundle::start('resizer');
+
+Then you can start resizing your images by simple calling:
+
+ Resizer::open( mixed $file )
+ ->resize( int $width , int $height , string 'exact, portrait, landscape, auto or crop' )
+ ->save( string 'path/to/file.jpg' , int $quality );
+
+### Credits
+
+The image resize class was originally written in a tutorial by Jarrod Oberto on [NetTuts+](http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/). I only modified it to use Laravel's File class, updated the coding style, added comments throughout the class file and turned it into a Laravel bundle.
+
+Enjoy.
View
343 resizer.php
@@ -0,0 +1,343 @@
+<?php
+
+/**
+ * Provides a very simple way to resize an image.
+ *
+ * Credits to Jarrod Oberto.
+ * Jarrod wrote a tutorial on NetTuts.
+ * http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
+ *
+ * I only turned it into a Laravel bundle.
+ *
+ * @package Resizer
+ * @version 1.0
+ * @author Maikel D (original author Jarrod Oberto)
+ * @link
+ * @example
+ * Resizer::open( mixed $file )
+ * ->resize( int $width , int $height , string 'exact, portrait, landscape, auto or crop' )
+ * ->save( string 'path/to/file.jpg' , int $quality );
+ *
+ * Resizer::open( Input::file('field_name') )
+ * ->resize( 800 , 600 , 'crop' )
+ * ->save( 'path/to/file.jpg' , 100 );
+ */
+class Resizer {
+
+ /**
+ * Store the image resource which we'll modify
+ * @var Resource
+ */
+ private $image;
+
+ /**
+ * Original width of the image we're modifying
+ * @var int
+ */
+ private $width;
+
+ /**
+ * Original height of the image we're modifying
+ * @var int
+ */
+ private $height;
+
+ /**
+ * Store the resource of the resized image
+ * @var Resource
+ */
+ private $image_resized;
+
+ /**
+ * Instantiates the Resizer and receives the path to an image we're working with
+ * @param mixed $file The file array provided by Laravel's Input::file('field_name') or a path to a file
+ */
+ function __construct( $file )
+ {
+ // Open up the file
+ $this->image = $this->open_image( $file );
+
+ // Get width and height of our image
+ $this->width = imagesx( $this->image );
+ $this->height = imagesy( $this->image );
+ }
+
+ /**
+ * Static call, Laravel style.
+ * Returns a new Resizer object, allowing for chainable calls
+ * @param mixed $file The file array provided by Laravel's Input::file('field_name') or a path to a file
+ * @return Resizer
+ */
+ public static function open( $file )
+ {
+ return new Resizer( $file );
+ }
+
+ /**
+ * Resizes and/or crops an image
+ * @param int $new_width The width of the image
+ * @param int $new_height The height of the image
+ * @param string $option Either exact, portrait, landscape, auto or crop.
+ * @return [type]
+ */
+ public function resize( $new_width , $new_height , $option = 'auto' )
+ {
+ // Get optimal width and height - based on $option
+ $option_array = $this->get_dimensions( $new_width , $new_height , $option );
+
+ $optimal_width = $option_array['optimal_width'];
+ $optimal_height = $option_array['optimal_height'];
+
+ // Resample - create image canvas of x, y size
+ $this->image_resized = imagecreatetruecolor( $optimal_width , $optimal_height );
+ imagecopyresampled( $this->image_resized , $this->image , 0 , 0 , 0 , 0 , $optimal_width , $optimal_height , $this->width , $this->height );
+
+ // if option is 'crop', then crop too
+ if ( $option == 'crop' ) {
+ $this->crop( $optimal_width , $optimal_height , $new_width , $new_height );
+ }
+
+ // Return $this to allow calls to be chained
+ return $this;
+ }
+
+ /**
+ * Save the image based on its file type.
+ * @param string $save_path Where to save the image
+ * @param int $image_quality The output quality of the image
+ * @return boolean
+ */
+ public function save( $save_path , $image_quality = 95 )
+ {
+ // Get extension of the output file
+ $extension = strtolower( File::extension($save_path) );
+
+ // Create and save an image based on it's extension
+ switch( $extension )
+ {
+ case 'jpg':
+ case 'jpeg':
+ if ( imagetypes() & IMG_JPG ) {
+ imagejpeg( $this->image_resized , $save_path , $image_quality );
+ }
+ break;
+
+ case 'gif':
+ if ( imagetypes() & IMG_GIF ) {
+ imagegif( $this->image_resized , $save_path );
+ }
+ break;
+
+ case 'png':
+ // Scale quality from 0-100 to 0-9
+ $scale_quality = round( ($image_quality/100) * 9 );
+
+ // Invert quality setting as 0 is best, not 9
+ $invert_scale_quality = 9 - $scale_quality;
+
+ if ( imagetypes() & IMG_PNG ) {
+ imagepng( $this->image_resized , $save_path , $invert_scale_quality );
+ }
+ break;
+
+ default:
+ return false;
+ break;
+ }
+
+ // Remove the resource for the resized image
+ imagedestroy( $this->image_resized );
+
+ return true;
+ }
+
+ /**
+ * Open a file, detect its mime-type and create an image resrource from it.
+ * @param array $file Attributes of file from the $_FILES array
+ * @return mixed
+ */
+ private function open_image( $file )
+ {
+
+ // If $file isn't an array, we'll turn it into one
+ if ( !is_array($file) ) {
+ $file = array(
+ 'type' => File::mime( File::extension($file) ),
+ 'tmp_name' => $file
+ );
+ }
+
+ $mime = $file['type'];
+ $file_path = $file['tmp_name'];
+
+ switch ( $mime )
+ {
+ case File::mime('jpg'): $img = @imagecreatefromjpeg( $file_path ); break;
+ case File::mime('gif'): $img = @imagecreatefromgif( $file_path ); break;
+ case File::mime('png'): $img = @imagecreatefrompng( $file_path ); break;
+ default: $img = false; break;
+ }
+
+ return $img;
+ }
+
+ /**
+ * Return the image dimentions based on the option that was chosen.
+ * @param int $new_width The width of the image
+ * @param int $new_height The height of the image
+ * @param string $option Either exact, portrait, landscape, auto or crop.
+ * @return array
+ */
+ private function get_dimensions( $new_width , $new_height , $option )
+ {
+ switch ( $option )
+ {
+ case 'exact':
+ $optimal_width = $new_width;
+ $optimal_height = $new_height;
+ break;
+ case 'portrait':
+ $optimal_width = $this->get_size_by_fixed_height( $new_height );
+ $optimal_height = $new_height;
+ break;
+ case 'landscape':
+ $optimal_width = $new_width;
+ $optimal_height = $this->get_size_by_fixed_width( $new_width );
+ break;
+ case 'auto':
+ $option_array = $this->get_size_by_auto( $new_width , $new_height );
+ $optimal_width = $option_array['optimal_width'];
+ $optimal_height = $option_array['optimal_height'];
+ break;
+ case 'crop':
+ $option_array = $this->get_optimal_crop( $new_width , $new_height );
+ $optimal_width = $option_array['optimal_width'];
+ $optimal_height = $option_array['optimal_height'];
+ break;
+ }
+
+ return array(
+ 'optimal_width' => $optimal_width,
+ 'optimal_height' => $optimal_height
+ );
+ }
+
+ /**
+ * Returns the width based on the image height
+ * @param int $new_height The height of the image
+ * @return int
+ */
+ private function get_size_by_fixed_height( $new_height )
+ {
+ $ratio = $this->width / $this->height;
+ $new_width = $new_height * $ratio;
+
+ return $new_width;
+ }
+
+ /**
+ * Returns the height based on the image width
+ * @param int $new_width The width of the image
+ * @return int
+ */
+ private function get_size_by_fixed_width( $new_width )
+ {
+ $ratio = $this->height / $this->width;
+ $new_height = $new_width * $ratio;
+
+ return $new_height;
+ }
+
+ /**
+ * Checks to see if an image is portrait or landscape and resizes accordingly.
+ * @param int $new_width The width of the image
+ * @param int $new_height The height of the image
+ * @return array
+ */
+ private function get_size_by_auto( $new_width , $new_height )
+ {
+ // Image to be resized is wider (landscape)
+ if ( $this->height < $this->width )
+ {
+ $optimal_width = $new_width;
+ $optimal_height= $this->get_size_by_fixed_width( $new_width );
+ }
+ // Image to be resized is taller (portrait)
+ elseif ( $this->height > $this->width )
+ {
+ $optimal_width = $this->get_size_by_fixed_height( $new_height );
+ $optimal_height= $new_height;
+ }
+ // Image to be resizerd is a square
+ else
+ {
+ if ( $new_height < $new_width ) {
+ $optimal_width = $new_width;
+ $optimal_height= $this->get_size_by_fixed_width( $new_width );
+ } else if ( $new_height > $new_width ) {
+ $optimal_width = $this->get_size_by_fixed_height( $new_height );
+ $optimal_height= $new_height;
+ } else {
+ // Sqaure being resized to a square
+ $optimal_width = $new_width;
+ $optimal_height= $new_height;
+ }
+ }
+
+ return array(
+ 'optimal_width' => $optimal_width,
+ 'optimal_height' => $optimal_height
+ );
+ }
+
+ /**
+ * Attempts to find the best way to crop. Whether crop is based on the
+ * image being portrait or landscape.
+ * @param int $new_width The width of the image
+ * @param int $new_height The height of the image
+ * @return array
+ */
+ private function get_optimal_crop( $new_width , $new_height )
+ {
+ $height_ratio = $this->height / $new_height;
+ $width_ratio = $this->width / $new_width;
+
+ if ( $height_ratio < $width_ratio ) {
+ $optimal_ratio = $height_ratio;
+ } else {
+ $optimal_ratio = $width_ratio;
+ }
+
+ $optimal_height = $this->height / $optimal_ratio;
+ $optimal_width = $this->width / $optimal_ratio;
+
+ return array(
+ 'optimal_width' => $optimal_width,
+ 'optimal_height' => $optimal_height
+ );
+ }
+
+ /**
+ * Crops an image from its center
+ * @param int $optimal_width The width of the image
+ * @param int $optimal_height The height of the image
+ * @param int $new_width The new width
+ * @param int $new_height The new height
+ * @return true
+ */
+ private function crop( $optimal_width , $optimal_height , $new_width , $new_height )
+ {
+ // Find center - this will be used for the crop
+ $crop_start_x = ( $optimal_width / 2 ) - ( $new_width / 2 );
+ $crop_start_y = ( $optimal_height / 2 ) - ( $new_height / 2 );
+
+ $crop = $this->image_resized;
+
+ // Now crop from center to exact requested size
+ $this->image_resized = imagecreatetruecolor( $new_width , $new_height );
+ imagecopyresampled( $this->image_resized , $crop , 0 , 0 , $crop_start_x , $crop_start_y , $new_width , $new_height , $new_width , $new_height );
+
+ return true;
+ }
+
+}
View
5 start.php
@@ -0,0 +1,5 @@
+<?php
+
+Autoloader::map(array(
+ 'Resizer' => __DIR__ . DS . 'resizer.php'
+));
Please sign in to comment.
Something went wrong with that request. Please try again.