/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class image { private $supported, $edit, $root, $image; public function __construct( $root ) { //Check if we're working with an actual //Directory { //Fill in the array of the things we can do 'negative' => IMG_FILTER_NEGATE, 'greyscale' => IMG_FILTER_GRAYSCALE, 'brightness'=> IMG_FILTER_BRIGHTNESS, 'contract' => IMG_FILTER_CONTRAST, 'colorize' => IMG_FILTER_COLORIZE, 'edge' => IMG_FILTER_EDGEDETECT, 'emboss' => IMG_FILTER_EMBOSS, 'guassion' => IMG_FILTER_GAUSSIAN_BLUR, 'blur' => IMG_FILTER_SELECTIVE_BLUR, 'sketch' => IMG_FILTER_MEAN_REMOVAL, 'smooth' => IMG_FILTER_SMOOTH ); //Fill in an array of the image formats we support 'jpeg' => 'jpeg', 'jpg' => 'jpeg', 'png' => 'png', 'gif' => 'gif' ); //Finally fill in the root and enforce trailing slash } else { throw new Exception("[Class Of: Image] Provided root directory does not exists"); } } public function set( $img ) { //See if our image file exists and it's supprted { { //Set all neccesary information $function = 'imagecreatefrom' . $this->supported[ $type ]; $this->image['file'] = $img; $this->image['oResource'] = $function( $this->root . $img ); $this->image['type'] = $this->supported[ $type ]; ///Below is error reprting } else { throw new Exception("[Class Of: Image] Image file is not currently supported"); } } else { throw new Exception("[Class Of: Image] Image file does not exists relative to root"); } return $this; } public function execute( $filename = '' ) { //Make a filename of one is not provided if( $filename == '' ) { $filename = '_edited_' . $this->image['file']; } //Create image $function = 'image' . $this->image['type']; $function( $this->image['oResource'], $this->root . $filename, 100); //Clean cache } public function effect() { //gather up all our arguments //Check if I support what they want done { //Create priliminary argument array //Now all our image filter function return $this; ///Error reporting } else { throw new Exception("[Class Of: Gallery] Effect not supported"); } } public function ratio( $maxWidth ) { //Calculate the max height and send off to resize function $maxHeight = ($maxWidth * $this->image['height']) / $this->image['width']; $this->resize( $maxWidth, $maxHeight ); } public function resize( $width, $height = '' ) { { //Assume width is a percentage value now //Check for .20 || .2 bug $num = ( $num > 10 ) ? '.' . $num : '.0' . $num; $width = $this->image['width'] - ($this->image['width'] * $num); $height = $this->image['height'] - ($this->image['height'] * $num); } //Create a temporary pallete imagecopyresampled( $img, $this->image['oResource'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height']); //Fill in the original with the new $this->image['oResource'] = $img; $this->image['width'] = $width; $this->image['height'] = $height; return $this; } public function flip( $dir ) { //Check is we support basically switch( $dir ) { //The process for flipping either left and right //Deviates by the subtraction of one variable case $dir == 'left' || $dir == 'right': { //Create a pallete //Begin traversing the image plane at the left uppermost corner //travel down to the right down corner for( $y = $this->image['height']; $y >= 0; --$y ) { for( $x = 0; $x <= $this->image['width']; ++$x ) { //Extract color index //Determine side so we can check which variable to subtract from if( $dir == 'left' ) { } else { } } } //set new height and width break; } //Down is rather simple... case $dir == 'down' : { } } //set new image resource $this->image['oResource'] = $img; return $this; } public function crop( $oX, $oY, $nX, $nY ) { //We must determine the number by which to //incrementing variables as well as the length //and height of the new image if( $oX > $nX ) { $xBegin = $nX; $xLen = $oX - $nX; } else { $xBegin = $oX; $xLen = $nX - $oX; } if( $oY > $nY ) { $yBegin = $nY; $yLen = $oY - $nY; } else { $yBegin = $oY; $yLen = $nY - $oY; } //Create pallete //Begin incrementing at 0 as if traversing //the new image instead of the old for( $y = 0; $y <= $yLen; ++$y ) { for( $x = 0; $x <= $xLen; ++$x ) { //Adding our pretermined value takes us the pixel //location we wanted to go to } } //Set neccesary information $this->image['oResource'] = $img; return $this; } } ?>