<?php
/**
 * Класс выводит большую или маленькую фотку юзера из СУБД
 * *
 * (c) Дмитрий Куринский, 2006.
 * Пространство имён: xmlns:mr=http://www.mirari.ru
 */

    
class mr_userphoto{
 
 static public function 
tiny($login)
 {
  
uses::module("mr_db");

  
$id mr_db::fetch("SELECT id FROM mr_users WHERE login='$login' LIMIT 1"mr_db::get);
  if(!
$id) exit;

  
$f mr_db::fetch("SELECT type, image FROM mr_user_photos_thumbnail WHERE user_id=$id LIMIT 1"mr_db::obj);
  if(!
is_object($f))
  {
      
$f mr_db::fetch("SELECT type, image FROM mr_user_photos WHERE user_id=$id LIMIT 1"mr_db::obj);
      if(!
is_object($f))
      exit;
      
$tmp tempnam(".""tinyph");
      
file_put_contents($tmp$f->image);
      list(
$width$height$type, ) = getimagesize($tmp);
      
$w_max 120;
      
$h_max 120;
      
#Если уменьшать не надо
      
if($width <= $w_max && $height <= $h_max)
      {

          
mr_db::query("INSERT INTO mr_user_photos_thumbnail(id, width, height, type, image) VALUES($id$width$height, '$type', '".mysql_escape_string($f->image)."')");
          
$image $f->image;

      
#А тут приходится уменьшать
      
} else {
          
$k $width/$w_max>$height/$h_max $width/$w_max $height/$h_max;
          switch(
$type)
          {
              case 
IMAGETYPE_GIF$im imagecreatefromgif($tmp); break;
              case 
IMAGETYPE_JPEG$im imagecreatefromjpeg($tmp); break;
              case 
IMAGETYPE_PNG$im imagecreatefrompng($tmp); break;
          }
          
$newim imagecreatetruecolor(round($width/$k), round($height/$k));
          
imagecopyresized($newim$im0000imagesx($newim), imagesy($newim), $width$height);
          
imagedestroy($im);

          switch(
$type)
          {
              case 
IMAGETYPE_GIFimagegif($newim$tmp); break;
              case 
IMAGETYPE_JPEGimagejpeg($newim$tmp); break;
              case 
IMAGETYPE_PNGimagepng($newim$tmp); break;
          }

          
$image file_get_contents($tmp);
          
$type image_type_to_mime_type($type);
          
mr_db::query("INSERT INTO mr_user_photos_thumbnail(user_id, width, height, type, image) VALUES($id, ".imagesx($newim).", ".imagesy($newim).", '".image_type_to_mime_type($type)."', '".mysql_escape_string($image)."')");

          
imagedestroy($newim);
      }

      
unlink($tmp);
  } else list(
$image$type) = array($f->image$f->type);
  

  
header("Content-type: ".$type);

  print 
$image;

  exit;  
 }
 
 static public function 
full($login)
 {
  
uses::module("mr_db");

  
$id mr_db::fetch("SELECT id FROM mr_users WHERE login='$login' LIMIT 1"mr_db::get);
  if(!
$id) exit;

  
$f mr_db::fetch("SELECT type, image FROM mr_user_photos WHERE user_id=$id LIMIT 1"mr_db::obj);
  if(!
is_object($f))
   exit;

  
header("Content-type: ".$f->type);

  print 
$f->image;

  exit;
 }
    }
?>