Image Class

With this class you will be able to:

  • Manipulate any jpg, gif, or png image
  • Convert it to the desired image type
  • Resize the image to any width or height
    • While maintaining the original proportions
  • Extract a square portion of the image
  • Resize the image unlimited times without losing the original image's quality
  • Save the image(s) to your server
  • Display the image to your user

object Image ( )

Creates a new image object.

Example
$image = new Image;

bool url ( string $website )

Opens an image resource from specified $website. This image will be used in all of the methods that follow, and so must be called first.

$website The url of the image you would like to work with.
Returns True if successful, false if not.
Example
$image->url ('http://www.somesite.com/images/copy.jpg');

string type ( )

This is so that you can know what you are working with.

Returns Either '.jpg', '.gif', or '.png'.
Example
$type = $image->type();

bool convert ( string $type )

If the image is a gif and you would like to covert it to a jpg for instance, you can do that here.

$type Either 'jpg', 'gif', or 'png'.
Returns True if successful, false if not.
Example
$image->convert ('jpg');

bool resize ( integer $width [, integer $height ] )

This will resize the original image within the width and height specified while still maintaining its original proportions. You can call this method as many times as you like, and you will always be working with the original image.

$width The maximum width (in pixels) you would like this image to be.
$height The maximum height (in pixels) you would like this image to be. This value defaults to 80% of whatever width is specified.
Returns True if successful, false if not.
Example
$image->resize (500); // Takes the users massive 10 Megapixel image, and cuts it down to size for the web.

bool square ( integer $pixels )

This method will generate a square image of $pixels size.

$pixels The size (in pixels) you would like the square to be.
Returns True if successful, false if not.
Example
$image->square (80);

bool save ( string $location [, integer $quality= 80 ] )

This will save an image to the location on your server.

$location Where you would like the image to reside on your server. Be sure to include the full directory path.
$quality The quality of the image created from 0 (worst quality, smallest file size) to 100 (best quality, biggest file size). Default is 80.
Returns True if successful, false if not.
Example
$image->save ('/base/directory/mysite.com/images/copied.jpg');

bool display ( [ integer $quality= 80 ] )

This method outputs the image directly to the browser.

$quality The quality of the image created from 0 (worst quality, smallest file size) to 100 (best quality, biggest file size). Default is 80.
Returns True if successful, false if not.
Example
$image->display (80);
Click to Download the PHP Image Class

 Subscribe to our feed

Image.php

<?php 
 
/* 
 *    author:           Kyle Gadd 
 *    documentation:    http://www.php-ease.com/classes/image.html 
 * 
 *    This program is free software: you can redistribute it and/or modify 
 *    it under the terms of the GNU General Public License as published by 
 *    the Free Software Foundation, either version 3 of the License, or 
 *    (at your option) any later version. 
 * 
 *    This program is distributed in the hope that it will be useful, 
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *    GNU General Public License for more details. 
 * 
 *    You should have received a copy of the GNU General Public License 
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */ 
 
class Image { 
 
  private $data = false; // the original - never changes 
  private $width; 
  private $height; 
  private $type; 
  private $image; // created and updated in $this->resize() 
 
  public function url ($website) { 
    $website = str_replace(' ', '%20', trim($website)); 
    if ($info = @getimagesize($website)) { 
       switch ($info[2]) { 
        case 1: $this->type = 'gif'; break; 
        case 2: $this->type = 'jpg'; break; 
        case 3: $this->type = 'png'; break; 
        default: return false; break; // image type not supported 
       } 
      $this->width = $info[0]; 
      $this->height = $info[1]; 
      $data = file_get_contents ($website); 
      if ($data !== FALSE) { 
        $this->data = $data; 
        return true; 
      } 
    } 
    return false; 
  } 
 
  public function type () { 
    $type = ''; 
    if (!empty($this->type)) $type = '.' . $this->type; 
    return $type; 
  } 
 
  public function convert ($type) { 
    if (in_array($type, array('jpg', 'gif', 'png'))) { 
      $this->type = $type; 
      return $this->resize(); 
    } 
    return false; 
  } 
 
  public function resize ($width='', $height='') { // constrains proportions within $width and $height 
    if ($this->data === FALSE) return false; 
    if (empty($width)) { 
      $width = $this->width; 
      $height = $this->height; 
    } else { 
      list ($width, $height) = $this->proportional ($width, $height); 
    } 
    $this->image = ''; // to clean the slate if necessary 
    $this->image = @imagecreatefromstring($this->data); 
    $image = imagecreatetruecolor ($width, $height); 
    if ($this->type == 'png') { 
      imagealphablending ($image, false); 
      imagesavealpha ($image, true); 
      $transparent = imagecolorallocatealpha ($image, 255, 255, 255, 127); 
      imagefilledrectangle ($image, 0, 0, $width, $height, $transparent); 
    } else { 
      $white = imagecolorallocate ($image, 255, 255, 255); 
      imagefilledrectangle ($image, 0, 0, $width, $height, $white); 
    } 
    imagecopyresampled ($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height); 
    $this->image = $image; 
    return true; 
  } 
 
 
  public function square ($pixels) { // generates a square image of $pixels size 
    if ($this->data === FALSE) return false; 
    $width = $height = $pixels; 
    $x = $y = 0; 
    $size = $this->width; 
    if ($this->height > $this->width) { 
      $y = round(($this->height - $this->width) / 2); 
    } elseif ($this->width > $this->height) { 
      $x = round(($this->width - $this->height) / 2); 
      $size = $this->height; 
    } 
    $this->image = ''; // to clean the slate if necessary 
    $this->image = @imagecreatefromstring($this->data); 
    $image = imagecreatetruecolor ($width, $height); 
    if ($this->type == 'png') { 
      imagealphablending ($image, false); 
      imagesavealpha ($image, true); 
      $transparent = imagecolorallocatealpha ($image, 255, 255, 255, 127); 
      imagefilledrectangle ($image, 0, 0, $width, $height, $transparent); 
    } else { 
      $white = imagecolorallocate ($image, 255, 255, 255); 
      imagefilledrectangle ($image, 0, 0, $width, $height, $white); 
    } 
    imagecopyresampled ($image, $this->image, 0, 0, $x, $y, $width, $height, $size, $size); 
    $this->image = $image; 
    return true; 
  } 
 
  public function save ($location, $quality=80) { 
    if (!is_dir(dirname($location))) mkdir(dirname($location), 0755, true); 
    if (!empty($this->image)) { 
      if ($this->type == 'jpg') { 
        return imagejpeg ($this->image, $location, $quality); 
      } elseif ($this->type == 'gif') { 
        return imagegif ($this->image, $location); 
      } elseif ($this->type == 'png') { 
        if ($quality >= 90) { 
          $quality = 0; 
        } else { 
          $quality = abs(round($quality / 10) - 9); 
        } 
        return imagepng ($this->image, $location, $quality); 
      } 
    } elseif (!empty($this->data)) { 
      if (file_put_contents ($location, $this->data)) return true; 
    } 
    return false; 
  } 
 
  public function display ($quality=80) { 
    if (!empty($this->image)) { 
      if ($this->type == 'jpg') { 
        header ('Content-type: image/jpeg'); 
        return imagejpeg ($this->image, NULL, $quality); 
      } elseif ($this->type == 'gif') { 
        header ('Content-type: image/gif'); 
        return imagegif ($this->image); 
      } elseif ($this->type == 'png') { 
        if ($quality >= 90) { 
          $quality = 0; 
        } else { 
          $quality = abs(round($quality / 10) - 9); 
        } 
        header ('Content-type: image/png'); 
        return imagepng ($this->image, NULL, $quality); 
      } 
    } elseif (!empty($this->data)) { 
      switch ($this->type) { 
        case 'jpg': header ('Content-type: image/jpeg'); break; 
        case 'gif': header ('Content-type: image/gif'); break; 
        case 'png': header ('Content-type: image/png'); break; 
      } 
      echo $this->data; 
      return true; 
    } 
    return false; 
  } 
 
  private function proportional ($width, $height) { 
    if (empty($height)) $height = $width * .8; 
    $use = (($width / $height) <= ($this->width / $this->height)) ? 'width' : 'height'; 
    $max_dimension = ($use == 'width') ? $width : $height; 
    $ratio = ($this->width >= $this->height) ? $max_dimension / $this->width : $max_dimension / $this->height; 
    if($this->width > $max_dimension || $this->height > $max_dimension) {  
      $width = round($this->width * $ratio);  
      $height = round($this->height * $ratio);  
    } else { // the resize is larger than the original 
      $width = round($this->width);  
      $height = round($this->height); 
    } 
    return array ($width, $height);  
  } 
 
} 
 
?>

comments powered by Disqus
Copyright © 2011 PHP-Ease.com - PHP Made Easy