За последние 24 часа нас посетили 15458 программистов и 1620 роботов. Сейчас ищут 1106 программистов ...

Оцениваем класс работы с картинками

Тема в разделе "Обработка изображений средствами PHP", создана пользователем Danilka, 21 ноя 2007.

  1. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    Собственно сам класс (обновлено):
    PHP:
    1. <?php
    2. class IMG
    3. {
    4.  
    5. protected $max_side_size = 1000000;
    6. protected $width    = 0;
    7. protected $height   = 0;
    8. protected $type     = NULL;
    9. protected $image    = NULL;
    10. protected $error    = NULL;
    11. protected $quality  = 95;
    12. protected $gd_ver   = 1;
    13.  
    14. function __construct()
    15. {
    16.     if( !$gd = get_extension_funcs( 'gd' ) )
    17.     {
    18.         $this -> error = 1;
    19.     }
    20.     else
    21.     {
    22.         if( in_array( 'imagegd2' , $gd ) )  $this -> gd_ver = 1;
    23.         else    $this -> gd_ver = 2;
    24.     }
    25. }
    26.  
    27. public function get_w()
    28. {
    29.     return( $this->width );
    30. }
    31.  
    32. public function get_h()
    33. {
    34.     return( $this->height );
    35. }
    36.  
    37. public function get_image()
    38. {
    39.     return( $this->image );
    40. }
    41.  
    42. protected function hex2rgb( $color )
    43. {
    44.     $color = str_replace( '#' , '' , $color );
    45.     return( array( 1 => hexdec('0x'.$color{0}.$color{1}) , 2 => hexdec('0x'.$color{2}.$color{3}) , 3 => hexdec('0x'.$color{4}.$color{5}) ) );
    46. }
    47.  
    48. protected function get_html_color( $color )
    49. {
    50.     $color = $this -> hex2rgb( $color );
    51.     $color = imagecolorallocate( $this->image , $color[1] , $color[2] , $color[3] );
    52.     return( $color );
    53. }
    54.  
    55. public function create_blank( $width , $height , $bg_color )
    56. {
    57.     $width = (int) $width;
    58.     $height = (int) $height;
    59.     if( $width < 1 )    $width = 1;
    60.     if( $height < 1 )   $height = 1;
    61.     if( $height > $this->max_side_size || $width > $this->max_side_size )
    62.     {
    63.         return( NULL );
    64.     }
    65.  
    66.     if( $this -> image = imagecreatetruecolor( $width , $height ) )
    67.     {
    68.         if( $color = $this -> get_html_color( $bg_color ) )
    69.         {
    70.             imagefill( $this->image , 0 , 0 , $color );
    71.         }
    72.         $this -> width = $width;
    73.         $this -> height = $height;
    74.         return( 1 );
    75.     }
    76.     else
    77.     {
    78.         $this -> error = 1;
    79.         return( NULL );
    80.     }
    81. }
    82.  
    83. public function put_text( $text , $font , $x_pos , $y_pos , $color )
    84. {
    85.     if( !$this -> image )   return( NULL );
    86.  
    87.     $w = $this -> width;
    88.     $h = $this -> height;
    89.  
    90.     ### generating X position ###
    91.     if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center )
    92.     {
    93.         $px = (int) ( $w / 2 );
    94.     }
    95.     else
    96.     {
    97.         if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 )
    98.         {
    99.             $x_pos = (int) $x_pos;
    100.             if( $x_pos >= $w )  $x_pos = 0;
    101.             $px = $x_pos;
    102.         }
    103.         else
    104.         {
    105.             $x_pos = (int) $x_pos;
    106.             if( $x_pos < 0 )
    107.             {
    108.                 if( $x_pos < -$w )  $px = $w ;
    109.                 else            $px = $w + $x_pos;
    110.             }
    111.             else
    112.             {
    113.                 $px = $w;
    114.             }
    115.         }
    116.     }
    117.  
    118.     ### generating Y position ###
    119.     if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center )
    120.     {
    121.         $py = (int) ( $h / 2 );
    122.     }
    123.     else
    124.     {
    125.         if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 )
    126.         {
    127.             $y_pos = (int) $y_pos;
    128.             if( $y_pos >= $h )  $y_pos = 0;
    129.             $py = $y_pos;
    130.         }
    131.         else
    132.         {
    133.             $y_pos = (int) $y_pos;
    134.             if( $y_pos < 0 )
    135.             {
    136.                 if( $y_pos < -$h )  $py = $h;
    137.                 else            $py = $h + $y_pos;
    138.             }
    139.             else
    140.             {
    141.                 $py = $h;
    142.             }
    143.         }
    144.     }
    145.  
    146.     $color = $this -> get_html_color( $color );
    147.     return( imagestring( $this->image , $font , $px , $py , $text , $color ) );
    148. }
    149.  
    150. public function put_image( $p_image , $x_pos , $y_pos )
    151. {
    152.     if( !$p_image ) return( NULL );
    153.     if( !$this -> image )   return( NULL );
    154.     $pw = imagesx( $p_image );
    155.     $ph = imagesy( $p_image );
    156.     $w = $this -> width;
    157.     $h = $this -> height;
    158.     if( !$ph || !$pw || $ph < 1 || $pw < 1 || $ph > $this->max_side_size || $pw > $this->max_side_size )    return( NULL );
    159.  
    160.     ### generating X position ###
    161.     if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center )
    162.     {
    163.         $px = (int) ( ( $w - $pw ) / 2 );
    164.     }
    165.     else
    166.     {
    167.         if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 )
    168.         {
    169.             $x_pos = (int) $x_pos;
    170.             if( $x_pos >= $w )  $x_pos = 0;
    171.             $px = $x_pos;
    172.         }
    173.         else
    174.         {
    175.             $x_pos = (int) $x_pos;
    176.             if( $x_pos < 0 )
    177.             {
    178.                 if( $x_pos < -$w )  $px = $w - $pw;
    179.                 else            $px = $w - $pw + $x_pos;
    180.             }
    181.             else
    182.             {
    183.                 $px = $w - $pw;
    184.             }
    185.         }
    186.     }
    187.  
    188.     ### generating Y position ###
    189.     if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center )
    190.     {
    191.         $py = (int) ( ( $h - $ph ) / 2 );
    192.     }
    193.     else
    194.     {
    195.         if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 )
    196.         {
    197.             $y_pos = (int) $y_pos;
    198.             if( $y_pos >= $h )  $y_pos = 0;
    199.             $py = $y_pos;
    200.         }
    201.         else
    202.         {
    203.             $y_pos = (int) $y_pos;
    204.             if( $y_pos < 0 )
    205.             {
    206.                 if( $y_pos < -$h )  $py = $h - $ph;
    207.                 else            $py = $h - $ph + $y_pos;
    208.             }
    209.             else
    210.             {
    211.                 $py = $h - $ph;
    212.             }
    213.         }
    214.     }
    215.  
    216.     if( imagecopyresized( $this->image , $p_image , $px , $py , 0 , 0 , $pw , $ph , $pw , $ph ) )   return( 1 );
    217.     else    return( NULL );
    218. }
    219.  
    220. public function open( $file_name )
    221. {
    222.     if( is_file( $file_name ) )
    223.     {
    224.         list( $width , $height , $type ) = getimagesize( $file_name );
    225.  
    226.         switch( $type )
    227.         {
    228.             case 1 :
    229.                 if( $tmp_image = imagecreatefromgif( $file_name ) )
    230.                 {
    231.                     $this -> image  = $tmp_image;
    232.                     $this -> width  = $width;
    233.                     $this -> height = $height;
    234.                     return( 1 );
    235.                 }
    236.                 else    return( NULL );
    237.             break;
    238.  
    239.             case 2 :
    240.                 if( $tmp_image = imagecreatefromjpeg( $file_name ) )
    241.                 {
    242.                     $this -> image  = $tmp_image;
    243.                     $this -> width  = $width;
    244.                     $this -> height = $height;
    245.                     return( 1 );
    246.                 }
    247.                 else    return( NULL );
    248.             break;
    249.  
    250.             case 3 :
    251.                 if( $tmp_image = imagecreatefrompng( $file_name ) )
    252.                 {
    253.                     $this -> image  = $tmp_image;
    254.                     $this -> width  = $width;
    255.                     $this -> height = $height;
    256.                     return( 1 );
    257.                 }
    258.                 else    return( NULL );
    259.             break;
    260.  
    261.             default :
    262.                 return( NULL );
    263.             break;
    264.         }
    265.     }
    266.     else
    267.     {
    268.         return( NULL );
    269.     }
    270. }
    271.  
    272. public function open_post( $name )
    273. {
    274.     if( !isset( $_FILES[ $name ] ) )    return( NULL );
    275.     return( $this -> open( $_FILES[$name]['tmp_name'] ) );
    276. }
    277.  
    278. public function open_string( $string )
    279. {
    280.     if( $string )
    281.     {
    282.         if( $tmp_image = imagecreatefromstring( $string ) )
    283.         {
    284.             $this -> image  = $tmp_image;
    285.             $this -> width  = imagesx( $this->image );
    286.             $this -> height = imagesy( $this->image );
    287.             return( 1 );
    288.         }
    289.         else    return( NULL );
    290.     }
    291.     else    return( NULL );
    292. }
    293.  
    294. public function crop( $x , $y , $width , $height )
    295. {
    296.     if( !$this -> image )   return( NULL );
    297.     if( $width < 1 )    $width = 1;
    298.     if( $height < 1 )   $height = 1;
    299.     if( $height > $this->max_side_size || $width > $this->max_side_size )
    300.     {
    301.         return( NULL );
    302.     }
    303.  
    304.     if( $temp_image = imagecreatetruecolor( $width , $height ) )
    305.     {
    306.         if( imagecopyresized ( $temp_image , $this->image , 0 , 0 , $x , $y , $width , $height , $width , $height ) )
    307.         {
    308.             $this -> image = $temp_image;
    309.             $this -> width = $width;
    310.             $this -> height = $height;
    311.         }
    312.     }
    313.     else return( NULL );
    314. }
    315.  
    316. public function resize_w( $new_size )
    317. {
    318.     if( !$this->image ) return( NULL );
    319.     $new_size = (int) $new_size;
    320.     if( $new_size < 1 || $new_size > $this->max_side_size ) return( NULL );
    321.     $w = $this -> width;
    322.     $h = $this -> height;
    323.  
    324.     $nw = $new_size;
    325.     if( $nw >= $w ) return( NULL );
    326.     $ratio = $w / $nw;
    327.     $nh = (int) ( $h / $ratio );
    328.     $temp_image = imagecreatetruecolor( $nw , $nh );
    329.     if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) )
    330.     {
    331.         $this -> width  = $nw;
    332.         $this -> height = $nh;
    333.         $this -> image  = $temp_image;
    334.         return( 1 );
    335.     }
    336.     else
    337.     {
    338.         imagedestroy( $temp_image );
    339.         return( NULL );
    340.     }
    341. }
    342.  
    343. public function resize_h( $new_size )
    344. {
    345.     if( !$this->image ) return( NULL );
    346.     $new_size = (int) $new_size;
    347.     if( $new_size < 1 || $new_size > $this->max_side_size ) return( NULL );
    348.     $w = $this -> width;
    349.     $h = $this -> height;
    350.  
    351.     $nh = $new_size;
    352.     if( $nh >= $h ) return( NULL );
    353.     $ratio = $h / $nh;
    354.     $nw = (int) ( $w / $ratio );
    355.     $temp_image = imagecreatetruecolor( $nw , $nh );
    356.     if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) )
    357.     {
    358.         $this -> width  = $nw;
    359.         $this -> height = $nh;
    360.         $this -> image  = $temp_image;
    361.         return( 1 );
    362.     }
    363.     else
    364.     {
    365.         imagedestroy( $temp_image );
    366.         return( NULL );
    367.     }
    368. }
    369.  
    370. public function set_quality( $quality )
    371. {
    372.     $quality = (int) $quality;
    373.     if( $quality > 100 )    $quality = 100;
    374.     if( $quality < 0 )  $quality = 0;
    375.     $this -> quality = $quality;
    376. }
    377.  
    378. public function out_jpg( $file_name )
    379. {
    380.     if( !$this->image ) return( NULL );
    381.     if( $file_name )
    382.     {
    383.         imagejpeg( $this->image , $file_name , $this -> quality );
    384.     }
    385.     else
    386.     {
    387.         header( 'Content-type: image/jpeg' );
    388.         imagejpeg( $this ->image );
    389.     }
    390. }
    391.  
    392. public function out_gif( $file_name )
    393. {
    394.     if( !$this->image ) return( NULL );
    395.     if( $file_name )
    396.     {
    397.         imagepng( $this->image , $file_name );
    398.     }
    399.     else
    400.     {
    401.         header( 'Content-type: image/gif' );
    402.         imagepng( $this->image );
    403.     }
    404. }
    405.  
    406. public function out_png( $file_name )
    407. {
    408.     if( !$this->image ) return( NULL );
    409.     if( $file_name )
    410.     {
    411.         $quality = (int) ($this->quality / 10);
    412.         if( $quality > 9 )  $quality = 9;
    413.         imagepng( $this->image , $file_name , $quality );
    414.     }
    415.     else
    416.     {
    417.         header( 'Content-type: image/png' );
    418.         imagepng( $this->image );
    419.     }
    420. }
    421.  
    422. }### end of IMG
    423.  
    Пример работы:
    Было
    [​IMG]
    Стало
    [​IMG]

    Вот кусок описывающий все операции над картинкой:
    PHP:
    1. $img = new IMG();
    2. $img -> open( 'kvn_v_kepke.jpg' );
    3. $img -> resize_h( 300 );
    4. $img -> crop( 32 , 25 , $img->get_w()-87 , $img->get_h()-60 );
    5.  
    6. $fon = new IMG();
    7. $fon -> create_blank( $img->get_w()+4 , $img->get_h()+4 , '#60b5a2' );
    8. $fon -> put_image( $img->get_image() , 'center' , 'center' );
    9. $fon -> put_text( 'Voffka - cowboy!' , 3 , 25 , -18 , '#60b5a2' );
    10. $fon -> out_png( NULL );
     
  2. dark-demon

    dark-demon Активный пользователь

    С нами с:
    16 фев 2007
    Сообщения:
    1.920
    Симпатии:
    1
    Адрес:
    леноград
    лучше так:

    $img = new IMG( 'kvn_v_kepke.jpg' );
    $img -> resizeAH( 300 );
    $img -> crop( 32 , 25 , -87 , -60 );
     
  3. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    Да, резонно, тогда не нужно делать разбор, по какой стороне ресайзить. Респект!

    А вот по поводу кропа, тут наверное просто нужно договорить. Предпологается на яваскрипте написать кусок, чтобы рамкой выбирался кроп. И там будет лучше юзать так, как сейчас реализовано, по сторонам прямоугольника.

    Какие ещё будут коменты?
     
  4. antonn

    antonn Активный пользователь

    С нами с:
    10 июн 2007
    Сообщения:
    2.996
    Симпатии:
    0
    может лучше imagettftext() ?
     
  5. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    А чем лучше?
    Если дело только в угле, то не думаю. Так как он будет использоваться редко, функции с переменным количествои параметров не поддерживаются и придётся каждый раз писать линий ноль, что в конце концов приведёт к тому, что забудешь, для чего он здесь вообще.
    У мыня как-то давно такая история была с функцией фильтрации текста перед отображением. Наворотил целую кучу, потом одни ноли в неё передовал.

    Если дело не только в угле, тогда что ещё?
     
  6. antonn

    antonn Активный пользователь

    С нами с:
    10 июн 2007
    Сообщения:
    2.996
    Симпатии:
    0
    можно использовать разные шрифты (в смысле свои ttf). Да и проверить нужно, что то помнится в какой то функции были проблемы с кирилицей, но не помню в какой :)
     
  7. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    В моей :), как оказалось.
    А если ей шрифт не передать, она дефолтный заюзает?
     
  8. Ganzal

    Ganzal Суперстар
    Команда форума Модератор

    С нами с:
    15 мар 2007
    Сообщения:
    9.893
    Симпатии:
    965
    Код (Text):
    1.  $fon -> put_image( $img->get_image() , 'center' , 'center' );
    может лучше для горизонтали оставить left/center/right а для вертикали top/middle/bottom? а то я вижу 2 раза center и мне приходится смотреть в класс чтобы понять на каком месте какое измерение стоит
     
  9. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    Ganzal, согласен.
    Исправлять в своей версии не буду, ибо не привык к такому разделению.
    Но за подсказку спасибо.
     
  10. Koc

    Koc Активный пользователь

    С нами с:
    3 мар 2008
    Сообщения:
    2.253
    Симпатии:
    0
    Адрес:
    \Ukraine\Dnepropetrovsk
    да, если есть обновления - выкладывай
     
  11. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    PHP:
    1.  
    2. ############################
    3. ## v.2.1 from 19 Dec 2008 ##
    4. ############################
    5.  
    6. class IMG
    7. {
    8.  
    9. private $max_side_size = 10000;
    10. private $width  = 0;
    11. private $height = 0;
    12. private $type       = null;
    13. private $image  = null;
    14. private $error  = null;
    15. private $quality    = 90;
    16.  
    17. function __construct()
    18. {
    19. }
    20.  
    21. public function check()
    22. {
    23.     if( $this->image && $this->width > 0 && $this->height > 0 ) return true;
    24.     else    return false;
    25. }
    26.  
    27. public function w()
    28. {
    29.     return( $this->width );
    30. }
    31.  
    32. public function h()
    33. {
    34.     return( $this->height );
    35. }
    36.  
    37. public function get_image()
    38. {
    39.     return( $this->image );
    40. }
    41.  
    42. private function hex2rgb( $color )
    43. {
    44.     $color = str_replace( '#' , '' , $color );
    45.     return( array( 1 => hexdec('0x'.$color{0}.$color{1}) , 2 => hexdec('0x'.$color{2}.$color{3}) , 3 => hexdec('0x'.$color{4}.$color{5}) ) );
    46. }
    47.  
    48. private function get_html_color( $color )
    49. {
    50.     $color = $this -> hex2rgb( $color );
    51.     $color = imagecolorallocate( $this->image , $color[1] , $color[2] , $color[3] );
    52.     return( $color );
    53. }
    54.  
    55. public function create_blank( $width , $height , $bg_color )
    56. {
    57.     $width = (int) $width;
    58.     $height = (int) $height;
    59.     if( $width < 1 )    $width = 1;
    60.     if( $height < 1 )   $height = 1;
    61.     if( $height > $this->max_side_size || $width > $this->max_side_size )
    62.     {
    63.         return false;
    64.     }
    65.  
    66.     if( $this -> image = imagecreatetruecolor( $width , $height ) )
    67.     {
    68.         if( $color = $this -> get_html_color( $bg_color ) )
    69.         {
    70.             imagefill( $this->image , 0 , 0 , $color );
    71.         }
    72.         $this -> width = $width;
    73.         $this -> height = $height;
    74.         return true;
    75.     }
    76.     else
    77.     {
    78.         $this -> error = true;
    79.         return false;
    80.     }
    81. }
    82.  
    83. public function put_text( $text , $font , $x_pos , $y_pos , $color )
    84. {
    85.     if( !$this -> image )   return false;
    86.  
    87.     $w = $this -> width;
    88.     $h = $this -> height;
    89.  
    90.     ### generating X position ###
    91.     if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center )
    92.     {
    93.         $px = (int) ( $w / 2 );
    94.     }
    95.     else
    96.     {
    97.         if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 )
    98.         {
    99.             $x_pos = (int) $x_pos;
    100.             if( $x_pos >= $w )  $x_pos = 0;
    101.             $px = $x_pos;
    102.         }
    103.         else
    104.         {
    105.             $x_pos = (int) $x_pos;
    106.             if( $x_pos < 0 )
    107.             {
    108.                 if( $x_pos < -$w )  $px = $w ;
    109.                 else            $px = $w + $x_pos;
    110.             }
    111.             else
    112.             {
    113.                 $px = $w;
    114.             }
    115.         }
    116.     }
    117.  
    118.     ### generating Y position ###
    119.     if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center )
    120.     {
    121.         $py = (int) ( $h / 2 );
    122.     }
    123.     else
    124.     {
    125.         if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 )
    126.         {
    127.             $y_pos = (int) $y_pos;
    128.             if( $y_pos >= $h )  $y_pos = 0;
    129.             $py = $y_pos;
    130.         }
    131.         else
    132.         {
    133.             $y_pos = (int) $y_pos;
    134.             if( $y_pos < 0 )
    135.             {
    136.                 if( $y_pos < -$h )  $py = $h;
    137.                 else            $py = $h + $y_pos;
    138.             }
    139.             else
    140.             {
    141.                 $py = $h;
    142.             }
    143.         }
    144.     }
    145.  
    146.     $color = $this -> get_html_color( $color );
    147.     return( imagestring( $this->image , $font , $px , $py , $text , $color ) );
    148. }
    149.  
    150. public function put_image( $p_image, $x_pos=0, $y_pos=0 )
    151. {
    152.     if( !$p_image ) return false;
    153.     if( !$this -> image )   return false;
    154.     $pw = imagesx( $p_image );
    155.     $ph = imagesy( $p_image );
    156.     $w = $this -> width;
    157.     $h = $this -> height;
    158.     if( !$ph || !$pw || $ph < 1 || $pw < 1 || $ph > $this->max_side_size || $pw > $this->max_side_size )    return false;
    159.  
    160.     if( imagecopyresized( $this->image , $p_image , $x_pos , $y_pos , 0 , 0 , $pw , $ph , $pw , $ph ) ) return true;
    161.     else    return false;
    162. }
    163.  
    164. public function open( $file_name )
    165. {
    166.     if( is_file( $file_name ) )
    167.     {
    168.         list( $width , $height , $type ) = getimagesize( $file_name );
    169.  
    170.         switch( $type )
    171.         {
    172.             case 1 :
    173.                 if( $tmp_image = imagecreatefromgif( $file_name ) )
    174.                 {
    175.                     $this -> image  = $tmp_image;
    176.                     $this -> width  = $width;
    177.                     $this -> height = $height;
    178.                     return true;
    179.                 }
    180.                 else    return false;
    181.             break;
    182.  
    183.             case 2 :
    184.                 if( $tmp_image = imagecreatefromjpeg( $file_name ) )
    185.                 {
    186.                     $this -> image  = $tmp_image;
    187.                     $this -> width  = $width;
    188.                     $this -> height = $height;
    189.                     return true;
    190.                 }
    191.                 else    return false;
    192.             break;
    193.  
    194.             case 3 :
    195.                 if( $tmp_image = imagecreatefrompng( $file_name ) )
    196.                 {
    197.                     $this -> image  = $tmp_image;
    198.                     $this -> width  = $width;
    199.                     $this -> height = $height;
    200.                     return true;
    201.                 }
    202.                 else    return false;
    203.             break;
    204.  
    205.             default :
    206.                 return false;
    207.             break;
    208.         }
    209.     }
    210.     else
    211.     {
    212.         return false;
    213.     }
    214. }
    215.  
    216. public function open_post( $name )
    217. {
    218.     if( !isset( $_FILES[ $name ] ) )    return false;
    219.     return( $this -> open( $_FILES[$name]['tmp_name'] ) );
    220. }
    221.  
    222. public function open_data( $string )
    223. {
    224.     if( $string )
    225.     {
    226.         if( $tmp_image = imagecreatefromstring( $string ) )
    227.         {
    228.             $this -> image  = $tmp_image;
    229.             $this -> width  = imagesx( $this->image );
    230.             $this -> height = imagesy( $this->image );
    231.             return true;
    232.         }
    233.         else    return false;
    234.     }
    235.     else    return false;
    236. }
    237.  
    238. public function crop( $x , $y , $width , $height )
    239. {
    240.     if( !$this -> image )   return false;
    241.     if( $width < 1 )    $width = 1;
    242.     if( $height < 1 )   $height = 1;
    243.     if( $height > $this->max_side_size || $width > $this->max_side_size )
    244.     {
    245.         return false;
    246.     }
    247.  
    248.     if( $temp_image = imagecreatetruecolor( $width , $height ) )
    249.     {
    250.         if( imagecopyresized ( $temp_image , $this->image , 0 , 0 , $x , $y , $width , $height , $width , $height ) )
    251.         {
    252.             $this -> image = $temp_image;
    253.             $this -> width = $width;
    254.             $this -> height = $height;
    255.         }
    256.     }
    257.     else return false;
    258. }
    259.  
    260. public function resize_w( $new_size )
    261. {
    262.     if( !$this->image ) return false;
    263.     $new_size = (int) $new_size;
    264. // Если это раскоментировать, то не будет давать растягивать изображения, а только сжимать
    265. //  if( $new_size < 1 || $new_size > $this->max_side_size ) return false;
    266.     $w = $this -> width;
    267.     $h = $this -> height;
    268.  
    269.     $nw = $new_size;
    270. // Если это раскоментировать, то не будет давать растягивать изображения, а только сжимать
    271. //  if( $nw >= $w ) return false;
    272.     $ratio = $w / $nw;
    273.     $nh = (int) ( $h / $ratio );
    274.     $temp_image = imagecreatetruecolor( $nw , $nh );
    275.     if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) )
    276.     {
    277.         $this -> width  = $nw;
    278.         $this -> height = $nh;
    279.         $this -> image  = $temp_image;
    280.         return true;
    281.     }
    282.     else
    283.     {
    284.         imagedestroy( $temp_image );
    285.         return false;
    286.     }
    287. }
    288.  
    289. public function resize_h( $new_size )
    290. {
    291.     if( !$this->image ) return false;
    292.     $new_size = (int) $new_size;
    293.     if( $new_size < 1 || $new_size > $this->max_side_size ) return false;
    294.     $w = $this -> width;
    295.     $h = $this -> height;
    296.  
    297.     $nh = $new_size;
    298.     if( $nh >= $h ) return false;
    299.     $ratio = $h / $nh;
    300.     $nw = (int) ( $w / $ratio );
    301.     $temp_image = imagecreatetruecolor( $nw , $nh );
    302.     if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) )
    303.     {
    304.         $this -> width  = $nw;
    305.         $this -> height = $nh;
    306.         $this -> image  = $temp_image;
    307.         return true;
    308.     }
    309.     else
    310.     {
    311.         imagedestroy( $temp_image );
    312.         return false;
    313.     }
    314. }
    315.  
    316. public function resize( $new_side )
    317. {
    318.     if( $this->width > $this->height )
    319.         return $this -> resize_w( $new_side );
    320.     else
    321.         return $this -> resize_h( $new_side );
    322. }
    323.  
    324. public function quality( $quality = 75 )
    325. {
    326.     $quality = (int) $quality;
    327.     if( $quality > 100 )    $quality = 100;
    328.     if( $quality < 0 )  $quality = 0;
    329.     $this -> quality = $quality;
    330. }
    331.  
    332. public function save_jpg( $file_name = null )
    333. {
    334.     if( !$this->image ) return false;
    335.     if( $file_name )
    336.     {
    337.         imagejpeg( $this->image , $file_name , $this -> quality );
    338.     }
    339.     else
    340.     {
    341.         header( 'Content-type: image/jpeg' );
    342.         imagejpeg( $this ->image, null, $this -> quality );
    343.     }
    344.     return true;
    345. }
    346.  
    347. public function save_gif( $file_name = null )
    348. {
    349.     if( !$this->image ) return false;
    350.     if( $file_name )
    351.     {
    352.         imagepng( $this->image , $file_name );
    353.     }
    354.     else
    355.     {
    356.         header( 'Content-type: image/gif' );
    357.         imagepng( $this->image );
    358.     }
    359.     return true;
    360. }
    361.  
    362. public function save_png( $file_name = null )
    363. {
    364.     if( !$this->image ) return false;
    365.     if( $file_name )
    366.     {
    367.         $quality = (int) ($this->quality / 10);
    368.         if( $quality > 9 )  $quality = 9;
    369.         imagepng( $this->image , $file_name , $quality );
    370.     }
    371.     else
    372.     {
    373.         header( 'Content-type: image/png' );
    374.         imagepng( $this->image );
    375.     }
    376.     return true;
    377. }
    378.  
    379. }### end of IMG
    380. ?>
    381.  
     
  12. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    Недостаток, как уже было упомянуто в добавлении текста кирилицей. Но мне текст не нужен, так что не заморачиваюсь.
    Врятли ещё будет много обновлений, так как собираюсь переходить на ИмэйджМэджик.

    p.s. Кос, приятно тебя читать. Как дела? Шаблонку закинул или пользуешь. Я там тоже что-то вроде подправлял малец, но html кэша так и не сделал.
     
  13. Koc

    Koc Активный пользователь

    С нами с:
    3 мар 2008
    Сообщения:
    2.253
    Симпатии:
    0
    Адрес:
    \Ukraine\Dnepropetrovsk
    Danilka
    деревья выводить при помощи нее таки можно! Очень клево и удобно ей пользоваться. Как будет свободное время - залью на code.google, может че-нить доделаю в ней.
     
  14. Victor Bazinov

    Victor Bazinov Активный пользователь

    С нами с:
    30 окт 2007
    Сообщения:
    69
    Симпатии:
    0
    а деструктор где?)
    играюсь с классом, постоянно памяти не хватает :))
     
  15. sylex

    sylex Активный пользователь

    С нами с:
    9 ноя 2008
    Сообщения:
    625
    Симпатии:
    0
    Адрес:
    Омск
    и комменты бы описал, какие входные, выходные данные
     
  16. Danilka

    Danilka Активный пользователь

    С нами с:
    8 ноя 2007
    Сообщения:
    192
    Симпатии:
    0
    Victor Bazinov unset( $img );
    sylex Для себя писал, да и коментить там нечего.