Wednesday, January 24, 2018

PHP Image class with data_uri, resize, crop, watermark, scale, fitTosize



PHP Image manipulation class almost every options in this class.

Following features are included in this PHP class. 

  • data_uri
  • getWidth
  • getHeight
  • resizeToHeight
  • resizeToWidth
  • scale
  • crop
  • resize
  • addWatermark
  • fitToSize



<?php


/*
Written by: Jailani M, http://www.W3Lessons.com
w3_image Image Manipulation Class 
*/
class w3_image {

   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=100, $permissions=null) {
    

imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
imagealphablending($this->image, false);
imagesavealpha($this->image, true);

    
      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) {
  
  imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
  imagealphablending($this->image, false);
  imagesavealpha($this->image, true);  
    
  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 data_uri($image_type=IMAGETYPE_JPEG) {
  
  imagecolortransparent($this->image, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
  imagealphablending($this->image, false);
  imagesavealpha($this->image, true);  
   ob_start();
  if( $image_type == IMAGETYPE_JPEG ) {
  $mime="image/jpeg";
  imagejpeg($this->image);
  } elseif( $image_type == IMAGETYPE_GIF ) {
  $mime="image/gif";   
  imagegif($this->image);         
  } elseif( $image_type == IMAGETYPE_PNG ) {
  $mime="image/png";   
  imagepng($this->image);
  }
  $image_data= ob_get_clean();
  $base64=base64_encode($image_data);
  return "data:$mime;base64,$base64";
 } 
   
   
   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);
  imagealphablending($new_image, false);
  imagesavealpha($new_image, true);   
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
     $this->image = $new_image;   
   }  
   
   function crop($left,$top,$width,$height)
   {
 // create a blank image having the same width and height as the crop area
    // this will be our cropped image
    $cropped_image = imagecreatetruecolor($width, $height);
 $bgwhite = imagecolorallocatealpha($cropped_image,255,255,255,0);
 imagefilledrectangle($cropped_image, 0, 0,$width,$height, $bgwhite);
    // copy the crop area from the source image to the blank image created above
    imagecopy($cropped_image, $this->image, 0, 0, $left, $top,$width, $height);
  $this->image = $cropped_image;
   }
   
   function addWatermark($watermark_image,$position="B_RIGHT")
   {
    // T_LEFT, T_RIGHT, B_LEFT, B_RIGHT
    
  ImageAlphaBlending($this->image, true); 
  
  // Same Size PNG Watermark Image with Transparency
  $logoImage = ImageCreateFromPNG($watermark_image); 
  $logoW = ImageSX($logoImage); 
  $logoH = ImageSY($logoImage); 
  
  if($position == "T_LEFT")
  {
  $dst_x = 0;
  $dst_y = 0;
  }elseif($position == "T_RIGHT")
  {
  $dst_x = $this->getWidth()-$logoW;
  $dst_y = 0;
  }elseif($position == "B_LEFT")
  {
  $dst_x = 0;
  $dst_y = $this->getHeight()-$logoH;   
  }elseif($position == "B_RIGHT")
  {
  $dst_x = $this->getWidth()-$logoW;
  $dst_y = $this->getHeight()-$logoH;
  }
  
  
  // were you see the two 1's this is the offset from the top-left hand corner!
  ImageCopy($this->image, $logoImage, $dst_x, $dst_y, 0, 0, $logoW, $logoH);  
   }
   
   function fitToSize($width,$height)
   {
 // create a blank image having the same width and height fitToSize
    // this will be our final image
    $fitToImage = imagecreatetruecolor($width, $height);
 $bgwhite = imagecolorallocatealpha($fitToImage,255,255,255,0);
 imagefilledrectangle($fitToImage, 0, 0,$width,$height, $bgwhite);
    // resize image based on fit to size in both height and width
 $this->resizeToWidth($width);
 
 if($this->getHeight() > $height)
 $this->resizeToHeight($height); 
 
 $_x = ($width - $this->getWidth())/2;
 $_w = $this->getWidth();
 
 $_y = ($height - $this->getHeight()) / 2; 
 $_h = $this->getHeight();
 
 imagecopy($fitToImage,$this->image,$_x,$_y,0,0,$_w,$_h);
  $this->image = $fitToImage;
   }
}



Tuesday, January 23, 2018

Simple Two way Encryption and Decryption Function PHP



Hello guys, after a long time, I came back to my blog with new flavor of articles, and I am plan to concentrate my blog half my working day here after. So my followers can expect valuable development ideas from me through comment and feed.

Here we go our come back article, Here now I teach you the Two way encryption and decryption method with full explanation.

Objective Build a function for Two Way Encryption and Decryption:

            Encrypt as well as Decrypt our given string with our own Encrypt Decrypt Key and Initiative Vector (IV) (?), with Advanced Encryption Standard (AES) (?) this method is highly secure method all time ever with few PHP functions to build Two way Encryption and Decryption.

Functions Involved in Two Day Encryption and Decryption.

hash - Generate a hash value (message digest)
string hash ( string $algo , string $data [, bool $raw_output = false ] )

substr - Return part of a string
            string substr ( string $string , int $start [, int $length ] )

base64_encode - Encodes data with MIME base64
string base64_encode ( string $data )

base64_decode - Decodes data encoded with MIME base64
            string base64_decode ( string $data [, bool $strict = false ] )

openssl_encrypt - Encrypts data
string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )

openssl_decrypt - Decrypts data
            string openssl_decrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] ) 

Two Encryption & Decryption Function

/*
Written by: Jailani M, http://www.W3Lessons.com
Defineds:
ED_KEY - Encryption and Decription key your choice
IV_KEY - INITIATIVE VECTOR your choice
*/
define("ED_KEY","your_key");
define("IV_KEY","your_iv");

function w3_encode( $string) {
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $encoded_string = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
    return $encoded_string;
}

function w3_decode($string)
{
    $encrypt_method = "AES-256-CBC";
    $key = hash( 'md5', ED_KEY );
    $iv = substr( hash( 'md5', IV_KEY ), 0, 16 );
    $decoded_string = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
    return $decoded_string;
}



Download This Script     Live Demo     Download Script