Pages

Wednesday, March 6, 2013

SimpleImage.php


<?php

ini_set('memory_limit', '128M');

class SimpleImage {
 
   var $image;
   var $image_type;

   function load($filename) {
 
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=85, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);        
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }  
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);        
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }  
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }
   function resize($width,$height) {
 
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;  
   }    
}



function fw_get_dimension_new($src, $width = '', $height = '')
{

    // Convert width/height to int for proper validation.
    // intval() used to support compatibility with plugins like image-handler
    $width = empty($width) ? $width : intval($width);
    $height = empty($height) ? $height : intval($height);

// alt is added to the img tag even if it is null to prevent browsers from outputting
// the image filename as default

    if ($width != '' && $height != '' and file_exists($src))
{

 $image_size = @getimagesize($src);

 $ratio = ($image_size[0] != 0 ? $width / $image_size[0] : 1);
 if ($image_size[1]*$ratio > $height) {
$ratio = $height / $image_size[1];
$width = $image_size[0] * $ratio;
 } else {
$height = $image_size[1] * $ratio;
 }
// only use proportional image when image is larger than proportional size
 if ($image_size[0] < $width and $image_size[1] < $height) {
$width=$image_size[0];
$height=intval($image_size[1]);
 } else {
$width=round($width);
$height=round($height);
 }
    }

    $output['width'] = $width;
    $output['height'] = $height;

    return $output;
  }
 

/* how to use example by harikrishna
$file = "../images/save.png";
$dimensions = fw_get_dimension_new($file,'10','10');

$image = new SimpleImage();

$image->load($file);
$image->resize($dimensions['width'],$dimensions['height']);
$image->save($file);
*/
?>

No comments: