Changeset 1181

Show
Ignore:
Timestamp:
07/22/08 22:31:19 (4 months ago)
Author:
_ck_
Message:

0.1.5 first attempt at inline image display ability

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bb-attachments/trunk/bb-attachments.php

    r1128 r1181  
    66Author: _ck_ 
    77Author URI: http://bbShowcase.org 
    8 Version: 0.1.2 
     8Version: 0.1.5 
    99 
    1010License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ 
     
    1414 
    1515$bb_attachments['role']['see']="read";           // minimum role to see list of attachments = read/participate/moderate/administrate 
     16$bb_attachments['role']['inline']="read";    // minimum role to view inline images = read/participate/moderate/administrate 
    1617$bb_attachments['role']['download']="participate";  // minimum role to download = read/participate/moderate/administrate 
    1718$bb_attachments['role']['upload']="participate";  // minimum role to upload = participate/moderate/administrate (times out with post edit time) 
     
    4041$bb_attachments['max']['filename']['administrate']=80;  // override 
    4142 
     43$bb_attachments['inline']['width']=590;         // max inline image width in pixels (for display, real width unlimited) 
     44$bb_attachments['inline']['height']=590;                // max inline image height in pixels (for display, real height unlimited) 
     45$bb_attachments['inline']['solution']="resize"; // resize|frame - images can be either resized or CSS framed to meet above requirement 
     46                                                                        // only resize is supported at this time 
    4247$bb_attachments['style']="#thread .post li {clear:none;}"; 
    4348 
     
    6166add_action( 'bb_init', 'bb_attachments_init'); 
    6267add_action( 'bb_post.php', 'bb_attachments_process_post'); 
     68add_filter( 'bb_allowed_tags', 'bb_attachments_extra_tags' ); 
    6369bb_register_activation_hook( __FILE__,  'bb_attachments_install'); 
    6470 
     
    7278if (isset($_GET['bb_attachments'])) { 
    7379        if (isset($_GET['bbat'])) { 
    74                 bb_attachments_download(); 
     80                if (isset($_GET['inline'])) {bb_attachments_inline();} 
     81                else {bb_attachments_download();} 
    7582        } else {  
    7683                if (bb_attachments_location()!='edit.php') { 
     
    115122function bb_attachments_add_css() { global $bb_attachments;  echo '<style type="text/css">'.$bb_attachments['style'].'</style>';} // inject css 
    116123 
     124function bb_attachments_extra_tags( $tags ) {if (!isset($tags['img'])) {$tags['img'] = array('src' => array(), 'title' => array(), 'alt' => array());} return $tags;} 
     125 
    117126function bb_attachments($post_id=0) { 
    118127global $bb_attachments_on_page; 
     
    149158        if ((!is_topic() || isset($_GET['bb_attachments'])) && bb_current_user_can('moderate')) {$filter=""; $admin=bb_current_user_can('administrate');}         
    150159        if (bb_current_user_can($bb_attachments['role']['delete'])) {$can_delete=true;} 
     160         
     161        $file = bb_attachments_location();       $can_inline=true; 
     162        if (!($bb_attachments['role']['inline']=="read" || bb_current_user_can($bb_attachments['role']['inline']))) {$can_inline=false;} 
    151163                 
    152164        if (!isset($bb_attachments_cache[$post_id])) { 
    153165                $bb_attachments_cache[$post_id]=$bbdb->get_results("SELECT * FROM bb_attachments WHERE post_id = $post_id ORDER BY time DESC LIMIT 999"); 
    154         }                
    155  
     166        }                        
    156167 
    157168        if (count($bb_attachments_cache[$post_id])) { 
     
    191202                                                $output.=' [<a onClick="return confirm('."'".__('Delete')." $attachment->filename ?"."'".')" href="'.add_query_arg('bbat_delete',$attachment->id).'">x</a>] '; 
    192203                                } 
     204 
     205                                if ($attachment->status==0 && $file=="edit.php" && $can_inline) {                                                                
     206                                        $output.=" [<a href='#' onclick='bbatt_inline_insert($attachment->post_id,$attachment->id); return false;'>".__("INSERT")."</a>] ";                              
     207                                }                                                
    193208         
    194209                                $output.=" </span></li>"; 
     
    197212        } 
    198213if ($output) {$output="<h3>".__("Attachments")."</h3><ol>".$output."</ol>";} 
     214if ($file="edit.php") { 
     215$output.='<scr'.'ipt type="text/javascript" defer="defer">function bbatt_inline_insert(post_id,id) { 
     216        myField=document.getElementsByTagName("textarea")[0];   
     217        tmp="?bb_attachments="+post_id+"&bbat="+id; q="'."'".'"; 
     218        myValue="<a href="+q+tmp+q+"><img src="+q+tmp+"&inline"+q+"></a>";       
     219        if (document.selection) {myField.focus();sel = document.selection.createRange();sel.text = myValue;} 
     220        else if (myField.selectionStart || myField.selectionStart == "0") {var startPos = myField.selectionStart; var endPos = myField.selectionEnd; 
     221                myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 
     222        } else {myField.value += myValue;} 
     223return false;} </script>'; 
     224} 
    199225} 
    200226return $output; 
     
    368394                exit();          
    369395        }        
    370 } else {echo "<script type='text/javascript'>alert('".__('Sorry, download is restricted.')."');</script>";} 
     396} else {echo "<scr"."ipt type='text/javascript'>alert('".__('Sorry, download is restricted.')."');</script>";} 
     397
     398 
     399function bb_attachments_inline($filenum=0) { 
     400global $bbdb, $bb_attachments; 
     401$filenum=intval($filenum); 
     402if ($filenum==0 && isset($_GET['bbat'])) {$filenum=intval($_GET['bbat']);} 
     403if ($filenum>0 && ($bb_attachments['role']['inline']=="read" || bb_current_user_can($bb_attachments['role']['inline']))) { 
     404        $file=$bbdb->get_results("SELECT * FROM bb_attachments WHERE id = $filenum AND status = 0 LIMIT 1");             
     405        if (isset($file[0]) && $file[0]->id) { 
     406                $file=$file[0]; $file->filename=stripslashes($file->filename);                           
     407                $fullpath=$bb_attachments['path'].floor($file->id/1000)."/".$file->id.".".$file->filename; 
     408                if (file_exists($fullpath)) {                            
     409                        if (!list($width, $height, $type) = getimagesize($fullpath)) {exit();} // not an image?! 
     410                        $mime=image_type_to_mime_type($type);   // lookup number to full string 
     411                        $mime=trim(substr($mime,0,strpos($mime.";",";")));      // trim full string if necessary                                         
     412                         
     413                        if ($height>$bb_attachments['inline']['height'] || $width>$bb_attachments['inline']['width']) { 
     414                                if (!file_exists($fullpath.".resize") && !bb_attachments_resize($fullpath,$type,$width,$height)) {exit();}               
     415                                $fullpath.=".resize"; 
     416                                $file->filename.=".resize";                      
     417                                $file->size=filesize($fullpath); 
     418                                if (!$file->size) {exit();} 
     419                        } 
     420                         
     421                        if (ini_get('zlib.output_compression')) {ini_set('zlib.output_compression', 'Off');}    // fix for IE 
     422                        // header ("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); 
     423                        // header("Pragma: hack"); 
     424                        header("Content-Type: ".$mime); 
     425                        header("Content-Length: $file->size"); 
     426                        header('Content-Disposition: inline; filename="'.$file->filename.'"'); 
     427                        header("Content-Transfer-Encoding: binary");               
     428                        ob_clean(); 
     429                        flush();  
     430                        $fp = @fopen($fullpath,"rb"); 
     431                                set_time_limit(0);  
     432                        fpassthru($fp); // avoids file touch bug with readfile 
     433                        fclose($fp);                                                                                     
     434                        }                
     435                exit();          
     436        }        
     437} else { 
     438        $im=@imagecreatetruecolor(1,1); 
     439        header("Content-type: image/png"); 
     440        header("Content-Length: ".strlen($im)); 
     441        imagepng($im); 
     442        imagedestroy($im); 
     443        exit(); 
     444
     445
     446 
     447function bb_attachments_resize($filename, $type, $width, $height) {              
     448global $bb_attachments; 
     449        switch($type) { 
     450        case IMAGETYPE_JPEG: 
     451                $img = imagecreatefromjpeg($filename);  $save= 'imagejpeg'; break; 
     452        case IMAGETYPE_GIF: 
     453                $img = imagecreatefromgif($filename); $save='imagegif';  break; 
     454        case IMAGETYPE_PNG:                      
     455                $img = imagecreatefrompng($filename);   $save_img = 'imagepng';  break; 
     456        default:                 
     457                return false; 
     458        }        
     459        $scale_x=1; if ($width>$bb_attachments['inline']['width']) {$scale_x = $width/$bb_attachments['inline']['width'];} 
     460        $scale_y=1; if ($height>$bb_attachments['inline']['height']) {$scale_y = $height/$bb_attachments['inline']['height'];} 
     461        $scale=$scale_x; if ($scale_y>$scale_x) {$scale=$scale_y;}; 
     462        if ($scale<=1) {return false;}   
     463        $new_width = round(1/$scale * $width); 
     464        $new_height = round(1/$scale * $height); 
     465                 
     466        $output=$filename.".resize";             
     467        if (!file_exists($output)) { 
     468                $new= imagecreatetruecolor($new_width, $new_height); 
     469                imagecopyresampled($new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
     470                $save($new, $output); 
     471                imagedestroy($new); 
     472        } 
     473        imagedestroy($img);              
     474return true;             
    371475} 
    372476 
  • bb-attachments/trunk/readme.txt

    r1069 r1181  
    5151* 0.1.0 uploading now possible directly on new posts (instead of only attaching afterwards) 
    5252* 0.1.1 role check before hooks, ability to disable upload on new posts if compatibility issues 
     53* 0.1.5 first attempt at inline image display ability 
    5354         
    5455== To Do == 
     
    5758* check for file duplicates before saving 
    5859* thumbnails for image attachments 
    59 * serving images inline rather than just downloading 
    6060* pre-validate upload filenames via javascript to spare user upload time with rejection 
    6161* admin menu