one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-three forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine seventy seventy-one seventy-two seventy-three seventy-four seventy-five seventy-six seventy-seven seventy-eight seventy-nine eighty eighty-one eighty-two eighty-three eighty-four eighty-five eighty-six eighty-seven eighty-eight eighty-nine ninety ninety-one ninety-two ninety-three ninety-four ninety-five ninety-six ninety-seven ninety-eight ninety-nine one hundred one hundred and one one hundred and two one hundred and three one hundred and four one hundred and five one hundred and six one hundred and seven one hundred and eight one hundred and nine one hundred and ten one hundred and eleven one hundred and twelve one hundred and thirteen one hundred and fourteen one hundred and fifteen one hundred and sixteen one hundred and seventeen one hundred and eighteen one hundred and nineteen one hundred and twenty one hundred and twenty-one one hundred and twenty-two one hundred and twenty-three one hundred and twenty-four one hundred and twenty-five one hundred and twenty-six one hundred and twenty-seven one hundred and twenty-eight one hundred and twenty-nine one hundred and thirty one hundred and thirty-one one hundred and thirty-two one hundred and thirty-three one hundred and thirty-four one hundred and thirty-five one hundred and thirty-six one hundred and thirty-seven one hundred and thirty-eight one hundred and thirty-nine one hundred and forty one hundred and forty-one one hundred and forty-two one hundred and forty-three one hundred and forty-four one hundred and forty-five one hundred and forty-six one hundred and forty-seven one hundred and forty-eight one hundred and forty-nine one hundred and fifty one hundred and fifty-one one hundred and fifty-two one hundred and fifty-three one hundred and fifty-four one hundred and fifty-five one hundred and fifty-six one hundred and fifty-seven one hundred and fifty-eight one hundred and fifty-nine one hundred and sixty one hundred and sixty-one one hundred and sixty-two one hundred and sixty-three one hundred and sixty-four one hundred and sixty-five one hundred and sixty-six one hundred and sixty-seven one hundred and sixty-eight one hundred and sixty-nine one hundred and seventy one hundred and seventy-one one hundred and seventy-two one hundred and seventy-three one hundred and seventy-four one hundred and seventy-five one hundred and seventy-six one hundred and seventy-seven one hundred and seventy-eight one hundred and seventy-nine one hundred and eighty one hundred and eighty-one one hundred and eighty-two one hundred and eighty-three one hundred and eighty-four one hundred and eighty-five one hundred and eighty-six one hundred and eighty-seven one hundred and eighty-eight one hundred and eighty-nine one hundred and ninety one hundred and ninety-one one hundred and ninety-two one hundred and ninety-three one hundred and ninety-four one hundred and ninety-five one hundred and ninety-six one hundred and ninety-seven one hundred and ninety-eight one hundred and ninety-nine two hundred two hundred and one two hundred and two two hundred and three two hundred and four two hundred and five two hundred and six two hundred and seven two hundred and eight two hundred and nine two hundred and ten two hundred and eleven two hundred and twelve two hundred and thirteen two hundred and fourteen two hundred and fifteen two hundred and sixteen two hundred and seventeen two hundred and eighteen two hundred and nineteen two hundred and twenty two hundred and twenty-one two hundred and twenty-two two hundred and twenty-three two hundred and twenty-four two hundred and twenty-five two hundred and twenty-six two hundred and twenty-seven two hundred and twenty-eight two hundred and twenty-nine two hundred and thirty two hundred and thirty-one two hundred and thirty-two two hundred and thirty-three two hundred and thirty-four two hundred and thirty-five two hundred and thirty-six two hundred and thirty-seven two hundred and thirty-eight two hundred and thirty-nine two hundred and forty two hundred and forty-one two hundred and forty-two two hundred and forty-three two hundred and forty-four two hundred and forty-five two hundred and forty-six two hundred and forty-seven two hundred and forty-eight two hundred and forty-nine two hundred and fifty two hundred and fifty-one two hundred and fifty-two two hundred and fifty-three two hundred and fifty-four two hundred and fifty-five two hundred and fifty-six two hundred and fifty-seven two hundred and fifty-eight two hundred and fifty-nine two hundred and sixty two hundred and sixty-one two hundred and sixty-two two hundred and sixty-three two hundred and sixty-four two hundred and sixty-five two hundred and sixty-six two hundred and sixty-seven two hundred and sixty-eight two hundred and sixty-nine two hundred and seventy two hundred and seventy-one two hundred and seventy-two two hundred and seventy-three two hundred and seventy-four two hundred and seventy-five two hundred and seventy-six two hundred and seventy-seven two hundred and seventy-eight two hundred and seventy-nine two hundred and eighty two hundred and eighty-one two hundred and eighty-two two hundred and eighty-three two hundred and eighty-four two hundred and eighty-five two hundred and eighty-six two hundred and eighty-seven two hundred and eighty-eight two hundred and eighty-nine two hundred and ninety two hundred and ninety-one two hundred and ninety-two two hundred and ninety-three two hundred and ninety-four two hundred and ninety-five two hundred and ninety-six two hundred and ninety-seven two hundred and ninety-eight two hundred and ninety-nine three hundred three hundred and one three hundred and two three hundred and three three hundred and four three hundred and five three hundred and six three hundred and seven three hundred and eight three hundred and nine three hundred and ten three hundred and eleven three hundred and twelve three hundred and thirteen three hundred and fourteen three hundred and fifteen three hundred and sixteen three hundred and seventeen three hundred and eighteen three hundred and nineteen three hundred and twenty three hundred and twenty-one three hundred and twenty-two three hundred and twenty-three three hundred and twenty-four three hundred and twenty-five three hundred and twenty-six three hundred and twenty-seven three hundred and twenty-eight three hundred and twenty-nine three hundred and thirty three hundred and thirty-one three hundred and thirty-two three hundred and thirty-three three hundred and thirty-four three hundred and thirty-five three hundred and thirty-six three hundred and thirty-seven three hundred and thirty-eight three hundred and thirty-nine three hundred and forty three hundred and forty-one three hundred and forty-two three hundred and forty-three three hundred and forty-four three hundred and forty-five three hundred and forty-six three hundred and forty-seven three hundred and forty-eight three hundred and forty-nine three hundred and fifty three hundred and fifty-one three hundred and fifty-two three hundred and fifty-three three hundred and fifty-four three hundred and fifty-five three hundred and fifty-six three hundred and fifty-seven three hundred and fifty-eight three hundred and fifty-nine three hundred and sixty three hundred and sixty-one three hundred and sixty-two three hundred and sixty-three three hundred and sixty-four three hundred and sixty-five three hundred and sixty-six three hundred and sixty-seven three hundred and sixty-eight three hundred and sixty-nine three hundred and seventy three hundred and seventy-one three hundred and seventy-two three hundred and seventy-three three hundred and seventy-four three hundred and seventy-five three hundred and seventy-six three hundred and seventy-seven three hundred and seventy-eight three hundred and seventy-nine three hundred and eighty three hundred and eighty-one three hundred and eighty-two three hundred and eighty-three three hundred and eighty-four three hundred and eighty-five three hundred and eighty-six three hundred and eighty-seven three hundred and eighty-eight three hundred and eighty-nine three hundred and ninety three hundred and ninety-one three hundred and ninety-two three hundred and ninety-three three hundred and ninety-four three hundred and ninety-five three hundred and ninety-six three hundred and ninety-seven three hundred and ninety-eight three hundred and ninety-nine four hundred four hundred and one four hundred and two four hundred and three four hundred and four four hundred and five four hundred and six four hundred and seven four hundred and eight four hundred and nine four hundred and ten four hundred and eleven four hundred and twelve four hundred and thirteen four hundred and fourteen four hundred and fifteen four hundred and sixteen four hundred and seventeen four hundred and eighteen four hundred and nineteen four hundred and twenty four hundred and twenty-one four hundred and twenty-two four hundred and twenty-three four hundred and twenty-four four hundred and twenty-five four hundred and twenty-six four hundred and twenty-seven four hundred and twenty-eight four hundred and twenty-nine four hundred and thirty four hundred and thirty-one four hundred and thirty-two four hundred and thirty-three four hundred and thirty-four four hundred and thirty-five four hundred and thirty-six four hundred and thirty-seven four hundred and thirty-eight four hundred and thirty-nine four hundred and forty four hundred and forty-one four hundred and forty-two four hundred and forty-three four hundred and forty-four four hundred and forty-five four hundred and forty-six four hundred and forty-seven four hundred and forty-eight four hundred and forty-nine four hundred and fifty four hundred and fifty-one four hundred and fifty-two four hundred and fifty-three four hundred and fifty-four four hundred and fifty-five four hundred and fifty-six four hundred and fifty-seven four hundred and fifty-eight four hundred and fifty-nine four hundred and sixty four hundred and sixty-one four hundred and sixty-two four hundred and sixty-three four hundred and sixty-four four hundred and sixty-five four hundred and sixty-six four hundred and sixty-seven four hundred and sixty-eight four hundred and sixty-nine four hundred and seventy four hundred and seventy-one four hundred and seventy-two four hundred and seventy-three four hundred and seventy-four four hundred and seventy-five four hundred and seventy-six four hundred and seventy-seven four hundred and seventy-eight four hundred and seventy-nine four hundred and eighty four hundred and eighty-one four hundred and eighty-two four hundred and eighty-three four hundred and eighty-four four hundred and eighty-five four hundred and eighty-six four hundred and eighty-seven four hundred and eighty-eight four hundred and eighty-nine four hundred and ninety four hundred and ninety-one four hundred and ninety-two four hundred and ninety-three four hundred and ninety-four four hundred and ninety-five four hundred and ninety-six four hundred and ninety-seven four hundred and ninety-eight four hundred and ninety-nine five hundred five hundred and one five hundred and two five hundred and three five hundred and four five hundred and five five hundred and six five hundred and seven five hundred and eight five hundred and nine five hundred and ten five hundred and eleven five hundred and twelve five hundred and thirteen five hundred and fourteen five hundred and fifteen five hundred and sixteen five hundred and seventeen five hundred and eighteen five hundred and nineteen five hundred and twenty five hundred and twenty-one five hundred and twenty-two five hundred and twenty-three five hundred and twenty-four five hundred and twenty-five five hundred and twenty-six five hundred and twenty-seven five hundred and twenty-eight | /*The measurement function is used to detect whether there is any content in the array storing error information, that is, whether there is any error in the last operation*/
protected function haveErrors ( ) {
if ( sizeof ( $this -> errors ) > zero ) {
return true ;
}
return false ;
}
/*This function outputs the stored error information*/
protected function serveErrors ( ) {
//Set http header
header ( $_SERVER [ 'SERVER_PROTOCOL' ] . ' 400 Bad Request' ) ;
//Circular output error list information
$html = '<ul>' ;
foreach ( $this -> errors as $err ) {
$html .= '<li>' . htmlentities ( $err ) . '</li>' ;
}
$html .= '</ul>' ;
//Output other error messages
echo '<h1>A TimThumb error has occured</h1>The following error(s) occured:<br />' . $html . '<br />' ;
echo '<br />Query String : ' . htmlentities ( $_SERVER [ 'QUERY_STRING' ] ) ;
echo '<br />TimThumb version : ' . VERSION ;
}
/*This function is used to read the local image*/
protected function serveInternalImage ( ) {
//Write a log to record the local image address
$this -> debug ( three , "Local image path is $this->localImage " ) ;
//If the address is invalid
if ( ! $this -> localImage ) {
//Log this error and exit execution
$this -> sanityFail ( "localImage not set after verifying it earlier in the code." ) ;
return false ;
}
//Get local picture size
$fileSize = filesize ( $this -> localImage ) ;
//If the size of the local picture exceeds the relevant settings of the configuration file
if ( $fileSize > MAX_FILE_SIZE ) {
//Record the error reason and exit
$this -> error ( "The file you specified is greater than the maximum allowed file size." ) ;
return false ;
}
//If the acquired image size is invalid
if ( $fileSize <= zero ) {
//Log errors and exit
$this -> error ( "The file you specified is <= 0 bytes." ) ;
return false ;
}
//If the above verification is passed, a log will be written to record that the local image will be processed with the processImageAndWriteToCache function
$this -> debug ( three , "Calling processImageAndWriteToCache() for local image." ) ;
//If the processing is successful, the image will be returned from the cache
if ( $this -> processImageAndWriteToCache ( $this -> localImage ) ) {
$this -> serveCacheFile ( ) ;
return true ;
//Return false if failed
} else {
return false ;
}
}
/*This function is used to clean the cache*/
protected function cleanCache ( ) {
//If the defined cache time is less than 0, exit
if ( FILE_CACHE_TIME_BETWEEN_CLEANS < zero ) {
return ;
}
//Write logs, record cache clearing operations, level 3
$this -> debug ( three , "cleanCache() called" ) ;
//This file is a timestamp file that records the last cache clearing operation
$lastCleanFile = $this -> cacheDirectory . '/timthumb_cacheLastCleanTime.touch' ; //If the file defined above does not exist, it means that this is the first time to clear the cache. Create this file and return null
if ( ! is_file ( $lastCleanFile ) ) {
//Write log, record creation file, level 1
$this -> debug ( one , "File tracking last clean doesn't exist. Creating $lastCleanFile " ) ;
//Create this file
if ( ! touch ( $lastCleanFile ) ) {
//If it fails, report an error and exit
$this -> error ( "Could not create cache clean timestamp file." ) ;
}
return ;
}
//If the cache time has exceeded
if ( @ filemtime ( $lastCleanFile ) < ( time ( ) - FILE_CACHE_TIME_BETWEEN_CLEANS ) ) {
//Write a log to record the following cache clearing operations
$this -> debug ( one , "Cache was last cleaned more than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago. Cleaning now." ) ;
//Create a new purge cache timestamp file
if ( ! touch ( $lastCleanFile ) ) {
//Record the error message in case of failure
$this -> error ( "Could not create cache clean timestamp file." ) ;
}
//This array stores all cache files according to the cache file directory and cache file suffix defined previously
$files = glob ( $this -> cacheDirectory . '/*' . FILE_CACHE_SUFFIX ) ;
//If there are cache files
if ( $files ) {
//Calculate the difference between the current time and the maximum lifetime of the cache to determine whether the cache file is deleted
$timeAgo = time ( ) - FILE_CACHE_MAX_FILE_AGE ;
//Traversal cache file array
foreach ( $files as $file ) {
//If the file creation time is less than the value calculated above, that is to say, the cache file will be deleted when its expiration date is reached
if ( @ filemtime ( $file ) < $timeAgo ) {
//Record deletion cache file, level 3
$this -> debug ( three , "Deleting cache file $file older than max age: " . FILE_CACHE_MAX_FILE_AGE . " seconds" ) ;
@ unlink ( $file ) ;
}
}
}
return true ;
//Do not clear if the cache time is not exceeded
} else {
//Write logs without clearing cache
$this -> debug ( three , "Cache was cleaned less than " . FILE_CACHE_TIME_BETWEEN_CLEANS . " seconds ago so no cleaning needed." ) ;
}
return false ;
}
/*Core function, processing pictures and writing to cache*/
protected function processImageAndWriteToCache ( $localImage ) {
//Get picture information
$sData = getimagesize ( $localImage ) ;
//Image type marker
$origType = $sData [ two ] ;
//Mime type
$mimeType = $sData [ 'mime' ] ;
//Write a log to record the mime type of the incoming image
$this -> debug ( three , "Mime type of image is $mimeType " ) ;
//Perform image mime type verification. Only gif, jpg and png are allowed
if ( ! preg_match ( '/^image\/(?:gif|jpg|jpeg|png)$/i' , $mimeType ) ) {
//If not, record the error message and exit the script
return $this -> error ( "The image being resized is not a valid gif, jpg or png." ) ;
}
//Image processing requires GD library support. Check whether GD library is installed
if ( ! function_exists ( 'imagecreatetruecolor' ) ) {
//If not installed, the script will be launched
return $this -> error ( 'GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library' ) ;
}
//If the GD library is installed, the image filter function imagefilter is supported, and the IMG_FILTER_NEGATE constant is supported
if ( function_exists ( 'imagefilter' ) && defined ( 'IMG_FILTER_NEGATE' ) ) {
//Define a filter effect array, and the numbers after it represent the parameters that need to be passed in additionally
$imageFilters = array (
//Negative film
one => array ( IMG_FILTER_NEGATE , zero ) ,
//Black-and-white
two => array ( IMG_FILTER_GRAYSCALE , zero ) ,
//Brightness level
three => array ( IMG_FILTER_BRIGHTNESS , one ) ,
//Contrast Level
four => array ( IMG_FILTER_CONTRAST , one ) ,
//Image conversion to specified color
five => array ( IMG_FILTER_COLORIZE , four ) ,
//Protruding edge
six => array ( IMG_FILTER_EDGEDETECT , zero ) ,
//Relief
seven => array ( IMG_FILTER_EMBOSS , zero ) ,
//Blur image with Gaussian algorithm
eight => array ( IMG_FILTER_GAUSSIAN_BLUR , zero ) ,
//Blurred image
nine => array ( IMG_FILTER_SELECTIVE_BLUR , zero ) ,
//Average removal method to achieve contour effect
ten => array ( IMG_FILTER_MEAN_REMOVAL , zero ) ,
//Smoothing
eleven => array ( IMG_FILTER_SMOOTH , zero ) ,
) ;
} //The width of the generated picture, specified by the w parameter in get, is 0 by default
$new_width = ( int ) abs ( $this -> param ( 'w' , zero ) ) ;
//The height of the generated image, specified by the h parameter in get, is 0 by default
$new_height = ( int ) abs ( $this -> param ( 'h' , zero ) ) ;
//Generate the image scaling mode, which is specified by the zc parameter in get. The default value is DEFAULT_ZC in the configuration file
$zoom_crop = ( int ) $this -> param ( 'zc' , DEFAULT_ZC ) ;
//The quality of the generated image, specified by the q parameter in get, defaults to the value of DEFAULT_Q in the configuration file
$quality = ( int ) abs ( $this -> param ( 'q' , DEFAULT_Q ) ) ;
//Cropped position
$align = $this -> cropTop ? 't' : $this -> param ( 'a' , 'c' ) ;
//For image processing operations to be performed, multiple filters are separated by "|". For optional parameters, see the note at $imageFilters. Because different filters require different parameters, for example, one filter requires multiple parameters, multiple parameters are separated by. For example, 1,2 | 3,1,1 means to apply 1 and 3 filtering effects to the image respectively. The corresponding filtering effects of 1 and 3 are determined by the $imageFilters array. Filter 1 requires an additional parameter. Here, filter 1 and filter 3 require two additional parameters. Here, filter 1 and filter 1 are passed
$filters = $this -> param ( 'f' , DEFAULT_F ) ;
//Whether to sharpen the picture is specified by the s parameter in get. The default value is DEFAULT_S in the configuration file
$sharpen = ( bool ) $this -> param ( 's' , DEFAULT_S ) ;
//The default background canvas color of the generated picture, specified by the cc parameter in get, is the value of DEFAULT_CC in the configuration file by default
$canvas_color = $this -> param ( 'cc' , DEFAULT_CC ) ;
//Whether the background of the generated png image is transparent
$canvas_trans = ( bool ) $this -> param ( 'ct' , '1' ) ; //If the height and width are not specified, set them to 100 * 100
if ( $new_width == zero && $new_height == zero ) {
$new_width = one hundred ;
$new_height = one hundred ;
} //Limit maximum height and width
$new_width = min ( $new_width , MAX_WIDTH ) ;
$new_height = min ( $new_height , MAX_HEIGHT ) ; //Detect and set the maximum memory occupied by php operation
$this -> setMemoryLimit ( ) ; //Open Image Resources
$image = $this -> openImage ( $mimeType , $localImage ) ;
//If the opening fails, record the information and exit the script
if ( $image === false ) {
return $this -> error ( 'Unable to open image.' ) ;
} //Get the original image, that is, the width and height of the image opened above
$width = imagesx ( $image ) ;
$height = imagesy ( $image ) ;
$origin_x = zero ;
$origin_y = zero ; //If the width or height of the newly generated picture is not specified, use this proportional algorithm to calculate the height or width value
if ( $new_width && ! $new_height ) {
$new_height = floor ( $height * ( $new_width / $width ) ) ;
} else if ( $new_height && ! $new_width ) {
$new_width = floor ( $width * ( $new_height / $height ) ) ;
} //If 3 is selected as the zoom mode, that is, zc=3 in get or DEFAULT_ZC=3 in the configuration file, then perform proportional scaling without clipping
if ( $zoom_crop == three ) { $final_height = $height * ( $new_width / $width ) ;
//Set the width or height after proportional calculation according to the proportional algorithm
if ( $final_height > $new_height ) {
$new_width = $width * ( $new_height / $height ) ;
} else {
$new_height = $final_height ;
} } //Create a new canvas with the processed length and width,
$canvas = imagecreatetruecolor ( $new_width , $new_height ) ;
//Turn off the color mixing mode, that is, save the alpha value of PNG to make the background transparent
imagealphablending ( $canvas , false ) ;
//Detect and convert the default canvas color. If a color value of 3 characters is given
if ( strlen ( $canvas_color ) == three ) { //if is 3-char notation, edit string into 6-char notation
//Convert to the color value represented by 6 characters
$canvas_color = str_repeat ( substr ( $canvas_color , zero , one ) , two ) . str_repeat ( substr ( $canvas_color , one , one ) , two ) . str_repeat ( substr ( $canvas_color , two , one ) , two ) ;
//If it is not a string of 3 or 6 lengths, it is illegal and set as the default value
} else if ( strlen ( $canvas_color ) != six ) {
$canvas_color = DEFAULT_CC ;
}
//Convert the three color values of R, G and B obtained above to decimal representation
$canvas_color_R = hexdec ( substr ( $canvas_color , zero , two ) ) ;
$canvas_color_G = hexdec ( substr ( $canvas_color , two , two ) ) ;
$canvas_color_B = hexdec ( substr ( $canvas_color , four , two ) ) ; //If the format of the incoming picture is png, and the configuration file sets the png background color to transparent, and the value of the incoming ct in get is true, then set the background color to transparent
if ( preg_match ( '/^image\/png$/i' , $mimeType ) && ! PNG_IS_TRANSPARENT && $canvas_trans ) {
$color = imagecolorallocatealpha ( $canvas , $canvas_color_R , $canvas_color_G , $canvas_color_B , one hundred and twenty-seven ) ;
//Otherwise, set it to opaque
} else {
$color = imagecolorallocatealpha ( $canvas , $canvas_color_R , $canvas_color_G , $canvas_color_B , zero ) ;
} //Fill background with assigned color
imagefill ( $canvas , zero , zero , $color ) ; //If 2 is selected as the zoom mode, the volume of the canvas is created according to the incoming value, and the width of the border is calculated
if ( $zoom_crop == two ) {
//Proportionally scaled height
$final_height = $height * ( $new_width / $width ) ;
//If the calculated equal height is greater than the incoming height
if ( $final_height > $new_height ) {
//Origin_x is equal to half of the new height passed in
$origin_x = $new_width / two ;
//Set the new width to the value calculated proportionally
$new_width = $width * ( $new_height / $height ) ;
//Calculate the difference between two origin_x
$origin_x = round ( $origin_x - ( $new_width / two ) ) ;
//Otherwise, calculate the difference between two origin_y values
} else {
$origin_y = $new_height / two ;
$new_height = $final_height ;
$origin_y = round ( $origin_y - ( $new_height / two ) ) ; } } //Save the complete alpha information when saving the image
imagesavealpha ( $canvas , true ) ; //If zoom mode is 1 or 2 or 3
if ( $zoom_crop > zero ) { $src_x = $src_y = zero ;
//Original width of picture
$src_w = $width ;
//Original height of picture
$src_h = $height ; //Picture aspect ratio
$cmp_x = $width / $new_width ;
$cmp_y = $height / $new_height ; //Clipping algorithm
if ( $cmp_x > $cmp_y ) {
$src_w = round ( $width / $cmp_x * $cmp_y ) ;
$src_x = round ( ( $width - ( $width / $cmp_x * $cmp_y ) ) / two ) ; } else if ( $cmp_y > $cmp_x ) { $src_h = round ( $height / $cmp_y * $cmp_x ) ;
$src_y = round ( ( $height - ( $height / $cmp_y * $cmp_x ) ) / two ) ; } //Calculate the clipping position according to the incoming parameters
if ( $align ) {
if ( strpos ( $align , 't' ) !== false ) {
$src_y = zero ;
}
if ( strpos ( $align , 'b' ) !== false ) {
$src_y = $height - $src_h ;
}
if ( strpos ( $align , 'l' ) !== false ) {
$src_x = zero ;
}
if ( strpos ( $align , 'r' ) !== false ) {
$src_x = $width - $src_w ;
}
} //Crop the image according to the algorithm and copy it to the background image
imagecopyresampled ( $canvas , $image , $origin_x , $origin_y , $src_x , $src_y , $new_width , $new_height , $src_w , $src_h ) ; } else { //If 0 is selected as the clipping mode, no clipping will be performed and an image will be generated
imagecopyresampled ( $canvas , $image , zero , zero , zero , zero , $new_width , $new_height , $width , $height ) ; }
//If image processing operations are defined and image processing functions are supported
if ( $filters != '' && function_exists ( 'imagefilter' ) && defined ( 'IMG_FILTER_NEGATE' ) ) {
//Split each filter process
$filterList = explode ( '|' , $filters ) ;
foreach ( $filterList as $fl ) {
//Split parameters in a filtering operation
$filterSettings = explode ( ',' , $fl ) ;
//If the selected filtering operation exists
if ( isset ( $imageFilters [ $filterSettings [ zero ] ] ) ) {
//Convert all parameters to int type
for ( $i = zero ; $i < four ; $i ++ ) {
if ( ! isset ( $filterSettings [ $i ] ) ) {
$filterSettings [ $i ] = null ;
} else {
$filterSettings [ $i ] = ( int ) $filterSettings [ $i ] ;
}
}
//Apply filter effects to images according to the different parameters required for each filter effect defined in $imageFilters
switch ( $imageFilters [ $filterSettings [ zero ] ] [ one ] ) { case one : imagefilter ( $canvas , $imageFilters [ $filterSettings [ zero ] ] [ zero ] , $filterSettings [ one ] ) ;
break ; case two : imagefilter ( $canvas , $imageFilters [ $filterSettings [ zero ] ] [ zero ] , $filterSettings [ one ] , $filterSettings [ two ] ) ;
break ; case three : imagefilter ( $canvas , $imageFilters [ $filterSettings [ zero ] ] [ zero ] , $filterSettings [ one ] , $filterSettings [ two ] , $filterSettings [ three ] ) ;
break ; case four : imagefilter ( $canvas , $imageFilters [ $filterSettings [ zero ] ] [ zero ] , $filterSettings [ one ] , $filterSettings [ two ] , $filterSettings [ three ] , $filterSettings [ four ] ) ;
break ; default : imagefilter ( $canvas , $imageFilters [ $filterSettings [ zero ] ] [ zero ] ) ;
break ; }
}
}
} //If the sharpening value is set and the system supports the sharpening function, the sharpening operation is performed
if ( $sharpen && function_exists ( 'imageconvolution' ) ) { $sharpenMatrix = array (
array ( - one ,- one ,- one ) ,
array ( - one , sixteen ,- one ) ,
array ( - one ,- one ,- one ) ,
) ; $divisor = eight ;
$offset = zero ; imageconvolution ( $canvas , $sharpenMatrix , $divisor , $offset ) ; }
//If the image is PNG or GIF, use imageruecolortopalette to reduce their size
if ( ( IMAGETYPE_PNG == $origType || IMAGETYPE_GIF == $origType ) && function_exists ( 'imageistruecolor' ) && ! imageistruecolor ( $image ) && imagecolortransparent ( $image ) > zero ) {
imagetruecolortopalette ( $canvas , false , imagecolorstotal ( $image ) ) ;
}
//Generate image cache according to different image types generated, and the value of $imgType is used to generate security header
$imgType = "" ;
$tempfile = tempnam ( $this -> cacheDirectory , 'timthumb_tmpimg_' ) ;
if ( preg_match ( '/^image\/(?:jpg|jpeg)$/i' , $mimeType ) ) {
$imgType = 'jpg' ;
imagejpeg ( $canvas , $tempfile , $quality ) ;
} else if ( preg_match ( '/^image\/png$/i' , $mimeType ) ) {
$imgType = 'png' ;
imagepng ( $canvas , $tempfile , floor ( $quality * zero point zero nine ) ) ;
} else if ( preg_match ( '/^image\/gif$/i' , $mimeType ) ) {
$imgType = 'gif' ;
imagegif ( $canvas , $tempfile ) ;
} else {
//If not, record this error and exit
return $this -> sanityFail ( "Could not match mime type after verifying it previously." ) ;
}
//Give priority to using optimpng tool to compress png images, provided that you have installed the tool
if ( $imgType == 'png' && OPTIPNG_ENABLED && OPTIPNG_PATH && @ is_file ( OPTIPNG_PATH ) ) {
//Record the address of optipng
$exec = OPTIPNG_PATH ;
//Write logs to record that optipng will run, level 3
$this -> debug ( three , "optipng'ing $tempfile " ) ;
//Get Picture Size
$presize = filesize ( $tempfile ) ;
//Perform image compression operation
$out = ` $exec - o1 $tempfile ` ;
//Clear file status cache
clearstatcache ( ) ;
//Get the compressed file size
$aftersize = filesize ( $tempfile ) ;
//Calculate how much compression
$sizeDrop = $presize - $aftersize ;
//Write logs according to different calculated values, level 1
if ( $sizeDrop > zero ) {
$this -> debug ( one , "optipng reduced size by $sizeDrop " ) ;
} else if ( $sizeDrop < zero ) {
$this -> debug ( one , "optipng increased size! Difference was: $sizeDrop " ) ;
} else {
$this -> debug ( one , "optipng did not change image size." ) ;
}
//Optipng does not exist. Try pngcrush
} else if ( $imgType == 'png' && PNGCRUSH_ENABLED && PNGCRUSH_PATH && @ is_file ( PNGCRUSH_PATH ) ) {
$exec = PNGCRUSH_PATH ;
//Different from optimpng, pngcrush will generate a new file from the processed file, so create a new file here
$tempfile2 = tempnam ( $this -> cacheDirectory , 'timthumb_tmpimg_' ) ;
//Write logs and record file names
$this -> debug ( three , "pngcrush'ing $tempfile to $tempfile2 " ) ;
//Run pngcrush
$out = ` $exec $tempfile $tempfile2 ` ;
$todel = "" ;
//If the file is generated successfully
if ( is_file ( $tempfile2 ) ) {
//Calculate the difference of the compressed file size
$sizeDrop = filesize ( $tempfile ) - filesize ( $tempfile2 ) ;
//If it is a valid compression, the compressed file will be used as the cache file
if ( $sizeDrop > zero ) {
$this -> debug ( one , "pngcrush was succesful and gave a $sizeDrop byte size reduction" ) ;
$todel = $tempfile ;
$tempfile = $tempfile2 ;
//Otherwise, this file is unnecessary
} else {
$this -> debug ( one , "pngcrush did not reduce file size. Difference was $sizeDrop bytes." ) ;
$todel = $tempfile2 ;
}
//You need to delete this file if it does not run successfully
} else {
$this -> debug ( three , "pngcrush failed with output: $out " ) ;
$todel = $tempfile2 ;
}
//Delete invalid files or large files before compression
@ unlink ( $todel ) ;
}
//Write the security header on the cached image
$this -> debug ( three , "Rewriting image with security header." ) ;
//Create a new cache file
$tempfile4 = tempnam ( $this -> cacheDirectory , 'timthumb_tmpimg_' ) ;
//
$context = stream_context_create ( ) ;
//Read the generated image cache content
$fp = fopen ( $tempfile , 'r' , zero , $context ) ;
//Write the security header to the new cache file. The length of the security header should always be $this ->filePrependSecurityBlock+6
file_put_contents ( $tempfile4 , $this -> filePrependSecurityBlock . $imgType . ' ?' . '>' ) ;
//Write the read cache image content to a new cache file
file_put_contents ( $tempfile4 , $fp , FILE_APPEND ) ;
//Close File Resources
fclose ( $fp ) ;
//Delete previously unsafe picture cache files
@ unlink ( $tempfile ) ;
//Write logs and lock cache files ~ ~
$this -> debug ( three , "Locking and replacing cache file." ) ;
//Create lock file file name
$lockFile = $this -> cachefile . '.lock' ;
//Create or open lock file
$fh = fopen ( $lockFile , 'w' ) ;
//Fail to create and exit directly
if ( ! $fh ) {
return $this -> error ( "Could not open the lockfile for writing an image." ) ;
}
//If the write lock is successfully added to the lock file
if ( flock ( $fh , LOCK_EX ) ) {
//Delete the original cache file
@ unlink ( $this -> cachefile ) ;
//Rename the overwrite and use the secure cache file as the cache file
rename ( $tempfile4 , $this -> cachefile ) ;
//Release the write lock
flock ( $fh , LOCK_UN ) ;
//Free resources
fclose ( $fh ) ;
//Delete lock file
@ unlink ( $lockFile ) ;
//Otherwise
} else {
//Close Resources
fclose ( $fh ) ;
//Delete lock file
@ unlink ( $lockFile ) ;
//Delete a secure cache file
@ unlink ( $tempfile4 ) ;
//Log errors and exit
return $this -> error ( "Could not get a lock for writing." ) ;
}
//Write the log and record the completion of the operation
$this -> debug ( three , "Done image replace with security header. Cleaning up and running cleanCache()" ) ;
//Free picture resources
imagedestroy ( $canvas ) ;
imagedestroy ( $image ) ;
//Successfully generated cache returns true
return true ;
} |