Advertisement
Refs

functions

Dec 1st, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 KB | None | 0 0
  1. <?
  2. $db = new mysqli("localhost", "root", "pass", "db");
  3. function isadmin()
  4. {
  5.     if($_SERVER['REMOTE_ADDR'] == "IP:adress")
  6.     return true;
  7.     return false;
  8. }
  9. function pager($rpp, $count, $href, $minus = 0) {
  10.     $pages = ceil($count / $rpp); // Skaita cik kopaa lapas
  11.     if (!$opts["lastpagedefault"])
  12.         $pagedefault = 0;
  13.     else {
  14.         $pagedefault = floor(($count - 1) / $rpp);
  15.         if ($pagedefault < 0)
  16.             $pagedefault = 0;
  17.     }
  18.  
  19.     if (isset($_GET["p"])) {
  20.         $page = $_GET["p"] - 1;
  21.         if ($page < 0)
  22.             $page = $pagedefault;
  23.     }
  24.     else
  25.         $page = $pagedefault;
  26.     $lastpage = $page;
  27.     if($lastpage > 0)
  28.     {
  29.        $pager = "<div><a href=\"$href".($lastpage)."\">«</a></div>";
  30.       }
  31.       if($lastpage+2 <= $pages)
  32.       {
  33.        $pager2 = "<div><a href=\"$href".($lastpage+2)."\">»</a></div>";
  34.     }  
  35.     if ($count) {
  36.         $pagerarr = array();
  37.         $dotted = 0;
  38.         $dotspace = 3;
  39.         $dotend = $pages+1 - $dotspace;
  40.         $curdotend = $page +1 - $dotspace;
  41.         $curdotstart = $page +1 + $dotspace ;
  42.        
  43.         for ($i = 1; $i <= $pages; $i++) {
  44.             if (($i >= $dotspace && $i <= $curdotend) || ($i >= $curdotstart && $i < $dotend))
  45.             {
  46.                 if (!$dotted)
  47.                 $pagerarr[] = "<div>...</div>";
  48.                 $dotted = 1;
  49.                 continue;
  50.             }
  51.             $dotted = 0;
  52.             $start = $i * $rpp;
  53.             $end = $start + $rpp - 1;
  54.             if ($end > $count)
  55.                 $end = $count;
  56.         $href2 = $href. $i;
  57.              $text = $i;
  58.             if ($i != $page +1)
  59.                 $pagerarr[] = "<div><a href=\"{$href2}\">$text</a></div>";
  60.             else
  61.                 $pagerarr[] = "<div class=\"current\">$text</div>";
  62.  
  63.                   }
  64.                   $pager2 .= "<br style='clear:both;'></br>";
  65.         $pagerstr = join("", $pagerarr);
  66.         $pagertop = "<div id=\"pages\">$pager$pagerstr$pager2</div>\n";
  67.         $pagerbottom = "<div id=\"pages\">$pager$pagerstr$pager2</div>\n";
  68.     }
  69.     else {
  70.         $pagertop = "<div id=\"pages\">$pager$pagerstr$pager2</div>\n";
  71.         $pagerbottom = "<div id=\"pages\">$pager$pagerstr$pager2</div>\n";
  72.     }
  73.  
  74.     $start = $page * $rpp;
  75.     return array($pagertop, $pagerbottom, "LIMIT $start,$rpp");
  76.    
  77. }
  78. function createThumb($sourcefile, $target_file, $new_w, $new_h)
  79. {
  80.   $root = dirname(__FILE__) . "/";
  81.   $source_path = $sourcefile;
  82.  
  83.   //
  84.   // Add file validation code here
  85.   //
  86.  
  87.   list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );
  88.  
  89.   switch ( $source_type )
  90.   {
  91.     case IMAGETYPE_GIF:
  92.       $source_gdim = imagecreatefromgif( $source_path );
  93.       $ext = ".gif";
  94.       $fija = "imagegif";
  95.       break;
  96.  
  97.     case IMAGETYPE_JPEG:
  98.       $source_gdim = imagecreatefromjpeg( $source_path );
  99.       $ext = ".jpg";
  100.       $fija = "imagejpeg";
  101.       break;
  102.  
  103.     case IMAGETYPE_PNG:
  104.       $source_gdim = imagecreatefrompng( $source_path );
  105.       $ext = ".png";
  106.       $fija = "imagepng";
  107.       break;
  108.   }
  109.  
  110.   $source_aspect_ratio = $source_width / $source_height;
  111.   $desired_aspect_ratio = $new_w / $new_h;
  112.  
  113.   if ( $source_aspect_ratio > $desired_aspect_ratio )
  114.   {
  115.     //
  116.     // Triggered when source image is wider
  117.     //
  118.     $temp_height = $new_h;
  119.     $temp_width = ( int ) ( $new_h * $source_aspect_ratio );
  120.   }
  121.   else
  122.   {
  123.     //
  124.     // Triggered otherwise (i.e. source image is similar or taller)
  125.     //
  126.     $temp_width = $new_w;
  127.     $temp_height = ( int ) ( $new_w / $source_aspect_ratio );
  128.   }
  129.  
  130.   //
  131.   // Resize the image into a temporary GD image
  132.   //
  133.  
  134.   $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  135.  imagealphablending($temp_gdim, false);
  136.  imagesavealpha($temp_gdim,true);
  137.  $transparent = imagecolorallocatealpha($temp_gdim, 255, 255, 255, 127);
  138.  imagefilledrectangle($temp_gdim, 0, 0, $temp_width,$temp_height, $transparent);
  139.  
  140.   imagecopyresampled(
  141.     $temp_gdim,
  142.     $source_gdim,
  143.     0, 0,
  144.     0, 0,
  145.     $temp_width, $temp_height,
  146.     $source_width, $source_height
  147.   );
  148.  
  149.   //
  150.   // Copy cropped region from temporary image into the desired GD image
  151.   //
  152.  
  153.   $x0 = ( $temp_width - $new_w ) / 2;
  154.   $y0 = ( $temp_height - $new_h ) / 2;
  155.  
  156.   $desired_gdim = imagecreatetruecolor( $new_w, $new_h );
  157.    imagealphablending($desired_gdim, false);
  158.  imagesavealpha($desired_gdim,true);
  159.  $transparent = imagecolorallocatealpha($desired_gdim, 255, 255, 255, 127);
  160.  imagefilledrectangle($desired_gdim, 0, 0, $new_w,$new_h, $transparent);
  161.   imagecopy(
  162.     $desired_gdim,
  163.     $temp_gdim,
  164.     0, 0,
  165.     $x0, $y0,
  166.     $new_w, $new_h
  167.   );
  168.  
  169.   //
  170.   // Render the image
  171.   // Alternatively, you can save the image in file-system or database
  172.   //
  173.  
  174.  
  175.   if($fija($desired_gdim, $root.$target_file.$ext)) {
  176. imagedestroy($desired_gdim );
  177. imagedestroy($temp_gdim);
  178. }
  179. return $ext;
  180.  
  181.   //
  182.   // Add clean-up code here
  183.   //
  184. }
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement