Development

Changeset 5756

You must first sign up to be able to contribute.

Changeset 5756

Show
Ignore:
Timestamp:
10/30/07 06:17:27 (6 years ago)
Author:
Jonathan.Todd
Message:

sfThumbnailCache: Added static functions and helper functions to DRY code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfThumbnailCachePlugin/lib/helper/sfThumbnailCacheHelper.php

    r5733 r5756  
    11<?php 
    2 function sf_thumbnail_s3fs_image_tag($photo,$w = null,$h = null) 
     2 
     3/** 
     4 * This function takes a s3fs id and returns the path to a thumbnail of  
     5 * the specified size. The thumbnail is automatically generated. If it doesn't 
     6 * exist the alternate is used. 
     7 *  
     8 * s3fs id's are of the form: 'mtime_MT_/bucket/key' like: 
     9 * "1193674271_MT_/adventex/c21hs/employee_cma_photos/3603.jpg" 
     10 *  
     11 */ 
     12function sf_thumbnail_cache_path($photo,$w = null,$h = null,$options = null) 
    313{ 
    414  $w = $w ? $w : 0; 
    515  $h = $h ? $h : 0; 
    6   $thumb_url = "/sfThumbnailCache/dispatchS3FS?w=$w&h=$h&f=".$photo; 
     16       
     17  $thumb = sfThumbnailCache::getNewInstance($photo,$w,$h); 
     18         
     19  return $thumb->getPath(); 
     20
     21 
     22/** 
     23 * This function takes a s3fs id and returns an image tag to a thumbnail of  
     24 * the specified size. The thumbnail is automatically generated. If it doesn't 
     25 * exist the alternate is used. 
     26 *  
     27 * s3fs id's are of the form: 'mtime_MT_/bucket/key' like: 
     28 * "1193674271_MT_/adventex/c21hs/employee_cma_photos/3603.jpg" 
     29 *  
     30 */ 
     31function sf_thumbnail_s3fs_image_tag($photo,$w = null,$h = null,$options = null) 
     32
     33  $w = $w ? $w : 0; 
     34  $h = $h ? $h : 0; 
     35   
     36  $alt_img = ''; 
     37  if(isset($options['alt_image'])) 
     38    $alt_img = $options['alt_image']; 
     39         
     40  $thumb_url = "/sfThumbnailCache/dispatchS3FS?w=$w&h=$h&f=".$photo."&alt_image=".$alt_img; 
    741  return "<img src='$thumb_url' />"; 
    842} 
     43 
     44/** 
     45 * This function takes a s3fs id and returns the path to a thumbnail of  
     46 * the specified size. The thumbnail is automatically generated. If it doesn't 
     47 * exist the alternate is used. 
     48 *  
     49 * s3fs id's are of the form: 'mtime_MT_/bucket/key' like: 
     50 * "1193674271_MT_/adventex/c21hs/employee_cma_photos/3603.jpg" 
     51 *  
     52 */ 
     53function sf_thumbnail_s3fs_thumb_path($photo,$w = null,$h = null,$options = null) 
     54{ 
     55  $w = $w ? $w : 0; 
     56  $h = $h ? $h : 0; 
     57   
     58  $alt_img = ''; 
     59  if(isset($options['alt_image'])) 
     60    $alt_img = $options['alt_image']; 
     61         
     62  $file = sfAmazonS3File::sfAmazonS3FilePathToArray($photo); 
     63  $bucket = $file['bucket']; 
     64  $key = $file['key']; 
     65  $mtime = $file['mtime']; 
     66  $file_thumb = '/sfAmazonS3FS/'.$bucket.'/'.$key; 
     67  $path = sfThumbnailCache::sf_thumbnail_s3_thumbnail($bucket,$key,$file_thumb,$w,$h,$mtime); 
     68         
     69  return $path; 
     70} 
     71 
  • plugins/sfThumbnailCachePlugin/lib/sfThumbnailCache.class.php

    r5733 r5756  
    181181  } 
    182182   
     183  /** 
     184   * Does cached thumbnail exist? 
     185   */ 
    183186  public function exists() 
    184187  { 
     
    186189  } 
    187190 
     191  /** 
     192   * Delete thumbnail 
     193   */ 
    188194  public function delete() 
    189195  { 
     
    222228   * Get thumbnail width 
    223229   */ 
    224   public function getWidth() 
    225  
     230  public function getWidth() 
     231 
    226232     return $this->width; 
    227233   } 
    228234    
    229   /** 
    230     * Get thumbnail height 
    231     */ 
    232     public function getHeight() 
    233    
     235  /** 
     236   * Get thumbnail height 
     237   */ 
     238  public function getHeight() 
     239 
    234240      return $this->height; 
    235241    } 
     
    265271 
    266272  } 
    267  
     273   
     274  /** 
     275   * Get modification time of cached file 
     276   */ 
    268277  public function getCacheMTime() 
    269278  { 
     
    365374    return substr($f, 0, strrpos($f, '.')); 
    366375  } 
     376   
     377  /** 
     378   * This function takes a s3fs bucket and key and returns thumbnail path. 
     379   * If thumb doesn't already exist master is pulled from s3. 
     380   *  
     381   * s3fs id's are of the form: 'mtime_MT_/bucket/key' like: 
     382   * "1193674271_MT_/adventex/c21hs/employee_cma_photos/3603.jpg" 
     383   *  
     384   */ 
     385  public static function sf_thumbnail_s3_thumbnail($bucket,$key,$file_thumb,$w,$h,$mtime) 
     386  { 
     387    $thumb = sfThumbnailCache::getNewInstanceNoMaster($file_thumb,$w,$h,true,true,null,$mtime); 
     388    // Valid thumb exists 
     389    if($thumb->cacheIsValid()) 
     390    { 
     391      return $thumb->getPath(); 
     392    } 
     393    // Valid thumb doesn't exist 
     394    else 
     395    { 
     396      // Try to get master file    
     397      $file = sfAmazonS3File::getNewInstance($key,$bucket,$mtime);       
     398 
     399      // Master exists, create thumbnail 
     400      if($file->fileExists()) 
     401      { 
     402        $file_master = $file->getPath(); 
     403        $master_mtime = $file->fileGetMTime(); 
     404        //echo "Mtime: $master_mtime"."<br><br>".print_r($file->s3GetFileMetaData(),1); 
     405        // Create thumb from master 
     406        $thumb = sfThumbnailCache::getNewInstance($file_master,$w,$h,true,true,null, 
     407          $file_thumb,$master_mtime); 
     408 
     409        return $thumb->getPath(); 
     410      } 
     411 
     412      // Master doesn't exists 
     413      return false; 
     414    }  
     415  } 
     416   
     417  /** 
     418   * This function takes a file and returns a path to it's thumb 
     419   *  
     420   */ 
     421  public static function sf_thumbnail_cache_path($photo,$w = null,$h = null,$options = null) 
     422  { 
     423    $w = $w ? $w : 0; 
     424    $h = $h ? $h : 0; 
     425 
     426    $thumb = sfThumbnailCache::getNewInstance($photo,$w,$h); 
     427 
     428    return $thumb->getPath(); 
     429  } 
     430 
     431   
    367432} 
    368433 
  • plugins/sfThumbnailCachePlugin/modules/sfThumbnailCache/actions/actions.class.php

    r5733 r5756  
    5555    $w = $this->getRequestParameter('w'); 
    5656    $h = $this->getRequestParameter('h'); 
    57      
     57    $alt_image = $this->getRequestParameter('alt_image'); 
    5858 
    59     $file = sfAmazonS3File::sfAmazonS3FilePathToArray($s3FSFile); 
    60     $bucket = $file['bucket']; 
    61     $key = $file['key']; 
    62     $mtime = $file['mtime']; 
    63      
    64     $file_thumb = '/sfAmazonS3FS/'.$bucket.'/'.$key; 
    65      
    66     // echo "Params: $s3FSFile<br>$w<br>$h<br>$bucket<br>$key<br>$mtime<br>"; 
    67     // exit; 
    68     // If a valid thumb can be generated get it 
    69     $path = $this->getS3FSThumbnail($bucket,$key,$file_thumb,$w,$h,$mtime); 
    70     // Use alternate 
    71     // if(!$path) 
    72     //     $path = $this->getS3FSThumbnail($bucket,$alt_key,$alt_thumb,$w,$h,$mtime); 
    73      
     59    if($s3FSFile!='') 
     60    { 
     61      $file = sfAmazonS3File::sfAmazonS3FilePathToArray($s3FSFile); 
     62      $bucket = $file['bucket']; 
     63      $key = $file['key']; 
     64      $mtime = $file['mtime']; 
     65      $file_thumb = '/sfAmazonS3FS/'.$bucket.'/'.$key; 
     66      $path = sfThumbnailCache::sf_thumbnail_s3_thumbnail($bucket,$key,$file_thumb,$w,$h,$mtime); 
     67    } 
     68 
     69    if(!$path) 
     70    { 
     71      $file = sfAmazonS3File::sfAmazonS3FilePathToArray($alt_image); 
     72      $alt_bucket = $file['bucket']; 
     73      $alt_key = $file['key']; 
     74      $alt_mtime = $file['mtime']; 
     75      $alt_thumb = '/sfAmazonS3FS/'.$alt_bucket.'/'.$alt_key; 
     76      $path = sfThumbnailCache::sf_thumbnail_s3_thumbnail($alt_bucket,$alt_key,$alt_thumb,$w,$h,$alt_mtime); 
     77    } 
     78 
    7479    return $this->dispatch($path,"image/jpeg",$mtime); 
    7580  } 
    7681   
    77    
    78   /** 
    79    * Returns path to thumbnail or false if thumbnail can't be created 
    80    */ 
    81   public function getS3FSThumbnail($bucket,$key,$file_thumb,$w,$h,$mtime) 
    82   { 
    83     $thumb = sfThumbnailCache::getNewInstanceNoMaster($file_thumb,$w,$h,true,true,null,$mtime); 
    84     // Valid thumb exists 
    85     if($thumb->cacheIsValid()) 
    86     { 
    87       return $thumb->getPath(); 
    88     } 
    89     // Valid thumb doesn't exist 
    90     else 
    91     { 
    92       // Try to get master file    
    93       $file = sfAmazonS3File::getNewInstance($key,$bucket,$mtime);       
    94        
    95       // Master exists, create thumbnail 
    96       if($file->fileExists()) 
    97       { 
    98         $file_master = $file->getPath(); 
    99         $master_mtime = $file->fileGetMTime(); 
    100         //echo "Mtime: $master_mtime"."<br><br>".print_r($file->s3GetFileMetaData(),1); 
    101         // Create thumb from master 
    102         $thumb = sfThumbnailCache::getNewInstance($file_master,$w,$h,true,true,null, 
    103           $file_thumb,$master_mtime); 
    104          
    105         return $thumb->getPath(); 
    106       } 
    107        
    108       // Master doesn't exists 
    109       return false; 
    110     }  
    111   } 
    11282}