shikopick
کاربر تازه وارد
- تاریخ عضویت
- 2 ژوئن 2005
- نوشتهها
- 71
- لایکها
- 0
PHP:
<?
/*image_converter.php*/
/*file selector*/
class file_selection extends GtkFileSelection{
function file_selection($title,$object_array=''){
$this->GtkFileSelection($title);
if(is_array($object_array)){
$this->object_array=$object_array;
}
$but=$this->ok_button;
$but->connect_object('clicked',array(&$this,'_get_file'));
$but->connect_object_after('clicked',array(&$this,'destroy'));
$but2=$this->cancel_button;
$but2->connect_object('clicked',array(&$this,'destroy'));
$this->show_all();
}
function _get_file(){
$filename = str_replace('\\','/',$this->get_filename());
/*object_array=>object,function,args */
if($this->object_array){
$obj=$this->object_array[0];
$func=$this->object_array[1];
$args=$this->object_array[2];
$args_all[0]=$filename;
$args_all[1]=$args;
eval("\$obj->\$func(\$args_all);");
}else{
return $filename;
}
}
}
/*grabs the contents of given directory for given file-types (only checks extension)*/
class dir_contents{
function dir_contents($requested_ext=''){
$this->requested_ext=$requested_ext;
}
function dir_get($dir){
$check=$this->requested_ext;
if(!is_dir($dir)){
$this->errors.="Directory $dir does not exist";return false;
}
$d=dir($dir);
while (false !== ($entry = $d->read())){
if($check){
$ext=substr($entry,-4);
if(in_array($ext,$check)){
$rets[]=$entry;
}
}else{
$rets[]=$entry;
}
}
$d->close();
if(!$rets){return false;}
return $rets;
}
}
/*build the GUI and connect to the other bits*/
class image_converter extends GtkWindow{
var $extensions;
var $convert_to;
function image_converter($convert_to,$extensions=''){
$this->GtkWindow();
$this->realize();
$this->extensions=$extensions;
$this->convert_to=$convert_to;
/*container to stick our buttons in*/
$v_box = &new GtkVbox();
$this->container=$v_box;
/*browse buttons*/
$E1= &new GtkHbox(TRUE,1);
$label= &new GtkLabel('directory to convert');
$entry_in= &new GtkEntry();
$but= &new GtkButton('browse');
$but->connect_object('clicked',array(&$this,_get_dir),$entry_in);
$this->_stick(&$E1,array($label,$entry_in,$but));
$E2= &new GtkHbox(TRUE,1);
$label= &new GtkLabel('save path');
$entry_out= &new GtkEntry();
$but= &new GtkButton('browse');
$but->connect_object('clicked',array(&$this,_get_dir),$entry_out);
$this->_stick(&$E2,array($label,$entry_out,$but));
$go=&new GtkButton();
$but_box = &new GtkVBox();
$but_label= &new GtkLabel('Convert');
$but_box->add($but_label);
$go->add($but_box);
$go->connect_object('clicked',array(&$this,'_convert'),$entry_in,$entry_out);
$this->status=$but_label;
$this->_stick($v_box,array($E1,$E2,$go));
$this->add($v_box);
$this->show_all();
}
function _convert($entry_in,$entry_out){
$status=$this->status;//button label//
$status->set_text("changed");
$dir_in=$entry_in->get_text();
$dir=&new dir_contents($this->extensions);
$array=$dir->dir_get($dir_in);
$dir_out=$entry_out->get_text();
if(is_dir($dir_out) && is_writable($dir_out)){
if(count($array)){
foreach($array as $im){
$x++;
while(gtk::events_pending())gtk::main_iteration();
$status->set_text("Converting $im ($x of ".count($array).')');
while(gtk::events_pending())gtk::main_iteration();
exec(IMAGICK."convert $dir_in"."$im $dir_out".str_replace(substr($im,-4),$this->convert_to,$im));
if(!is_dir("$dir_out/thumbs/")){mkdir("$dir_out/thumbs/",0755);}
exec(IMAGICK."convert -geometry 50x50 $dir_in"."$im $dir_out/thumbs/".str_replace(substr($im,-4),$this->convert_to,$im));
}
$status->set_text("Convert");
if(!$this->show_but_flag){
$but=&new GtkButton('Conversion Complete :: View Thumbnails');
$but->connect_object('clicked',array(thumbs,thumbs),"$dir_out/thumbs/",array($this->convert_to));
$this->show_but_flag=1;
$container=$this->container;
$container->add($but);
$container->show_all();
}
}else{
$status->set_text("No files found matching requested extensions");
}
}else{
$status->set_text("Output directory does not exist or is not writeable");
}
}
function _get_dir($obj){
$t=&new file_selection('file browser',array(&$this,'_get_dir_vals',&$obj));
}
function _get_dir_vals($array){
$array[1]->set_text($array[0]);
}
function _stick($what,$array){
for($x=0;$x<count($array);$x++){
$what->add($array[$x]);
}
}
}
class thumbs {
function thumbs($dir_out,$format){
$dir=&new dir_contents($format);
$array=$dir->dir_get($dir_out);
$dlg = &new GtkDialog();
$dlg->set_position(GTK_WIN_POS_CENTER);
$dlg->realize();
$dlg_vbox = $dlg->vbox;
$dlg_label = &new GtkLabel("Pretty Pictures");
$dlg_vbox->pack_start($dlg_label);
$box1 = &new GtkVBox();
$box2 = &new GtkVBox();
$s_win = &new GtkScrolledWindow();
$s_win->set_usize(50,120);
$s_win->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
$box1->pack_start($s_win);
$s_win->add_with_viewport($box2);
$box2->set_focus_vadjustment($s_win->get_vadjustment());
foreach($array as $im){
list($pixmap, $mask) = Gdk::pixmap_create_from_xpm($dlg->window,null,$dir_out.$im);
$ebox=&new GtkEventbox();
$ebox->set_events(GDK_BUTTON_PRESS_MASK);
$out=str_replace('thumbs/','',$dir_out.$im);
$ebox->connect_object('button-press-event',array(thumbs,show_full),$out);
$img = &new GtkPixmap($pixmap, $mask);
$ebox->add($img);
$box2->pack_start($ebox,FALSE,FALSE,2);
}
$dlg_vbox->add($box1);
$dlg->show_all();
}
function show_full($widget,$path){
$dlg = &new GtkDialog();
$dlg->set_position(GTK_WIN_POS_CENTER);
$dlg_vbox = $dlg->vbox;
$dlg_box = $dlg->action_area;
$dlg_label = &new GtkLabel($path);
$dlg_vbox->pack_start($dlg_label);
$dlg->realize();
list($pixmap, $mask) = Gdk::pixmap_create_from_xpm($dlg->window,null,$path);
$img = &new GtkPixmap($pixmap, $mask);
$dlg_box->add($img);
$dlg->show_all();
}
}?>
<?/*simplest example*/
(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')?dl('php_gtk.dll'):dl('php_gtk.so');
/*
this should be the only config required
and is should point to wherever your imagemagick
is installed, if imagemagick is in your PATH or
you are on *NIX then
DEFINE('IMAGICK','');
should do the trick
*/
DEFINE('IMAGICK','F:/phpdev4/bin/magick/');
/*include the class*/
include 'image_converter.php';
/*example usage*/ // convert all .gif,.png,.jpg files to .xmp format
$p=&new image_converter('.xpm',array('.gif','.jpg','.png'));
//thumbs::thumbs('c:/phpdev/gtkdev/test2/','.xpm');//browse a directory of xpm's//
Gtk::main();
?>