2015-01-27 22:40:28 +01:00
< ? php
2015-12-07 16:02:17 +01:00
$config = array ( 'mode' => 'strict' , ); class CHttpGet { private $request = array (); private $response = array (); public function __construct () { $this -> request [ 'header' ] = array (); } public function buildUrl ( $baseUrl , $merge ) { $parts = parse_url ( $baseUrl ); $parts = array_merge ( $parts , $merge ); $url = $parts [ 'scheme' ]; $url .= " :// " ; $url .= $parts [ 'host' ]; $url .= isset ( $parts [ 'port' ]) ? " : " . $parts [ 'port' ] : " " ; $url .= $parts [ 'path' ]; return $url ; } public function setUrl ( $url ) { $parts = parse_url ( $url ); $path = " " ; if ( isset ( $parts [ 'path' ])) { $pathParts = explode ( '/' , $parts [ 'path' ]); unset ( $pathParts [ 0 ]); foreach ( $pathParts as $value ) { $path .= " / " . rawurlencode ( $value ); } } $url = $this -> buildUrl ( $url , array ( " path " => $path )); $this -> request [ 'url' ] = $url ; return $this ; } public function setHeader ( $field , $value ) { $this -> request [ 'header' ][] = " $field : $value " ; return $this ; } public function parseHeader () { $rawHeaders = rtrim ( $this -> response [ 'headerRaw' ], " \r \n " ); $headerGroups = explode ( " \r \n \r \n " , $rawHeaders ); $header = explode ( " \r \n " , end ( $headerGroups )); $output = array (); if ( 'HTTP' === substr ( $header [ 0 ], 0 , 4 )) { list ( $output [ 'version' ], $output [ 'status' ]) = explode ( ' ' , $header [ 0 ]); unset ( $header [ 0 ]); } foreach ( $header as $entry ) { $pos = strpos ( $entry , ':' ); $output [ trim ( substr ( $entry , 0 , $pos ))] = trim ( substr ( $entry , $pos + 1 )); } $this -> response [ 'header' ] = $output ; return $this ; } public function doGet ( $debug = false ) { $options = array ( CURLOPT_URL => $this -> request [ 'url' ], CURLOPT_HEADER => 1 , CURLOPT_HTTPHEADER => $this -> request [ 'header' ], CURLOPT_AUTOREFERER => true , CURLOPT_RETURNTRANSFER => true , CURLINFO_HEADER_OUT => $debug , CURLOPT_CONNECTTIMEOUT => 5 , CURLOPT_TIMEOUT => 5 , CURLOPT_FOLLOWLOCATION => true , CURLOPT_MAXREDIRS => 2 , ); $ch = curl_init (); curl_setopt_array ( $ch , $options ); $response = curl_exec ( $ch ); if ( ! $response ) { throw new Exception ( " Failed retrieving url, details follows: " . curl_error ( $ch )); } $headerSize = curl_getinfo ( $ch , CURLINFO_HEADER_SIZE ); $this -> response [ 'headerRaw' ] = substr ( $response , 0 , $headerSize ); $this -> response [ 'body' ] = substr ( $response , $headerSize ); $this -> parseHeader (); if ( $debug ) { $info = curl_getinfo ( $ch ); echo " Request header<br><pre> " , var_dump ( $info [ 'request_header' ]), " </pre> " ; echo " Response header (raw)<br><pre> " , var_dump ( $this -> response [ 'headerRaw' ]), " </pre> " ; echo " Response header (parsed)<br><pre> " , var_dump ( $this -> response [ 'header' ]), " </pre> " ; } curl_close ( $ch ); return true ; } public function getStatus () { return isset ( $this -> response [ 'header' ][ 'status' ]) ? ( int ) $this -> response [ 'header' ][ 'status' ] : null ; } public function getLastModified () { return isset ( $this -> response [ 'header' ][ 'Last-Modified' ]) ? strtotime ( $this -> response [ 'header' ][ 'Last-Modified' ]) : null ; } public function getContentType () { $type = isset ( $this -> response [ 'header' ][ 'Content-Type' ]) ? $this -> response [ 'header' ][ 'Content-Type' ] : null ; return preg_match ( '#[a-z]+/[a-z]+#' , $type ) ? $type : null ; } public function getDate ( $default = false ) { return isset ( $this -> response [ 'header' ][ 'Date' ]) ? strtotime ( $this -> response [ 'header' ][ 'Date' ]) : $default ; } public function getMaxAge ( $default = false ) { $cacheControl = isset ( $this -> response [ 'header' ][ 'Cache-Control' ]) ? $this -> response [ 'header' ][ 'Cache-Control' ] : null ; $maxAge = null ; if ( $cacheControl ) { $part = explode ( '=' , $cacheControl ); $maxAge = ( $part [ 0 ] == " max-age " ) ? ( int ) $part [ 1 ] : null ; } if ( $maxAge ) { return $maxAge ; } $expire = isset ( $this -> response [ 'header' ][ 'Expires' ]) ? strtotime ( $this -> response [ 'header' ][ 'Expires' ]) : null ; return $expire ? $expire : $default ; } public function getBody () { return $this -> response [ 'body' ]; } } class CRemoteImage { private $saveFolder = null ; private $useCache = true ; private $http ; private $status ; private $defaultMaxAge = 604800 ; private $url ; private $fileName ; private $fileJson ; private $cache ; public function getStatus () { return $this -> status ; } public function getDetails () { return $this -> cache ; } public function setCache ( $path ) { $this -> saveFolder = rtrim ( $pa
http :// php . net / manual / en / function . imagefilter . php ' ); } } $args[' filters '][$key] = $filter; } } $args = array_merge($defaults, $args); foreach ($defaults as $key => $val) { $this->{$key} = $args[$key]; } if ($this->bgColor) { $this->setDefaultBackgroundColor($this->bgColor); } $this->newWidthOrig = $this->newWidth; $this->newHeightOrig = $this->newHeight; $this->cropOrig = $this->crop; return $this; } private function mapFilter($name) { $map = array( ' negate ' => array(' id '=>0, ' argc '=>0, ' type '=>IMG_FILTER_NEGATE), ' grayscale ' => array(' id '=>1, ' argc '=>0, ' type '=>IMG_FILTER_GRAYSCALE), ' brightness ' => array(' id '=>2, ' argc '=>1, ' type '=>IMG_FILTER_BRIGHTNESS), ' contrast ' => array(' id '=>3, ' argc '=>1, ' type '=>IMG_FILTER_CONTRAST), ' colorize ' => array(' id '=>4, ' argc '=>4, ' type '=>IMG_FILTER_COLORIZE), ' edgedetect ' => array(' id '=>5, ' argc '=>0, ' type '=>IMG_FILTER_EDGEDETECT), ' emboss ' => array(' id '=>6, ' argc '=>0, ' type '=>IMG_FILTER_EMBOSS), ' gaussian_blur ' => array(' id '=>7, ' argc '=>0, ' type '=>IMG_FILTER_GAUSSIAN_BLUR), ' selective_blur ' => array(' id '=>8, ' argc '=>0, ' type '=>IMG_FILTER_SELECTIVE_BLUR), ' mean_removal ' => array(' id '=>9, ' argc '=>0, ' type '=>IMG_FILTER_MEAN_REMOVAL), ' smooth ' => array(' id '=>10, ' argc '=>1, ' type '=>IMG_FILTER_SMOOTH), ' pixelate ' => array(' id '=>11, ' argc '=>2, ' type '=>IMG_FILTER_PIXELATE), ); if (isset($map[$name])) { return $map[$name]; } else { throw new Exception(' No such filter . '); } } public function loadImageDetails($file = null) { $file = $file ? $file : $this->pathToImage; is_readable($file) or $this->raiseError(' Image file does not exist . '); $info = list($this->width, $this->height, $this->fileType, $this->attr) = getimagesize($file); if (empty($info)) { throw new Exception("The file doesn' t seem to be a valid image . " ); } if ( $this->verbose ) { $this->log ( " Loading image details for : { $file } " ); $this->log ( " Image width x height ( type ) : { $this -> width } x { $this -> height } ({ $this -> fileType }) . " ); $this->log ( " Image filesize : " . filesize( $file ) . " bytes . " ); $this->log ( " Image mimetype : " . image_type_to_mime_type( $this->fileType )); } return $this ; } public function initDimensions() { $this->log ( " Init dimension ( before ) newWidth x newHeight is { $this -> newWidth } x { $this -> newHeight } . " ); if ( $this->newWidth [strlen( $this->newWidth )-1] == '%') { $this->newWidth = $this->width * substr( $this->newWidth , 0, -1) / 100; $this->log ( " Setting new width based on % to { $this -> newWidth } " ); } if ( $this->newHeight [strlen( $this->newHeight )-1] == '%') { $this->newHeight = $this->height * substr( $this->newHeight , 0, -1) / 100; $this->log ( " Setting new height based on % to { $this -> newHeight } " ); } is_null( $this->aspectRatio ) or is_numeric( $this->aspectRatio ) or $this->raiseError ('Aspect ratio out of range'); if ( $this->aspectRatio && is_null( $this->newWidth ) && is_null( $this->newHeight )) { if ( $this->aspectRatio >= 1) { $this->newWidth = $this->width ; $this->newHeight = $this->width / $this->aspectRatio ; $this->log ( " Setting new width & height based on width & aspect ratio ( >= 1 ) to ( w x h ) { $this -> newWidth } x { $this -> newHeight } " ); } else { $this->newHeight = $this->height ; $this->newWidth = $this->height * $this->aspectRatio ; $this->log ( " Setting new width & height based on width & aspect ratio ( < 1 ) to ( w x h ) { $this -> newWidth } x { $this -> newHeight } " ); } } elseif ( $this->aspectRatio && is_null( $this->newWidth )) { $this->newWidth = $this->newHeight * $this->aspectRatio ; $this->log ( " Setting new width based on aspect ratio to { $this -> newWidth } " ); } elseif ( $this->aspectRatio && is_null( $this->newHeight )) { $this->newHeight = $this->newWidth / $this->aspectRatio ; $this->log ( " Setting new height based on aspect ratio to { $this -> newHeight } " ); } if ( $this->dpr != 1) { if (!is_null( $this->newWidth )) { $this->newWidth = round( $this->newWidth * $this->dpr ); $this->log ( " Setting new width based on dpr = { $this -> dpr } - w = { $this -> newWidth } " ); } if (!is_null( $this->newHeight )) { $this->newHeight = round( $this->newHeight * $this->dpr ); $this->log ( " Setting new height based on dpr = { $this -> dpr } - h = { $this -> newHeight } " ); } }
2015-10-21 00:13:56 +02:00
11 float values . Got $expression . " ); } array_walk( $part , function ( $item , $key ) { if (!is_numeric( $item )) { throw new Exception( " Argument to convolve expression should be float but is not . " ); } }); return array( array( array( $part[0] , $part[1] , $part[2] ), array( $part[3] , $part[4] , $part[5] ), array( $part[6] , $part[7] , $part[8] ), ), $part[9] , $part[10] , ); } public function addConvolveExpressions( $options ) { $this->convolves = array_merge( $this->convolves , $options ); return $this ; } public function imageConvolution( $options = null) { $options = $options ? $options : $this->convolve ; $this->log ( " Convolution with '$options' " ); $options = explode( " : " , $options ); foreach ( $options as $option ) { list( $matrix , $divisor , $offset ) = $this->createConvolveArguments ( $option ); imageconvolution( $this->image , $matrix , $divisor , $offset ); } return $this ; } public function setDefaultBackgroundColor( $color ) { $this->log ( " Setting default background color to '$color' . " ); if (!(strlen( $color ) == 6 || strlen( $color ) == 8)) { throw new Exception( " Background color needs a hex value of 6 or 8
2015-01-27 22:40:28 +01:00
digits . 000000 - FFFFFF or 00000000 - FFFFFF7F .
2015-10-21 00:13:56 +02:00
Current value was : '$color' . " ); } $red = hexdec(substr( $color , 0, 2)); $green = hexdec(substr( $color , 2, 2)); $blue = hexdec(substr( $color , 4, 2)); $alpha = (strlen( $color ) == 8) ? hexdec(substr( $color , 6, 2)) : null; if (( $red < 0 || $red > 255) || ( $green < 0 || $green > 255) || ( $blue < 0 || $blue > 255) || ( $alpha < 0 || $alpha > 127) ) { throw new Exception( " Background color out of range . Red , green blue
2015-01-27 22:40:28 +01:00
should be 00 - FF and alpha should be 00 - 7 F .
2015-12-07 16:02:17 +01:00
Current value was : '$color' . " ); } $this->bgColor = strtolower( $color ); $this->bgColorDefault = array( 'red' => $red , 'green' => $green , 'blue' => $blue , 'alpha' => $alpha ); return $this ; } private function getBackgroundColor( $img = null) { $img = isset( $img ) ? $img : $this->image ; if ( $this->bgColorDefault ) { $red = $this->bgColorDefault ['red']; $green = $this->bgColorDefault ['green']; $blue = $this->bgColorDefault ['blue']; $alpha = $this->bgColorDefault ['alpha']; if ( $alpha ) { $color = imagecolorallocatealpha( $img , $red , $green , $blue , $alpha ); } else { $color = imagecolorallocate( $img , $red , $green , $blue ); } return $color ; } else { return 0; } } private function createImageKeepTransparency( $width , $height ) { $this->log ( " Creating a new working image width = { $width } px , height = { $height } px . " ); $img = imagecreatetruecolor( $width , $height ); imagealphablending( $img , false); imagesavealpha( $img , true); $index = $this->image ? imagecolortransparent( $this->image ) : -1; if ( $index != -1) { imagealphablending( $img , true); $transparent = imagecolorsforindex( $this->image , $index ); $color = imagecolorallocatealpha( $img , $transparent['red'] , $transparent['green'] , $transparent['blue'] , $transparent['alpha'] ); imagefill( $img , 0, 0, $color ); $index = imagecolortransparent( $img , $color ); $this->Log ( " Detected transparent color = " . implode( " , " , $transparent ) . " at index = $index " ); } elseif ( $this->bgColorDefault ) { $color = $this->getBackgroundColor ( $img ); imagefill( $img , 0, 0, $color ); $this->Log ( " Filling image with background color . " ); } return $img ; } public function setPostProcessingOptions( $options ) { if (isset( $options['jpeg_optimize'] ) && $options['jpeg_optimize'] ) { $this->jpegOptimizeCmd = $options['jpeg_optimize_cmd'] ; } else { $this->jpegOptimizeCmd = null; } if (isset( $options['png_filter'] ) && $options['png_filter'] ) { $this->pngFilterCmd = $options['png_filter_cmd'] ; } else { $this->pngFilterCmd = null; } if (isset( $options['png_deflate'] ) && $options['png_deflate'] ) { $this->pngDeflateCmd = $options['png_deflate_cmd'] ; } else { $this->pngDeflateCmd = null; } return $this ; } protected function getTargetImageExtension() { if (isset( $this->extension )) { return strtolower( $this->extension ); } else { return substr(image_type_to_extension( $this->fileType ), 1); } } public function save( $src = null, $base = null, $overwrite = true) { if (isset( $src )) { $this->setTarget ( $src , $base ); } if ( $overwrite === false && is_file( $this->cacheFileName )) { $this->Log ( " Not overwriting file since its already exists and \ $overwrite if false . " ); return; } is_writable( $this->saveFolder ) or $this->raiseError ('Target directory is not writable.'); $type = $this->getTargetImageExtension (); $this->Log ( " Saving image as " . $type ); switch( $type ) { case 'jpeg': case 'jpg': $this->Log ( " Saving image as JPEG to cache using quality = { $this -> quality } . " ); imagejpeg( $this->image , $this->cacheFileName , $this->quality ); if ( $this->jpegOptimizeCmd ) { if ( $this->verbose ) { clearstatcache(); $this->log ( " Filesize before optimize : " . filesize( $this->cacheFileName ) . " bytes . " ); } $res = array(); $cmd = $this->jpegOptimizeCmd . " - outfile $this -> cacheFileName $this -> cacheFileName " ; exec( $cmd , $res ); $this->log ( $cmd ); $this->log ( $res ); } break; case 'gif': $this->Log ( " Saving image as GIF to cache . " ); imagegif( $this->image , $this->cacheFileName ); break; case 'png': default: $this->Log ( " Saving image as PNG to cache using compression = { $this -> compress } . " ); imagealphablending( $this->image , false); imagesavealpha( $this->image , true); imagepng( $this->image , $this->cacheFileName , $this->compress ); if ( $this->pngFilterCmd ) { if ( $this->verbose ) { clearstatcache(); $this->Log ( " Filesize before filter optimize : " . filesize( $this->cacheFileName ) . " bytes . " ); } $res = array(); $cmd = $this->pngFilterCmd . " $this -> cacheFileName " ; exec( $cmd , $res ); $this->Log ( $cmd ); $this->Log ( $res ); } if ( $this->pngDeflateCmd ) { if ( $this->verbose ) { clearstatcache(); $this->Log ( " Filesize before deflate optimize : " . filesize( $this->cacheFileName ) . " bytes . " ); } $res = arr
2015-01-27 22:40:28 +01:00
< h1 > CImage Verbose Output </ h1 >
< pre > { $log } </ pre >
EOD ;
2016-04-18 15:58:39 +02:00
} } private function raiseError ( $message ) { throw new Exception ( $message ); } } class CCache { private $path ; public function setDir ( $path ) { if ( ! is_dir ( $path )) { throw new Exception ( " Cachedir is not a directory. " ); } $this -> path = $path ; return $this ; } public function getPathToSubdir ( $subdir , $create = true ) { $path = realpath ( $this -> path . " / " . $subdir ); if ( is_dir ( $path )) { return $path ; } if ( $create && is_writable ( $this -> path )) { $path = $this -> path . " / " . $subdir ; if ( mkdir ( $path )) { return realpath ( $path ); } } return false ; } public function getStatusOfSubdir ( $subdir ) { $path = realpath ( $this -> path . " / " . $subdir ); $exists = is_dir ( $path ); $res = $exists ? " exists " : " does not exist " ; if ( $exists ) { $res .= is_writable ( $path ) ? " , writable " : " , not writable " ; } return $res ; } public function removeSubdir ( $subdir ) { $path = realpath ( $this -> path . " / " . $subdir ); if ( is_dir ( $path )) { return rmdir ( $path ); } return null ; } } $version = " v0.7.11 (2016-04-18) " ; define ( " CIMAGE_USER_AGENT " , " CImage/ $version " ); function errorPage ( $msg , $type = 500 ) { global $mode ; switch ( $type ) { case 403 : $header = " 403 Forbidden " ; break ; case 404 : $header = " 404 Not Found " ; break ; default : $header = " 500 Internal Server Error " ; } if ( $mode == " strict " ) { $header = " 404 Not Found " ; } header ( " HTTP/1.0 $header " ); if ( $mode == " development " ) { die ( " [img.php] $msg " ); } error_log ( " [img.php] $msg " ); die ( " HTTP/1.0 $header " ); } set_exception_handler ( function ( $exception ) { errorPage ( " <p><b>img.php: Uncaught exception:</b> <p> " . $exception -> getMessage () . " </p><pre> " . $exception -> getTraceAsString () . " </pre> " , 500 ); }); function get ( $key , $default = null ) { if ( is_array ( $key )) { foreach ( $key as $val ) { if ( isset ( $_GET [ $val ])) { return $_GET [ $val ]; } } } elseif ( isset ( $_GET [ $key ])) { return $_GET [ $key ]; } return $default ; } function getDefined ( $key , $defined , $undefined ) { return get ( $key ) === null ? $undefined : $defined ; } function getConfig ( $key , $default ) { global $config ; return isset ( $config [ $key ]) ? $config [ $key ] : $default ; } function verbose ( $msg = null ) { global $verbose , $verboseFile ; static $log = array (); if ( ! ( $verbose || $verboseFile )) { return ; } if ( is_null ( $msg )) { return $log ; } $log [] = $msg ; } $configFile = __DIR__ . '/' . basename ( __FILE__ , '.php' ) . '_config.php' ; if ( is_file ( $configFile )) { $config = require $configFile ; } elseif ( ! isset ( $config )) { $config = array (); } $verbose = getDefined ( array ( 'verbose' , 'v' ), true , false ); $verboseFile = getDefined ( 'vf' , true , false ); verbose ( " img.php version = $version " ); $status = getDefined ( 'status' , true , false ); $mode = getConfig ( 'mode' , 'production' ); set_time_limit ( 20 ); ini_set ( 'gd.jpeg_ignore_warning' , 1 ); if ( ! extension_loaded ( 'gd' )) { errorPage ( " Extension gd is not loaded. " , 500 ); } if ( $mode == 'strict' ) { error_reporting ( 0 ); ini_set ( 'display_errors' , 0 ); ini_set ( 'log_errors' , 1 ); $verbose = false ; $status = false ; $verboseFile = false ; } elseif ( $mode == 'production' ) { error_reporting ( - 1 ); ini_set ( 'display_errors' , 0 ); ini_set ( 'log_errors' , 1 ); $verbose = false ; $status = false ; $verboseFile = false ; } elseif ( $mode == 'development' ) { error_reporting ( - 1 ); ini_set ( 'display_errors' , 1 ); ini_set ( 'log_errors' , 0 ); $verboseFile = false ; } elseif ( $mode == 'test' ) { error_reporting ( - 1 ); ini_set ( 'display_errors' , 1 ); ini_set ( 'log_errors' , 0 ); } else { errorPage ( " Unknown mode: $mode " , 500 ); } verbose ( " mode = $mode " ); verbose ( " error log = " . ini_get ( 'error_log' )); $defaultTimezone = getConfig ( 'default_timezone' , null ); if ( $defaultTimezone ) { date_default_timezone_set ( $defaultTimezone ); } elseif ( ! ini_get ( 'default_timezone' )) { date_default_timezone_set ( 'UTC' ); } $pwdConfig = getConfig ( 'password' , false ); $pwdAlways = getConfig ( 'password_always' , false ); $pwdType = getConfig ( 'password_type' , 'text' ); $pwd = get ( array ( 'password' , 'pwd' ), null ); $passwordMatch = null ; if ( $pwd ) { switch ( $pwdType ) { case 'md5' : $passwordMatch = ( $pwdConfig === md5 ( $pwd )); break ; case 'hash' : $passwordMatch = password_verify ( $pwd , $pwdConfig ); break ; case 'text' : $passwordMatch = ( $pwdConfig ==
2016-04-01 10:27:33 +02:00
matching file exists on the filesystem . ', 404 ); } } if ($imagePathConstraint && !$dummyImage && !$remoteSource) { $imageDir = realpath($imagePath); substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0 or errorPage( ' Security constraint : Source image is not below the directory " image_path "
2016-04-18 15:58:39 +02:00
as specified in the config file img_config . php . ', 404 ); } verbose("src = $srcImage"); $sizeConstant = getConfig(' size_constant ', function () { $sizes = array( ' w1 ' => 613, ' w2 ' => 630, ); $gridColumnWidth = 30; $gridGutterWidth = 10; $gridColumns = 24; for ($i = 1; $i <= $gridColumns; $i++) { $sizes[' c ' . $i] = ($gridColumnWidth + $gridGutterWidth) * $i - $gridGutterWidth; } return $sizes; }); $sizes = call_user_func($sizeConstant); $newWidth = get(array(' width ', ' w ')); $maxWidth = getConfig(' max_width ', 2000); if (isset($sizes[$newWidth])) { $newWidth = $sizes[$newWidth]; } if ($newWidth[strlen($newWidth)-1] == ' % ') { is_numeric(substr($newWidth, 0, -1)) or errorPage(' Width % not numeric . ', 404); } else { is_null($newWidth) or ($newWidth > 10 && $newWidth <= $maxWidth) or errorPage(' Width out of range . ', 404); } verbose("new width = $newWidth"); $newHeight = get(array(' height ', ' h ')); $maxHeight = getConfig(' max_height ', 2000); if (isset($sizes[$newHeight])) { $newHeight = $sizes[$newHeight]; } if ($newHeight[strlen($newHeight)-1] == ' % ') { is_numeric(substr($newHeight, 0, -1)) or errorPage(' Height % out of range . ', 404); } else { is_null($newHeight) or ($newHeight > 10 && $newHeight <= $maxHeight) or errorPage(' Height out of range . ', 404); } verbose("new height = $newHeight"); $aspectRatio = get(array(' aspect - ratio ', ' ar ')); $aspectRatioConstant = getConfig(' aspect_ratio_constant ', function () { return array( ' 3 : 1 ' => 3/1, ' 3 : 2 ' => 3/2, ' 4 : 3 ' => 4/3, ' 8 : 5 ' => 8/5, ' 16 : 10 ' => 16/10, ' 16 : 9 ' => 16/9, ' golden ' => 1.618, ); }); $aspectRatios = call_user_func($aspectRatioConstant); $negateAspectRatio = ($aspectRatio[0] == ' ! ') ? true : false; $aspectRatio = $negateAspectRatio ? substr($aspectRatio, 1) : $aspectRatio; if (isset($aspectRatios[$aspectRatio])) { $aspectRatio = $aspectRatios[$aspectRatio]; } if ($negateAspectRatio) { $aspectRatio = 1 / $aspectRatio; } is_null($aspectRatio) or is_numeric($aspectRatio) or errorPage(' Aspect ratio out of range ', 404); verbose("aspect ratio = $aspectRatio"); $cropToFit = getDefined(array(' crop - to - fit ', ' cf '), true, false); verbose("crop to fit = $cropToFit"); $backgroundColor = getConfig(' background_color ', null); if ($backgroundColor) { $img->setDefaultBackgroundColor($backgroundColor); verbose("Using default background_color = $backgroundColor"); } $bgColor = get(array(' bgColor ', ' bg - color ', ' bgc '), null); verbose("bgColor = $bgColor"); $resizeStrategy = getDefined(array(' no - resample '), true, false); if ($resizeStrategy) { $img->setCopyResizeStrategy($img::RESIZE); verbose("Setting = Resize instead of resample"); } $fillToFit = get(array(' fill - to - fit ', ' ff '), null); verbose("fill-to-fit = $fillToFit"); if ($fillToFit !== null) { if (!empty($fillToFit)) { $bgColor = $fillToFit; verbose("fillToFit changed bgColor to = $bgColor"); } $fillToFit = true; verbose("fill-to-fit (fixed) = $fillToFit"); } $keepRatio = getDefined(array(' no - ratio ', ' nr ', ' stretch '), false, true); verbose("keep ratio = $keepRatio"); $crop = get(array(' crop ', ' c ')); verbose("crop = $crop"); $area = get(array(' area ', ' a ')); verbose("area = $area"); $useOriginal = getDefined(array(' skip - original ', ' so '), false, true); $useOriginalDefault = getConfig(' skip_original ', false); if ($useOriginalDefault === true) { verbose("skip original is default ON"); $useOriginal = false; } verbose("use original = $useOriginal"); $useCache = getDefined(array(' no - cache ', ' nc '), false, true); verbose("use cache = $useCache"); $quality = get(array(' quality ', ' q ')); $qualityDefault = getConfig(' jpg_quality ', null); is_null($quality) or ($quality > 0 and $quality <= 100) or errorPage(' Quality out of range ', 404); if (is_null($quality) && !is_null($qualityDefault)) { $quality = $qualityDefault; } verbose("quality = $quality"); $compress = get(array(' compress ', ' co ')); $compressDefault = getConfig(' png_compression ', null); is_null($compress) or ($compress > 0 and $compress <= 9) or errorPage(' Compress out of range ' , 404 ); if ( is_null ( $compress ) && ! is_null ( $compressDefault )) { $compress = $compressDefault ; } verbose ( " compress =
2015-10-21 00:13:56 +02:00
<! doctype html >
< html lang = en >
< meta charset = utf - 8 >
< title > CImage status </ title >
< pre > $text </ pre >
EOD ;
2015-10-21 01:13:36 +02:00
exit ; } if ( $verboseFile ) { $img -> setVerboseToFile ( " $cachePath /log.txt " ); } $hookBeforeCImage = getConfig ( 'hook_before_CImage' , null ); if ( is_callable ( $hookBeforeCImage )) { verbose ( " hookBeforeCImage activated " ); $allConfig = $hookBeforeCImage ( $img , array ( 'newWidth' => $newWidth , 'newHeight' => $newHeight , 'aspectRatio' => $aspectRatio , 'keepRatio' => $keepRatio , 'cropToFit' => $cropToFit , 'fillToFit' => $fillToFit , 'crop' => $crop , 'area' => $area , 'upscale' => $upscale , 'scale' => $scale , 'rotateBefore' => $rotateBefore , 'autoRotate' => $autoRotate , 'bgColor' => $bgColor , 'palette' => $palette , 'filters' => $filters , 'sharpen' => $sharpen , 'emboss' => $emboss , 'blur' => $blur , 'convolve' => $convolve , 'rotateAfter' => $rotateAfter , 'outputFormat' => $outputFormat , 'dpr' => $dpr , 'postProcessing' => $postProcessing , )); verbose ( print_r ( $allConfig , 1 )); extract ( $allConfig ); } if ( $verbose ) { $query = array (); parse_str ( $_SERVER [ 'QUERY_STRING' ], $query ); unset ( $query [ 'verbose' ]); unset ( $query [ 'v' ]); unset ( $query [ 'nocache' ]); unset ( $query [ 'nc' ]); unset ( $query [ 'json' ]); $url1 = '?' . htmlentities ( urldecode ( http_build_query ( $query ))); $url2 = '?' . urldecode ( http_build_query ( $query )); echo <<< EOD
2015-03-06 13:00:21 +01:00
<! doctype html >
< html lang = en >
< meta charset = utf - 8 >
< title > CImage verbose output </ title >
< style > body { background - color : #ddd}</style>
2015-01-27 22:40:28 +01:00
< a href = $url1 >< code > $url1 </ code ></ a >< br >
< img src = '{$url1}' />
< pre id = " json " ></ pre >
< script src = " https://code.jquery.com/jquery-2.1.1.min.js " ></ script >
< script type = " text/javascript " >
window . getDetails = function ( url , id ) {
$ . getJSON ( url , function ( data ) {
element = document . getElementById ( id );
2015-10-21 00:13:56 +02:00
element . innerHTML = " filename: " + data . filename + " \\ nmime type: " + data . mimeType + " \\ ncolors: " + data . colors + " \\ nsize: " + data . size + " \\ nwidth: " + data . width + " \\ nheigh: " + data . height + " \\ naspect-ratio: " + data . aspectRatio + ( data . pngType ? " \\ npng-type: " + data . pngType : '' );
2015-01-27 22:40:28 +01:00
});
}
</ script >
< script type = " text/javascript " > window . getDetails ( " { $url2 } &json " , " json " ) </ script >
EOD ;
2015-10-21 01:13:36 +02:00
} $img -> log ( " Incoming arguments: " . print_r ( verbose (), 1 )) -> setSaveFolder ( $cachePath ) -> useCache ( $useCache ) -> setSource ( $srcImage , $imagePath ) -> setOptions ( array ( 'newWidth' => $newWidth , 'newHeight' => $newHeight , 'aspectRatio' => $aspectRatio , 'keepRatio' => $keepRatio , 'cropToFit' => $cropToFit , 'fillToFit' => $fillToFit , 'crop' => $crop , 'area' => $area , 'upscale' => $upscale , 'scale' => $scale , 'rotateBefore' => $rotateBefore , 'autoRotate' => $autoRotate , 'bgColor' => $bgColor , 'palette' => $palette , 'filters' => $filters , 'sharpen' => $sharpen , 'emboss' => $emboss , 'blur' => $blur , 'convolve' => $convolve , 'rotateAfter' => $rotateAfter , 'outputFormat' => $outputFormat , 'dpr' => $dpr , ) ) -> loadImageDetails () -> initDimensions () -> calculateNewWidthAndHeight () -> setSaveAsExtension ( $saveAs ) -> setJpegQuality ( $quality ) -> setPngCompression ( $compress ) -> useOriginalIfPossible ( $useOriginal ) -> generateFilename ( $cachePath ) -> useCacheIfPossible ( $useCache ) -> load () -> preResize () -> resize () -> postResize () -> setPostProcessingOptions ( $postProcessing ) -> save () -> linkToCacheFile ( $aliasTarget ) -> output ();