Собственно сам класс (обновлено): PHP: <?php class IMG { protected $max_side_size = 1000000; protected $width = 0; protected $height = 0; protected $type = NULL; protected $image = NULL; protected $error = NULL; protected $quality = 95; protected $gd_ver = 1; function __construct() { if( !$gd = get_extension_funcs( 'gd' ) ) { $this -> error = 1; } else { if( in_array( 'imagegd2' , $gd ) ) $this -> gd_ver = 1; else $this -> gd_ver = 2; } } public function get_w() { return( $this->width ); } public function get_h() { return( $this->height ); } public function get_image() { return( $this->image ); } protected function hex2rgb( $color ) { $color = str_replace( '#' , '' , $color ); return( array( 1 => hexdec('0x'.$color{0}.$color{1}) , 2 => hexdec('0x'.$color{2}.$color{3}) , 3 => hexdec('0x'.$color{4}.$color{5}) ) ); } protected function get_html_color( $color ) { $color = $this -> hex2rgb( $color ); $color = imagecolorallocate( $this->image , $color[1] , $color[2] , $color[3] ); return( $color ); } public function create_blank( $width , $height , $bg_color ) { $width = (int) $width; $height = (int) $height; if( $width < 1 ) $width = 1; if( $height < 1 ) $height = 1; if( $height > $this->max_side_size || $width > $this->max_side_size ) { return( NULL ); } if( $this -> image = imagecreatetruecolor( $width , $height ) ) { if( $color = $this -> get_html_color( $bg_color ) ) { imagefill( $this->image , 0 , 0 , $color ); } $this -> width = $width; $this -> height = $height; return( 1 ); } else { $this -> error = 1; return( NULL ); } } public function put_text( $text , $font , $x_pos , $y_pos , $color ) { if( !$this -> image ) return( NULL ); $w = $this -> width; $h = $this -> height; ### generating X position ### if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center ) { $px = (int) ( $w / 2 ); } else { if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 ) { $x_pos = (int) $x_pos; if( $x_pos >= $w ) $x_pos = 0; $px = $x_pos; } else { $x_pos = (int) $x_pos; if( $x_pos < 0 ) { if( $x_pos < -$w ) $px = $w ; else $px = $w + $x_pos; } else { $px = $w; } } } ### generating Y position ### if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center ) { $py = (int) ( $h / 2 ); } else { if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 ) { $y_pos = (int) $y_pos; if( $y_pos >= $h ) $y_pos = 0; $py = $y_pos; } else { $y_pos = (int) $y_pos; if( $y_pos < 0 ) { if( $y_pos < -$h ) $py = $h; else $py = $h + $y_pos; } else { $py = $h; } } } $color = $this -> get_html_color( $color ); return( imagestring( $this->image , $font , $px , $py , $text , $color ) ); } public function put_image( $p_image , $x_pos , $y_pos ) { if( !$p_image ) return( NULL ); if( !$this -> image ) return( NULL ); $pw = imagesx( $p_image ); $ph = imagesy( $p_image ); $w = $this -> width; $h = $this -> height; if( !$ph || !$pw || $ph < 1 || $pw < 1 || $ph > $this->max_side_size || $pw > $this->max_side_size ) return( NULL ); ### generating X position ### if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center ) { $px = (int) ( ( $w - $pw ) / 2 ); } else { if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 ) { $x_pos = (int) $x_pos; if( $x_pos >= $w ) $x_pos = 0; $px = $x_pos; } else { $x_pos = (int) $x_pos; if( $x_pos < 0 ) { if( $x_pos < -$w ) $px = $w - $pw; else $px = $w - $pw + $x_pos; } else { $px = $w - $pw; } } } ### generating Y position ### if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center ) { $py = (int) ( ( $h - $ph ) / 2 ); } else { if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 ) { $y_pos = (int) $y_pos; if( $y_pos >= $h ) $y_pos = 0; $py = $y_pos; } else { $y_pos = (int) $y_pos; if( $y_pos < 0 ) { if( $y_pos < -$h ) $py = $h - $ph; else $py = $h - $ph + $y_pos; } else { $py = $h - $ph; } } } if( imagecopyresized( $this->image , $p_image , $px , $py , 0 , 0 , $pw , $ph , $pw , $ph ) ) return( 1 ); else return( NULL ); } public function open( $file_name ) { if( is_file( $file_name ) ) { list( $width , $height , $type ) = getimagesize( $file_name ); switch( $type ) { case 1 : if( $tmp_image = imagecreatefromgif( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return( 1 ); } else return( NULL ); break; case 2 : if( $tmp_image = imagecreatefromjpeg( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return( 1 ); } else return( NULL ); break; case 3 : if( $tmp_image = imagecreatefrompng( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return( 1 ); } else return( NULL ); break; default : return( NULL ); break; } } else { return( NULL ); } } public function open_post( $name ) { if( !isset( $_FILES[ $name ] ) ) return( NULL ); return( $this -> open( $_FILES[$name]['tmp_name'] ) ); } public function open_string( $string ) { if( $string ) { if( $tmp_image = imagecreatefromstring( $string ) ) { $this -> image = $tmp_image; $this -> width = imagesx( $this->image ); $this -> height = imagesy( $this->image ); return( 1 ); } else return( NULL ); } else return( NULL ); } public function crop( $x , $y , $width , $height ) { if( !$this -> image ) return( NULL ); if( $width < 1 ) $width = 1; if( $height < 1 ) $height = 1; if( $height > $this->max_side_size || $width > $this->max_side_size ) { return( NULL ); } if( $temp_image = imagecreatetruecolor( $width , $height ) ) { if( imagecopyresized ( $temp_image , $this->image , 0 , 0 , $x , $y , $width , $height , $width , $height ) ) { $this -> image = $temp_image; $this -> width = $width; $this -> height = $height; } } else return( NULL ); } public function resize_w( $new_size ) { if( !$this->image ) return( NULL ); $new_size = (int) $new_size; if( $new_size < 1 || $new_size > $this->max_side_size ) return( NULL ); $w = $this -> width; $h = $this -> height; $nw = $new_size; if( $nw >= $w ) return( NULL ); $ratio = $w / $nw; $nh = (int) ( $h / $ratio ); $temp_image = imagecreatetruecolor( $nw , $nh ); if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) ) { $this -> width = $nw; $this -> height = $nh; $this -> image = $temp_image; return( 1 ); } else { imagedestroy( $temp_image ); return( NULL ); } } public function resize_h( $new_size ) { if( !$this->image ) return( NULL ); $new_size = (int) $new_size; if( $new_size < 1 || $new_size > $this->max_side_size ) return( NULL ); $w = $this -> width; $h = $this -> height; $nh = $new_size; if( $nh >= $h ) return( NULL ); $ratio = $h / $nh; $nw = (int) ( $w / $ratio ); $temp_image = imagecreatetruecolor( $nw , $nh ); if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) ) { $this -> width = $nw; $this -> height = $nh; $this -> image = $temp_image; return( 1 ); } else { imagedestroy( $temp_image ); return( NULL ); } } public function set_quality( $quality ) { $quality = (int) $quality; if( $quality > 100 ) $quality = 100; if( $quality < 0 ) $quality = 0; $this -> quality = $quality; } public function out_jpg( $file_name ) { if( !$this->image ) return( NULL ); if( $file_name ) { imagejpeg( $this->image , $file_name , $this -> quality ); } else { header( 'Content-type: image/jpeg' ); imagejpeg( $this ->image ); } } public function out_gif( $file_name ) { if( !$this->image ) return( NULL ); if( $file_name ) { imagepng( $this->image , $file_name ); } else { header( 'Content-type: image/gif' ); imagepng( $this->image ); } } public function out_png( $file_name ) { if( !$this->image ) return( NULL ); if( $file_name ) { $quality = (int) ($this->quality / 10); if( $quality > 9 ) $quality = 9; imagepng( $this->image , $file_name , $quality ); } else { header( 'Content-type: image/png' ); imagepng( $this->image ); } } }### end of IMG Пример работы: Было Стало Вот кусок описывающий все операции над картинкой: PHP: $img = new IMG(); $img -> open( 'kvn_v_kepke.jpg' ); $img -> resize_h( 300 ); $img -> crop( 32 , 25 , $img->get_w()-87 , $img->get_h()-60 ); $fon = new IMG(); $fon -> create_blank( $img->get_w()+4 , $img->get_h()+4 , '#60b5a2' ); $fon -> put_image( $img->get_image() , 'center' , 'center' ); $fon -> put_text( 'Voffka - cowboy!' , 3 , 25 , -18 , '#60b5a2' ); $fon -> out_png( NULL );
лучше так: $img = new IMG( 'kvn_v_kepke.jpg' ); $img -> resizeAH( 300 ); $img -> crop( 32 , 25 , -87 , -60 );
Да, резонно, тогда не нужно делать разбор, по какой стороне ресайзить. Респект! А вот по поводу кропа, тут наверное просто нужно договорить. Предпологается на яваскрипте написать кусок, чтобы рамкой выбирался кроп. И там будет лучше юзать так, как сейчас реализовано, по сторонам прямоугольника. Какие ещё будут коменты?
А чем лучше? Если дело только в угле, то не думаю. Так как он будет использоваться редко, функции с переменным количествои параметров не поддерживаются и придётся каждый раз писать линий ноль, что в конце концов приведёт к тому, что забудешь, для чего он здесь вообще. У мыня как-то давно такая история была с функцией фильтрации текста перед отображением. Наворотил целую кучу, потом одни ноли в неё передовал. Если дело не только в угле, тогда что ещё?
можно использовать разные шрифты (в смысле свои ttf). Да и проверить нужно, что то помнится в какой то функции были проблемы с кирилицей, но не помню в какой
Код (Text): $fon -> put_image( $img->get_image() , 'center' , 'center' ); может лучше для горизонтали оставить left/center/right а для вертикали top/middle/bottom? а то я вижу 2 раза center и мне приходится смотреть в класс чтобы понять на каком месте какое измерение стоит
Ganzal, согласен. Исправлять в своей версии не буду, ибо не привык к такому разделению. Но за подсказку спасибо.
PHP: ############################ ## v.2.1 from 19 Dec 2008 ## ############################ class IMG { private $max_side_size = 10000; private $width = 0; private $height = 0; private $type = null; private $image = null; private $error = null; private $quality = 90; function __construct() { } public function check() { if( $this->image && $this->width > 0 && $this->height > 0 ) return true; else return false; } public function w() { return( $this->width ); } public function h() { return( $this->height ); } public function get_image() { return( $this->image ); } private function hex2rgb( $color ) { $color = str_replace( '#' , '' , $color ); return( array( 1 => hexdec('0x'.$color{0}.$color{1}) , 2 => hexdec('0x'.$color{2}.$color{3}) , 3 => hexdec('0x'.$color{4}.$color{5}) ) ); } private function get_html_color( $color ) { $color = $this -> hex2rgb( $color ); $color = imagecolorallocate( $this->image , $color[1] , $color[2] , $color[3] ); return( $color ); } public function create_blank( $width , $height , $bg_color ) { $width = (int) $width; $height = (int) $height; if( $width < 1 ) $width = 1; if( $height < 1 ) $height = 1; if( $height > $this->max_side_size || $width > $this->max_side_size ) { return false; } if( $this -> image = imagecreatetruecolor( $width , $height ) ) { if( $color = $this -> get_html_color( $bg_color ) ) { imagefill( $this->image , 0 , 0 , $color ); } $this -> width = $width; $this -> height = $height; return true; } else { $this -> error = true; return false; } } public function put_text( $text , $font , $x_pos , $y_pos , $color ) { if( !$this -> image ) return false; $w = $this -> width; $h = $this -> height; ### generating X position ### if( $x_pos == 'center' || $x_pos == 'CENTER' || $x_pos == Center ) { $px = (int) ( $w / 2 ); } else { if( $x_pos == 'left' || $x_pos == 'Left' || $x_pos == 'Left' || intval($x_pos) >= 0 ) { $x_pos = (int) $x_pos; if( $x_pos >= $w ) $x_pos = 0; $px = $x_pos; } else { $x_pos = (int) $x_pos; if( $x_pos < 0 ) { if( $x_pos < -$w ) $px = $w ; else $px = $w + $x_pos; } else { $px = $w; } } } ### generating Y position ### if( $y_pos == 'center' || $y_pos == 'CENTER' || $y_pos == Center ) { $py = (int) ( $h / 2 ); } else { if( $y_pos == 'top' || $y_pos == 'Top' || $y_pos == 'TOP' || intval($y_pos) >= 0 ) { $y_pos = (int) $y_pos; if( $y_pos >= $h ) $y_pos = 0; $py = $y_pos; } else { $y_pos = (int) $y_pos; if( $y_pos < 0 ) { if( $y_pos < -$h ) $py = $h; else $py = $h + $y_pos; } else { $py = $h; } } } $color = $this -> get_html_color( $color ); return( imagestring( $this->image , $font , $px , $py , $text , $color ) ); } public function put_image( $p_image, $x_pos=0, $y_pos=0 ) { if( !$p_image ) return false; if( !$this -> image ) return false; $pw = imagesx( $p_image ); $ph = imagesy( $p_image ); $w = $this -> width; $h = $this -> height; if( !$ph || !$pw || $ph < 1 || $pw < 1 || $ph > $this->max_side_size || $pw > $this->max_side_size ) return false; if( imagecopyresized( $this->image , $p_image , $x_pos , $y_pos , 0 , 0 , $pw , $ph , $pw , $ph ) ) return true; else return false; } public function open( $file_name ) { if( is_file( $file_name ) ) { list( $width , $height , $type ) = getimagesize( $file_name ); switch( $type ) { case 1 : if( $tmp_image = imagecreatefromgif( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return true; } else return false; break; case 2 : if( $tmp_image = imagecreatefromjpeg( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return true; } else return false; break; case 3 : if( $tmp_image = imagecreatefrompng( $file_name ) ) { $this -> image = $tmp_image; $this -> width = $width; $this -> height = $height; return true; } else return false; break; default : return false; break; } } else { return false; } } public function open_post( $name ) { if( !isset( $_FILES[ $name ] ) ) return false; return( $this -> open( $_FILES[$name]['tmp_name'] ) ); } public function open_data( $string ) { if( $string ) { if( $tmp_image = imagecreatefromstring( $string ) ) { $this -> image = $tmp_image; $this -> width = imagesx( $this->image ); $this -> height = imagesy( $this->image ); return true; } else return false; } else return false; } public function crop( $x , $y , $width , $height ) { if( !$this -> image ) return false; if( $width < 1 ) $width = 1; if( $height < 1 ) $height = 1; if( $height > $this->max_side_size || $width > $this->max_side_size ) { return false; } if( $temp_image = imagecreatetruecolor( $width , $height ) ) { if( imagecopyresized ( $temp_image , $this->image , 0 , 0 , $x , $y , $width , $height , $width , $height ) ) { $this -> image = $temp_image; $this -> width = $width; $this -> height = $height; } } else return false; } public function resize_w( $new_size ) { if( !$this->image ) return false; $new_size = (int) $new_size; // Если это раскоментировать, то не будет давать растягивать изображения, а только сжимать // if( $new_size < 1 || $new_size > $this->max_side_size ) return false; $w = $this -> width; $h = $this -> height; $nw = $new_size; // Если это раскоментировать, то не будет давать растягивать изображения, а только сжимать // if( $nw >= $w ) return false; $ratio = $w / $nw; $nh = (int) ( $h / $ratio ); $temp_image = imagecreatetruecolor( $nw , $nh ); if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) ) { $this -> width = $nw; $this -> height = $nh; $this -> image = $temp_image; return true; } else { imagedestroy( $temp_image ); return false; } } public function resize_h( $new_size ) { if( !$this->image ) return false; $new_size = (int) $new_size; if( $new_size < 1 || $new_size > $this->max_side_size ) return false; $w = $this -> width; $h = $this -> height; $nh = $new_size; if( $nh >= $h ) return false; $ratio = $h / $nh; $nw = (int) ( $w / $ratio ); $temp_image = imagecreatetruecolor( $nw , $nh ); if( imagecopyresampled ( $temp_image , $this->image , 0 , 0 , 0 , 0 , $nw , $nh , $w , $h ) ) { $this -> width = $nw; $this -> height = $nh; $this -> image = $temp_image; return true; } else { imagedestroy( $temp_image ); return false; } } public function resize( $new_side ) { if( $this->width > $this->height ) return $this -> resize_w( $new_side ); else return $this -> resize_h( $new_side ); } public function quality( $quality = 75 ) { $quality = (int) $quality; if( $quality > 100 ) $quality = 100; if( $quality < 0 ) $quality = 0; $this -> quality = $quality; } public function save_jpg( $file_name = null ) { if( !$this->image ) return false; if( $file_name ) { imagejpeg( $this->image , $file_name , $this -> quality ); } else { header( 'Content-type: image/jpeg' ); imagejpeg( $this ->image, null, $this -> quality ); } return true; } public function save_gif( $file_name = null ) { if( !$this->image ) return false; if( $file_name ) { imagepng( $this->image , $file_name ); } else { header( 'Content-type: image/gif' ); imagepng( $this->image ); } return true; } public function save_png( $file_name = null ) { if( !$this->image ) return false; if( $file_name ) { $quality = (int) ($this->quality / 10); if( $quality > 9 ) $quality = 9; imagepng( $this->image , $file_name , $quality ); } else { header( 'Content-type: image/png' ); imagepng( $this->image ); } return true; } }### end of IMG ?>
Недостаток, как уже было упомянуто в добавлении текста кирилицей. Но мне текст не нужен, так что не заморачиваюсь. Врятли ещё будет много обновлений, так как собираюсь переходить на ИмэйджМэджик. p.s. Кос, приятно тебя читать. Как дела? Шаблонку закинул или пользуешь. Я там тоже что-то вроде подправлял малец, но html кэша так и не сделал.
Danilka деревья выводить при помощи нее таки можно! Очень клево и удобно ей пользоваться. Как будет свободное время - залью на code.google, может че-нить доделаю в ней.