array( 'size' => '200x150', // 150x*, *x200, 150x100 'nofit' => false, // true, false 'enlarge' => false, // true, false 'crop' => 'n', // lt, t, rt, l, c ,r, lb, b, rb, n 'fillcolor' => '#d7d7d7', // white, gray, black, yellow, green, blue, #ffffff; 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'mainnews' => array( 'size' => '245x*', 'nofit' => false, 'enlarge' => false, 'crop' => 'n', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'small' => array( 'size' => '120x90', 'nofit' => false, 'enlarge' => false, 'crop' => 't', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'thumb' => array( 'size' => '200x*', 'nofit' => false, 'enlarge' => false, 'crop' => 'n', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'vote' => array( 'size' => '300x*', 'nofit' => false, 'enlarge' => true, 'crop' => 'n', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'preview' => array( 'size' => '35x*', 'nofit' => false, 'enlarge' => false, 'crop' => 't', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'photo' => array( 'size' => '600x*', 'nofit' => false, 'enlarge' => false, 'crop' => 'c', 'fillcolor' => '#ffffff', 'bgimg' => '', // todo для наложения на картинку 'watermark' => '', // todo для текста поверху ), 'cell-article' => array( 'size' => '87x55', // 150x*, *x200, 150x100 'nofit' => false, // true, false 'enlarge' => false, // true, false 'crop' => 'c', // lt, t, rt, l, c ,r, lb, b, rb, n 'fillcolor' => '#ffffff', // white, gray, black, yellow, green, blue, #ffffff; ), 'column-photo' => array( 'size' => '220x110', // 150x*, *x200, 150x100 'nofit' => false, // true, false 'enlarge' => false, // true, false 'crop' => 'c', // lt, t, rt, l, c ,r, lb, b, rb, n 'fillcolor' => '#ffffff', // white, gray, black, yellow, green, blue, #ffffff; ), ); $folder = $_SERVER['DOCUMENT_ROOT'].'/files/'; // folder (prefix) $folder_allow = ''; // '' - all thumb(); function get_parameters() { global $class; $get = array(); $matches = array(); if (@isset($_GET['url']) && preg_match("~_thumb\(([^\)]*)\)~isU", $_GET['url'], $matches)) { $getparams = substr($matches[0], 7, strpos($matches[0], ")")-7); $getparams = explode(",", $getparams); foreach ($getparams as $value) { if ($value != "") { if (preg_match( "~([0-9]+x[0-9]+|[0-9]+x\*|\*x[0-9]+)~", $value )) { $get['size'] = $value; } else { switch (strtolower($value)) { case "enlarge": $get['enlarge'] = true; break; case "nofit": $get['nofit'] = true; break; case "crop=lt": $get['crop'] = "lt"; break; case "crop=t": $get['crop'] = "t"; break; case "crop=rt": $get['crop'] = "rt"; break; case "crop=l": $get['crop'] = "l"; break; case "crop=c": $get['crop'] = "c"; break; case "crop=r": $get['crop'] = "r"; break; case "crop=lb": $get['crop'] = "lb"; break; case "crop=b": $get['crop'] = "b"; break; case "crop=rb": $get['crop'] = "rb"; break; case "crop=n": $get['crop'] = "n"; break; default: if (substr($value, 0, 4) != "crop=") { $get['class'] = $value; } break; } } } } $get['source_path'] = substr($_GET['url'], 0, strpos($_GET["url"], "_thumb(")) . substr($_GET['url'], strpos($_GET["url"], ")") + 1 ); } elseif (@isset($_GET['url']) && preg_match("~_thumb\.~isU", $_GET['url'], $matches)) { // default values $get['source_path'] = substr($_GET['url'], 0, strpos($_GET["url"], "_thumb.")) . substr($_GET['url'], strpos($_GET["url"], "_thumb.") + 6 ); } else { die("file not found"); // file not found } if (@isset($get['class']) && @isset($class[ $get['class'] ])) { $params = $class[ $get['class'] ]; } else { $params = $class['default']; } //$get['source_path'] = ltrim($get['source_path'], "/"); $params['source_filename'] = strrpos($get['source_path'], "/") !== false ? substr($get['source_path'], strrpos($get['source_path'], "/") + 1) : $get['source_path']; $params['result_filename'] = strrpos($_GET["url"], "/") !== false ? substr($_GET['url'], strrpos($_GET["url"], "/") + 1 ) : $_GET["url"]; $params['source_dir'] = strrpos($_GET["url"], "/") !== false ? ltrim(substr($_GET['url'], 0, strrpos($_GET["url"], "/") + 1 ), "/") : ""; if ( @isset($get['crop']) ) { $params['crop'] = $get['crop']; } if ( @isset($get['nofit']) ) { $params['nofit'] = $get['nofit']; } if ( @isset($get['enlarge']) ) { $params['enlarge'] = $get['enlarge']; } $matches = array(); if ( @isset($get['size']) && preg_match( "~^([0-9]+x[0-9]+|[0-9]+x\*|\*x[0-9]+)$~", $get['size'], $matches ) ) { $params['size'] = $get['size']; } $params['width'] = substr($params['size'], 0, strpos($params['size'],"x",0)); $params['height'] = substr($params['size'], strpos($params['size'],"x",0) + 1); if (!check_parameters($params) ) { error(array_merge($params, array('message' => '!check_parameters'))); } return $params; } function check_parameters($params) { // todo // $params['height'] > 0; // $params['width'] > 0; // $params['height'] != "*" && $params['width'] != "*"; //print_r($params); // if (!in_array($width, $widths)) // error(); // if (!is_readable($filename)) // error(); return true; } function thumb() { global $folder, $folder_allow, $widths; $parameters = get_parameters(); if (!function_exists('imagecreatefromgif') || ( substr( $parameters['source_dir'], 0, strlen(ltrim($folder_allow,'/')) ) != ltrim($folder_allow,'/') ) ) { error("!function_exists('imagecreatefromgif')"); } $source = array(); $source['filename'] = $folder . $parameters['source_dir'] . $parameters['source_filename']; // $source['dir'] = $folder . $parameters['source_dir']; $result = array(); $result['filename'] = $folder . $parameters['source_dir'] . $parameters['result_filename']; // $result['dir'] = $folder . $parameters['source_dir']; thumb_make($source, $result, $parameters); send_image($result['filename']); error(); } function thumb_make(&$source, &$result, &$parameters) { $info = getimagesize($source['filename']); if (!$info) { error(array_merge($source, array('message' => 'getimagesize'))); } // source dimentions $source['width'] = $info[0]; $source['height'] = $info[1]; $source['mime'] = $info['mime']; $source['type'] = $info[2]; if (!in_array($source['type'], array(1,2,3))) { error(array_merge($source, array('message' => '!in_array($source[\'type\'], array(1,2,3))'))); } $preview = array(); switch ($source['type']){ case 1: $source['image'] = imagecreatefromgif($source['filename']); break; case 2: $source['image'] = imagecreatefromjpeg($source['filename']); break; case 3: $source['image'] = imagecreatefrompng($source['filename']); break; } if (!$source['image']) { error(array_merge($source, array('message' => '!$source[\'image\']'))); } if ($parameters['width'] != "*" && $parameters['height'] != "*") { // hamster.jpg if ($source['width'] >= $parameters['width'] && $source['height'] >= $parameters['height']) { if ($parameters['crop'] == "n") { $preview['width'] = $parameters['width']; $preview['height'] = $parameters['height']; $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; } else { // that mean crop, resize and/or fill $preview['width'] = $parameters['width']; $preview['height'] = $parameters['height']; if ($parameters['nofit']) { $source['select_width'] = $preview['width']; $source['select_height'] = $preview['height']; } else { if ($parameters['width'] >= $parameters['height'] && $source['height'] < $source['width']) { $source['select_width'] = $source['width']; $source['select_height'] = $source['width'] * ($parameters['height'] / $parameters['width']); if ($source['select_height'] > $source['height']) { $source['select_height'] = $source['height']; $source['select_width'] = $source['height'] * ($parameters['width'] / $parameters['height']); } } else { $source['select_height'] = $source['height']; $source['select_width'] = $source['height'] * ($parameters['width'] / $parameters['height']); if ($source['select_width'] > $source['width']) { $source['select_width'] = $source['width']; $source['select_height'] = $source['width'] * ($parameters['height'] / $parameters['width']); } } } } $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } // bug.jpg elseif ($source['width'] >= $parameters['width'] && $source['height'] < $parameters['height']) { $preview['width'] = $parameters['width']; $preview['height'] = $parameters['height']; if ($parameters['crop'] == "n") { $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { if ($parameters['enlarge']) { $source['select_height'] = $source['height']; $source['select_width'] = $source['height'] * ($preview['width'] / $preview['height']); $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { $source['select_width'] = $preview['width']; $source['select_height'] = $source['height']; $preview['select_width'] = $source['select_width']; $preview['select_height'] = $source['select_height']; } } } // flower.jpg elseif ($source['width'] < $parameters['width'] && $source['height'] >= $parameters['height']) { $preview['width'] = $parameters['width']; $preview['height'] = $parameters['height']; if ($parameters['crop'] == "n") { $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { if ($parameters['enlarge']) { $source['select_width'] = $source['width']; $source['select_height'] = $source['width'] * ($preview['height'] / $preview['width']); $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { $source['select_width'] = $source['width']; $source['select_height'] = $preview['height']; $preview['select_width'] = $source['select_width']; $preview['select_height'] = $source['select_height']; } } } // dog.jpg else { $preview['width'] = $parameters['width']; $preview['height'] = $parameters['height']; if ($parameters['enlarge']) { if ($parameters['width'] > $parameters['height']) { $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['width'] * ($source['height'] / $source['width']); } else { $preview['select_height'] = $preview['height']; $preview['select_width'] = $preview['height'] * ($source['width'] / $source['height']); } } else { $preview['select_width'] = $source['width']; $preview['select_height'] = $source['height']; } $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; } } elseif ( $parameters['height'] == "*" ) { if ($source['width'] > $parameters['width']) { // 150x* $preview['width'] = $parameters['width']; if ($parameters['nofit']) { $preview['height'] = $source['height']; $source['select_height'] = $preview['height']; $source['select_width'] = $preview['width']; } else { $preview['height'] = (int) $source['height'] / ( $source['width'] / $parameters['width']); $source['select_width'] = $source['width']; $source['select_height'] = $source['width'] * ($preview['height'] / $preview['width']); } $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { // 150x* $preview['width'] = $parameters['width']; if ($parameters['enlarge']) { $preview['height'] = $source['height'] / ( $source['width'] / $parameters['width']); $source['select_height'] = $source['height']; $source['select_width'] = $source['width']; $preview['select_height'] = $preview['height']; $preview['select_width'] = $preview['width']; } else { $preview['height'] = $source['height']; $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; $preview['select_width'] = $source['width']; $preview['select_height'] = $source['height']; } } } elseif ( $parameters['width'] == "*" ) { if ($source['height'] > $parameters['height']) { // *x100 $preview['height'] = $parameters['height']; if ($parameters['nofit']) { $preview['width'] = $source['width']; $source['select_height'] = $preview['height']; $source['select_width'] = $preview['width']; } else { $preview['width'] = (int) $source['width'] / ( $source['height'] / $parameters['height']); $source['select_height'] = $source['height']; $source['select_width'] = $source['height'] * ($preview['width'] / $preview['height']); } $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['height']; } else { // *x100 $preview['height'] = $parameters['height']; if ($parameters['enlarge']) { $preview['width'] = $source['width'] / ( $source['height'] / $parameters['height']); $source['select_height'] = $source['height']; $source['select_width'] = $source['width']; $preview['select_height'] = $preview['height']; $preview['select_width'] = $preview['width']; } else { $preview['width'] = $source['width']; $source['select_width'] = $source['width']; $source['select_height'] = $source['height']; $preview['select_width'] = $source['width']; $preview['select_height'] = $source['height']; } } } else { error("todo for that images"); } use_position(&$source, &$preview, &$parameters); $preview['image'] = imagecreatetruecolor($preview['width'], $preview['height']); if (preg_match("~^\#([0-9a-f]{6})$~isU", $parameters['fillcolor'])) { eval('$r = 0x'. strtoupper(substr($parameters['fillcolor'],1,2)) . ";"); eval('$g = 0x'. strtoupper(substr($parameters['fillcolor'],3,2)) . ";"); eval('$b = 0x'. strtoupper(substr($parameters['fillcolor'],5,2)) . ";"); } else { switch (strtolower($parameters['fillcolor'])) { case 'black': $r = 0; $g = 0; $b = 0; break; case 'yellow': $r = 255; $g = 230; $b = 55; break; case 'green': $r = 55; $g = 255; $b = 55; break; case 'blue': $r = 55; $g = 230; $b = 255; break; case 'gray': $r = 180; $g = 180; $b = 180; break; case 'white': default: $r = 255; $g = 255; $b = 255; } } @imagefill($preview['image'], 0, 0, imagecolorallocate($preview['image'], $r, $g, $b)); // trigger_error("preview[x]=$preview[x], preview[y]=$preview[y], source[x]=$source[x], source[y]=$source[y], preview[select_width]=$preview[select_width], preview[select_height]=$preview[select_height], source[select_width]=$source[select_width], source[select_height]=$source[select_height]"); @imagecopyresampled($preview['image'], $source['image'], $preview['x'], $preview['y'], $source['x'], $source['y'], $preview['select_width'], $preview['select_height'], $source['select_width'], $source['select_height'] ); @imagejpeg($preview['image'], $result['filename'], 90); if (is_file($result['filename'])) { $mtime = filemtime($source['filename']); touch ( $result['filename'], $mtime, $mtime ); } else { error(); } imagedestroy($source['image']); imagedestroy($preview['image']); return true; } function use_position(&$source, &$preview, &$parameters) { if ($parameters['crop'] == "n") { $preview['x'] = 0; $preview['y'] = 0; $source['x'] = 0; $source['y'] = 0; if ($parameters['width'] != "*" && $parameters['height'] != "*") { // if($source['width'] > $parameters['width'] && $source['height'] > $parameters['height'] ) { $ratio_source = $source['width'] / $source['height']; $ratio_preview = $preview['width'] / $preview['height']; if( $ratio_preview >= $ratio_source ) { $preview['select_height'] = $preview['height']; $preview['select_width'] = $ratio_source * $preview['select_height']; }else{ $preview['select_width'] = $preview['width']; $preview['select_height'] = $preview['select_width'] / $ratio_source; } $preview['x'] = ( $preview['width'] - $preview['select_width']) / 2; $preview['y'] = ( $preview['height'] - $preview['select_height']) / 2; } } return; } if ( $source['width'] > $parameters['width'] ) { if (substr($parameters['crop'],0,1) == "l") { $preview['x'] = 0; $source['x'] = 0; } elseif (substr($parameters['crop'],0,1) == "r") { $preview['x'] = 0; $source['x'] = $source['width'] - $source['select_width']; } else { $preview['x'] = 0; $source['x'] = round(($source['width'] - $source['select_width']) / 2); } } else { if (substr($parameters['crop'],0,1) == "l") { $preview['x'] = 0; $source['x'] = 0; } elseif (substr($parameters['crop'],0,1) == "r") { $preview['x'] = $preview['width'] - $preview['select_width']; $source['x'] = 0; } else { $preview['x'] = round(($preview['width'] - $preview['select_width']) / 2); $source['x'] = round(($source['width'] - $source['select_width']) / 2); } } if ( $source['height'] > $parameters['height'] ) { if (ltrim($parameters['crop'],"lr") == "t") { $preview['y'] = 0; $source['y'] = 0; } elseif (ltrim($parameters['crop'],"lr") == "b") { $preview['y'] = 0; $source['y'] = $source['height'] - $source['select_height']; } else { $preview['y'] = 0; $source['y'] = round(($source['height'] - $source['select_height']) / 2); } } else { if (ltrim($parameters['crop'],"lr") == "t") { $preview['y'] = 0; $source['y'] = 0; } elseif (ltrim($parameters['crop'],"lr") == "b") { $preview['y'] = $preview['height'] - $preview['select_height']; $source['y'] = 0; } else { $preview['y'] = round(($preview['height'] - $preview['select_height']) / 2); $source['y'] = round(($source['height'] - $source['select_height']) / 2); } } return; } function send_image($filename) { $mtime = filemtime($filename); if ( @isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ) { $MODIFIED_SINCE = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']); // ie fix $MODIFIED_SINCE = trim($MODIFIED_SINCE[0]); if ( $MODIFIED_SINCE == date("D, d M Y H:i:s \G\M\T", $mtime) ) { header("HTTP/1.0 304 Not Modified"); exit; } } $image = file_get_contents($filename); $image = substr_replace($image, pack("Cnn", 0x01, 300, 300), 13, 5); header('HTTP/1.1 200 OK'); header('Last-Modified:'. date("D, d M Y H:i:s ", $mtime). "GMT" ); header('Accept-Ranges: bytes' ); header('Content-Length: '.strlen($image) ); header('Content-Type: image/jpeg'); // header('Content-Disposition: attachment; filename="'.basename($file).'"'); // readfile($filename); print $image; exit; } function error($mess="") { if ($mess == "") { header('HTTP/1.1 200 OK'); header('Content-type: image/gif'); header('Last-Modified:'. date('a, d b Y H:i:s GMT') ); print base64_decode('R0lGODlhEAAQAIAAAP/MAP8AACH5BAAAAAAALAAAAAAQABAAAAIahI+pyxYf2oFRAmovzNh2+WXi6GyeCaLkihQAOw=='); } else { print_r($mess); } exit; } ?>