Advertisement
Guest User

Untitled

a guest
Oct 20th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.50 KB | None | 0 0
  1. <?php
  2. /*******************************************************************************
  3. * FRESHIZER -> WP Image Resizer Script
  4. * =============================================================================
  5. *
  6. * @license GNU version 2
  7. * @author freshace
  8. * @version 1.5
  9. * @link http://github.com/boobslover/freshizer
  10. /*******************************************************************************
  11. * SETTINGS, PLEASE CHANGE ONLE THESE 3 CONSTANTS
  12. ******************************************************************************/
  13. // NOTE
  14. // ====
  15. // please notice, that the time is in SECONDS. There are not allowed math
  16. // operations in the definition. So instead of writing:
  17. // = 60(sec) * 60(min) * 24(hr) * 7(days); you have to write:
  18. // = 604800; // seconds in 7 days
  19.  
  20.  
  21. // CACHE TIME
  22. // ==========
  23. // When the new (cached) file is older than this time, script automatically
  24. // checks, if the old file has been changed. If not, then ve serve cached file
  25. // again. If yes, cached file is deleted and resized again.
  26. define('CACHE_TIME', 604800);
  27.  
  28. // CACHE DELETE FILES AFTER
  29. // ========================
  30. // Hard delete files ( not only compare if the original file has been changed,
  31. // but hardly delete from caching folder ), every X seconds. Please fill a large
  32. // number, because cached files runs much more speedely
  33. define('CACHE_DELETE_FILES_AFTER',10000000);
  34.  
  35. // CACHE DELETE FILES - check every X hits
  36. // =======================================
  37. // How often do we check if there are files which should be hard deleted ?
  38. // Optimal is approx 400 - 500 hits
  39. define('CACHE_DELETE_FILES_check_every_x_hits',150);
  40.  
  41.  
  42.  
  43. class blFile {
  44. CONST POINTER_END = 'pend';
  45.  
  46. private $_handle = null;
  47. private $_fileSize = null;
  48. private $_path = null;
  49. private $_writeBuffer = '';
  50.  
  51. /*----------------------------------------------------------------------------*/
  52. /* FUNCTIONS PUBLIC
  53. /*----------------------------------------------------------------------------*/
  54.  
  55. public function __construct( $handle, $path ) {
  56. $this->_setHandle( $handle );
  57. $this->_setPath( $path );
  58. }
  59.  
  60. public function readAll() {
  61. if( $this->getFileSize() > 0 )
  62. return (fread( $this->getHandle(), $this->getFileSize() ));
  63. else
  64. return null;
  65. }
  66. public function read( $size ) {
  67. if( $this->getFileSize() > 0 )
  68. return fread( $this->getHandle(), $size );
  69. else
  70. return null;
  71. }
  72. public function readAllAndClose() {
  73. $fileContent = $this->readAll();
  74. $this->closeFile();
  75.  
  76. return $fileContent;
  77. }
  78.  
  79. /**
  80. * @param string $content
  81. * @return blFile
  82. */
  83. public function write( $content ) {
  84. fwrite( $this->getHandle(), ( $content ) );
  85. return $this;
  86. }
  87.  
  88. public function writeBuffered( $content ) {
  89. $this->_setWriteBuffer( $content );
  90. return $this;
  91. }
  92.  
  93. /*public function __destruct() {
  94. var_dump($this);
  95. if( $this->_writeBuffer != '' ) {
  96. $this->write( $this->_writeBuffer ) ;
  97. }
  98.  
  99. $this->closeFile();
  100. }*/
  101.  
  102. /**
  103. * @return blFile
  104. */
  105. public function truncate() {
  106. ftruncate( $this->getHandle(), 0);
  107. $this->pointerStart();
  108. return $this;
  109. }
  110.  
  111. public function closeFile() {
  112. if( $this->getHandle() !== null ) {
  113. fclose( $this->getHandle() );
  114. $this->_setHandle(null);
  115. }
  116. }
  117.  
  118. /**
  119. * @return blFile
  120. */
  121. public function pointerStart() {
  122. $this->_movePointer( 0 );
  123. return $this;
  124. }
  125.  
  126. /**
  127. * @return blFile
  128. */
  129. public function pointerEnd() {
  130. $this->_movePointer( self::POINTER_END );
  131. return $this;
  132. }
  133.  
  134. /**
  135. * @param int $where
  136. * @return blFile
  137. */
  138. public function pointerTo( $where ) {
  139. return $this->_movePointer( $where );
  140. return $this;
  141. }
  142.  
  143.  
  144. /*----------------------------------------------------------------------------*/
  145. /* FUNCTIONS PRIVATE
  146. /*----------------------------------------------------------------------------*/
  147. private function _movePointer( $where ) {
  148. if( $where == self::POINTER_END ) {
  149. fseek( $this->getHandle(), 0, SEEK_END);
  150. } else {
  151. fseek( $this->getHandle(), $where, SEEK_SET);
  152. }
  153. }
  154.  
  155. /*----------------------------------------------------------------------------*/
  156. /* SETTERS AND GETTERS
  157. /*----------------------------------------------------------------------------*/
  158.  
  159. private function _setWriteBuffer( $content ) {
  160. $this->_writeBuffer = $content;
  161. }
  162.  
  163. private function _getWriteBuffer() {
  164. return $this->_writeBuffer;
  165. }
  166.  
  167. public function getHandle() {
  168. return $this->_handle;
  169. }
  170.  
  171. private function _setHandle( $handle ) {
  172. $this->_handle = $handle;
  173. }
  174.  
  175. public function getPath() {
  176. return $this->_path;
  177. }
  178.  
  179. private function _setPath( $path ) {
  180. $this->_path = $path;
  181. }
  182.  
  183. public function getFileSize() {
  184. if( $this->_fileSize == null ) {
  185. $this->_fileSize = filesize( $this->getPath() );
  186. }
  187.  
  188. return $this->_fileSize;
  189. }
  190. }
  191.  
  192.  
  193. class blFileSystem {
  194. private $_errors = array();
  195. /*----------------------------------------------------------------------------*/
  196. /* FUNCTIONS PUBLIC
  197. /*----------------------------------------------------------------------------*/
  198.  
  199. /**
  200. * Trying to open file. If neccessary, creates dir and file automatically
  201. *
  202. * @param string $path
  203. * @param bool $writing
  204. * @return blFile
  205. */
  206. public function openFile( $path, $writing = false ) {
  207. if( file_exists( $path ) ) {
  208. $mode = ( $writing ) ? 'r+' : 'r';
  209. } else {
  210. $mode = ( $writing ) ? 'c+' : 'c';
  211. }
  212. return $this->_openFile( $path, $mode );
  213. }
  214.  
  215. /**
  216. * Open file, if exists, truncate
  217. * @param string $path
  218. * @param bool $writing
  219. * @return blFile
  220. */
  221. public function createFile( $path, $writing = false ) {
  222. $mode = ( $writing ) ? 'c+' : 'c';
  223. return $this->_openFile( $path, $mode );
  224. }
  225.  
  226. public function deleteFile( $path ) {}
  227.  
  228. public function createDir( $path ) {
  229. if( mkdir( $path, 0777, true ) === false ) {
  230. $this->_addError( 'Unable to create DIR :'. $path );
  231. }
  232. }
  233.  
  234. public function saveImage( $image, $path ) {
  235. $pinfo = pathinfo( $path );
  236. $ext = $pinfo['extension'];
  237. $return = null;
  238.  
  239. switch( $ext ) {
  240. case 'jpg':
  241. $return = imagejpeg($image, $path, 90 );
  242. break;
  243. case 'jpeg':
  244. $return = imagejpeg($image, $path, 90 );
  245. break;
  246. case 'png':
  247. $return = imagepng( $image, $path, 0 );
  248. break;
  249.  
  250. case 'gif':
  251. $return = imagegif( $image, $path );
  252. break;
  253. }
  254.  
  255. return $return;
  256.  
  257. }
  258.  
  259. /*----------------------------------------------------------------------------*/
  260. /* FUNCTIONS PRIVATE
  261. /*----------------------------------------------------------------------------*/
  262.  
  263. private function _openFile( $path, $mode ) {
  264. $pathInfo = pathinfo( $path );
  265. $dirname = $pathInfo['dirname'];
  266. $file = null;
  267.  
  268. if( !is_dir( $dirname ) ) {
  269. $this->createDir( $dirname );
  270. }
  271.  
  272. $fileHandler = fopen( $path, $mode);
  273. $file = new blFile( $fileHandler, $path);
  274.  
  275. return $file;
  276. }
  277.  
  278. /*----------------------------------------------------------------------------*/
  279. /* SETTERS AND GETTERS
  280. /*----------------------------------------------------------------------------*/
  281. public function getErorrs() {
  282. return $this->_errors;
  283. }
  284.  
  285. private function _addError( $error ) {
  286. $this->_errors[] = $error;
  287. }
  288.  
  289. }
  290.  
  291.  
  292. interface blIConnection {
  293. public function getContent( $url );
  294. }
  295.  
  296.  
  297. class blConnectionAdapteur implements blIConnection {
  298. /**
  299. * @var blIConnection
  300. */
  301. private $_connectionMethod = null;
  302.  
  303. /*----------------------------------------------------------------------------*/
  304. /* PUBLIC FUNCTIONS
  305. /*----------------------------------------------------------------------------*/
  306.  
  307. public function getContent( $url ) {
  308. return $this->_getConnectionMethod()->getContent($url);
  309. }
  310. /*----------------------------------------------------------------------------*/
  311. /* PRIVATE FUNCTIONS
  312. /*----------------------------------------------------------------------------*/
  313.  
  314. private function _createProperConnection() {
  315. if( ini_get('allow_url_fopen') ) {
  316. $this->_setConnectionMethod( new blConnectionFopen() );
  317. } else if( function_exists( 'curl_init') ) {
  318. $this->_setConnectionMethod( new blConnectionCurl() );
  319. }
  320. }
  321.  
  322. /*----------------------------------------------------------------------------*/
  323. /* GETTERS AND SETTERS
  324. /*----------------------------------------------------------------------------*/
  325.  
  326. private function _setConnectionMethod( blIConnection $connectionMethod ) {
  327. $this->_connectionMethod = $connectionMethod;
  328. }
  329.  
  330. /**
  331. * @return blIConnection
  332. */
  333. private function _getConnectionMethod() {
  334. if( $this->_connectionMethod == null ) {
  335. $this->_createProperConnection();
  336. }
  337. return $this->_connectionMethod;
  338. }
  339. }
  340.  
  341.  
  342. class blConnectionCurl implements blIConnection {
  343. public function getContent( $url ) {
  344. $ch = curl_init();
  345. $timeout = 5;
  346. curl_setopt($ch, CURLOPT_URL, $url);
  347. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  348. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  349. $data = curl_exec($ch);
  350. curl_close($ch);
  351.  
  352. return $data;
  353. }
  354. }
  355.  
  356.  
  357. class blConnectionFopen implements blIConnection {
  358. public function getContent( $url ) {
  359.  
  360. $handle = fopen( $url, 'rb' );
  361. $fileContent = '';
  362. if( $handle !== false ) {
  363. while (!feof($handle)) {
  364. $fileContent .= fread($handle, 8192);
  365. }
  366. fclose( $handle );
  367. }
  368. return $fileContent;
  369. }
  370. }
  371.  
  372.  
  373. class blDownloader {
  374. /**
  375. * @var blIConnection
  376. */
  377. private $_connectionMethod = null;
  378.  
  379. public function getContent( $url ) {
  380. return $this->_getConnectionMethod()->getContent($url);
  381.  
  382. }
  383. /**
  384. *
  385. * @param blIConnection $connectionMethod
  386. */
  387. private function _setConnectionMethod( blIConnection $connectionMethod ) {
  388. $this->_connectionMethod = $connectionMethod;
  389. }
  390.  
  391. /**
  392. * @return blIConnection
  393. */
  394. private function _getConnectionMethod() {
  395. if( $this->_connectionMethod == null ) {
  396. $this->_setConnectionMethod( new blConnectionAdapteur() );
  397. }
  398. return $this->_connectionMethod;
  399. }
  400. }
  401.  
  402.  
  403. class blImgCache {
  404. CONST CACHE_FILENAME = 'img_caching_info.frs';
  405.  
  406.  
  407. /**
  408. * @var blFileSystem
  409. */
  410. private $_fileSystem = null;
  411.  
  412. /**
  413. * @var blFile
  414. */
  415. private $_cacheFile = null;
  416.  
  417. private $_cacheFileUnparsed = null;
  418.  
  419. private $_cacheFileParsed = null;
  420.  
  421. private $_cacheFileDir = null;
  422.  
  423. public function __construct( blFileSystem $fileSystem, $cacheFileDir ) {
  424.  
  425. $this->_setFileSystem($fileSystem);
  426. $this->_setCacheFileDir($cacheFileDir . self::CACHE_FILENAME );
  427.  
  428. $this->_loadCacheFile();
  429. $this->_unparseCacheFile();
  430.  
  431. }
  432.  
  433. public function addCachedFileRemote( $urlNew, $pathNew, $urlOld ) {
  434. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  435.  
  436. if( $this->_cacheFileUnparsed == null ) {
  437. $this->_cacheFileUnparsed = new stdClass();
  438. }
  439. if( !isset( $cacheFileUnparsed->remoteDataHolder[ $urlOld ] ) ) {
  440. $cachedFile = new stdClass();
  441. $cachedFile->urlNew = $urlNew;
  442. $cachedFile->pathNew = $pathNew;
  443. $cachedFile->urlOld = $urlOld;
  444. $cachedFile->timestamp = time();
  445.  
  446. $cacheFileUnparsed->remoteDataHolder[ $cachedFile->urlOld] = $cachedFile;
  447. }
  448. }
  449.  
  450. public function addCachedFile( $urlNew, $urlOld, $pathNew, $pathOld, $remote = false ) {
  451. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  452.  
  453. if( $this->_cacheFileUnparsed == null ) {
  454. $this->_cacheFileUnparsed = new stdClass();
  455. }
  456. if( !isset( $cacheFileUnparsed->dataHolder[ $urlNew ] ) ) {
  457. $cachedFile = new stdClass();
  458. $cachedFile->urlNew = $urlNew;
  459. $cachedFile->urlOld = $urlOld;
  460. $cachedFile->pathNew = $pathNew;
  461. $cachedFile->pathOld = $pathOld;
  462. $cachedFile->remote = $remote;
  463. $cachedFile->timestamp = time();
  464.  
  465. $cacheFileUnparsed->dataHolder[ $cachedFile->urlNew] = $cachedFile;
  466. }
  467.  
  468.  
  469. }
  470.  
  471. public function deleteCacheInfo( $urlNew ) {
  472.  
  473.  
  474. unset( $this->_getCacheFileUnparsed()->dataHolder[ $urlNew ] );
  475. }
  476.  
  477. public function deleteRemoteCacheInfo( $url ) {
  478. unset( $this->_getCacheFileUnparsed()->remoteDataHolder[ $url ] );
  479. }
  480.  
  481. public function getCacheInfo( $urlNew ) {
  482. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  483. if( isset( $cacheFileUnparsed->dataHolder[ $urlNew ] ) ) {
  484. $cachedImageInfo = $cacheFileUnparsed->dataHolder[ $urlNew ];
  485. $cachedImageInfo->valid = $this->_checkExpiration($cachedImageInfo);
  486. return $cacheFileUnparsed->dataHolder[ $urlNew ];
  487. } else {
  488. return null;
  489. }
  490. }
  491.  
  492. public function touchCachedFile( $urlNew ) {
  493. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  494. $cacheFileUnparsed->dataHolder[ $urlNew ]->valid = true;
  495. $cacheFileUnparsed->dataHolder[ $urlNew ]->timestamp = time();
  496. }
  497.  
  498. public function getRemoteCacheInfo( $urlOld ) {
  499. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  500. if( isset( $cacheFileUnparsed->remoteDataHolder[ $urlOld ] ) ) {
  501. $cachedImageInfo = $cacheFileUnparsed->remoteDataHolder[ $urlOld ];
  502. $cachedImageInfo->valid = $this->_checkExpiration($cachedImageInfo);
  503. return $cacheFileUnparsed->remoteDataHolder[ $urlOld ];
  504. } else {
  505. return null;
  506. }
  507. }
  508.  
  509. private function _checkExpiration( stdClass $cachedImageInfo ) {
  510. $currentTimestamp = time();
  511. $oldTimestamp = $cachedImageInfo->timestamp;
  512.  
  513. if( ( $oldTimestamp + CACHE_TIME ) < $currentTimestamp ) {
  514. return false;
  515. } else {
  516. return true;
  517. }
  518. }
  519.  
  520. /*public function deleteCacheInfo( $urlNew ) {
  521. $cacheFileUnparsed = $this->_getCacheFileUnparsed();
  522. unset( $cacheFileUnparsed->dataHolder[ $urlNew ] );
  523.  
  524. }*/
  525.  
  526. public function __destruct() {
  527.  
  528. $this->saveCacheFile();
  529. }
  530.  
  531. public function saveCacheFile() {
  532.  
  533. $this->_parseCacheFile();
  534. $this->_saveCacheFile();
  535. }
  536.  
  537. private function _parseCacheFile() {
  538. $cacheFileParsed = serialize( $this->_getCacheFileUnparsed() );
  539. $this->_setCacheFileParsed( $cacheFileParsed );
  540. }
  541.  
  542. private function _saveCacheFile() {
  543. if( $this->_getCacheFile()->getHandle() == null ) return ;
  544. $this->_getCacheFile()
  545. ->truncate()
  546. ->write( $this->_getCacheFileParsed() )
  547. ->closeFile();
  548. }
  549.  
  550.  
  551. private function _loadCacheFile() {
  552. $cacheFile = $this->_getFileSystem()->openFile( $this->_getCacheFileDir(), true);
  553.  
  554. $this->_setCacheFile( $cacheFile );
  555. $this->_setCacheFileParsed( $cacheFile->readAll() );
  556.  
  557. }
  558.  
  559. private function _unparseCacheFile() {
  560. if( $this->_getCacheFileParsed() != '') {
  561. $cacheFileContentUnparsed = unserialize( $this->_getCacheFileParsed() );
  562. $cacheFileContentUnparsed->hitsAfterLastDelete++;
  563.  
  564. $this->_setCacheFileUnparsed( $cacheFileContentUnparsed );
  565. } else {
  566. $cacheFileContentUnparsed = new stdClass();
  567. $cacheFileContentUnparsed->hitsAfterLastDelete = 1;
  568. $this->_setCacheFileUnparsed( $cacheFileContentUnparsed );
  569. }
  570.  
  571. if( $cacheFileContentUnparsed->hitsAfterLastDelete >= CACHE_DELETE_FILES_check_every_x_hits ) {
  572. $this->_hardDeleteCache();
  573. }
  574. }
  575.  
  576. private function _hardDeleteCache() {
  577. //var_dump(CACHE_DELETE_FILES_check_every_x_hits);
  578. $unsetArray = array();
  579. if( !empty( $this->_getCacheFileUnparsed()->dataHolder ) ) {
  580. foreach( $this->_getCacheFileUnparsed()->dataHolder as $url => $fileData ) {
  581. //pathNew, timestamp
  582. if( ( $fileData->timestamp + CACHE_DELETE_FILES_AFTER ) <= time() ) {
  583. $unsetArray[] = $url;
  584. unlink( $fileData->pathNew);
  585. }
  586. }
  587. }
  588. foreach( $unsetArray as $oneUrl ) {
  589. unset ($this->_getCacheFileUnparsed()->dataHolder[ $oneUrl ] );
  590. }
  591.  
  592. $unsetArray = array();
  593. if( !empty( $this->_getCacheFileUnparsed()->remoteDataHolder ) ) {
  594. foreach( $this->_getCacheFileUnparsed()->remoteDataHolder as $url => $fileData ) {
  595. //pathNew, timestamp
  596. if( ( $fileData->timestamp + CACHE_DELETE_FILES_AFTER ) <= time() ) {
  597. $unsetArray[] = $url;
  598. unlink( $fileData->pathNew);
  599. }
  600. }
  601. }
  602. foreach( $unsetArray as $oneUrl ) {
  603. unset ($this->_getCacheFileUnparsed()->remoteData[ $oneUrl ] );
  604. }
  605. $this->_getCacheFileUnparsed()->hitsAfterLastDelete = 0;
  606. }
  607.  
  608.  
  609. private function _setCacheFileParsed( $cacheFileParsed ) {
  610. $this->_cacheFileParsed = $cacheFileParsed;
  611. }
  612.  
  613. private function _getCacheFileParsed() {
  614. return $this->_cacheFileParsed;
  615. }
  616.  
  617. private function _setCacheFileUnparsed( $cacheFileUnparsed ) {
  618. $this->_cacheFileUnparsed = $cacheFileUnparsed;
  619. }
  620.  
  621. private function _getCacheFileUnparsed() {
  622. return $this->_cacheFileUnparsed;
  623. }
  624.  
  625. private function _setCacheFileDir( $cacheFileDir) {
  626. $this->_cacheFileDir = $cacheFileDir;
  627. }
  628.  
  629. private function _getCacheFileDir() {
  630. return $this->_cacheFileDir;
  631. }
  632.  
  633. private function _setCacheFile( blFile $cacheFile ) {
  634. $this->_cacheFile = $cacheFile;
  635. }
  636.  
  637. /**
  638. * @return blFile
  639. */
  640. private function _getCacheFile() {
  641. return $this->_cacheFile;
  642. }
  643.  
  644. private function _setFileSystem( blFileSystem $fileSystem ) {
  645. $this->_fileSystem = $fileSystem;
  646. }
  647.  
  648. /**
  649. *
  650. * @return blFileSystem
  651. */
  652. private function _getFileSystem() {
  653. return $this->_fileSystem;
  654. }
  655. }
  656.  
  657.  
  658. class blImgDownloader {
  659. /**
  660. * @var blFileSystem
  661. */
  662. private $_fileSystem = null;
  663.  
  664. /**
  665. * @var blInputStreamAdapteour
  666. */
  667. private $_inputStream = null;
  668.  
  669. public function __construct( blFileSystem $fileSystem, blInputStreamAdapteour $inputStreamAdapter ) {
  670. $this->_setFileSystem( $fileSystem );
  671. $this->_setInputStream( $inputStreamAdapter );
  672. }
  673.  
  674. public function downloadImage( $originalPath, $newPath ) {
  675. $img = $this->_getInputStream()->open( $originalPath )->readAll();
  676. if( $img != null ) {
  677. $this->_getFileSystem()->createFile( $newPath, true)->write( $img )->closeFile();
  678. }
  679. }
  680.  
  681. private function _setInputStream( blInputStreamAdapteour $inputStreamAdapter ) {
  682. $this->_inputStream = $inputStreamAdapter;
  683. }
  684.  
  685. /**
  686. * @return blInputStreamAdapteour
  687. */
  688. private function _getInputStream() {
  689. return $this->_inputStream;
  690. }
  691.  
  692. private function _setFileSystem( blFileSystem $fileSystem ) {
  693. $this->_fileSystem = $fileSystem;
  694. }
  695. /**
  696. * @return blFileSystem
  697. */
  698. private function _getFileSystem() {
  699. return $this->_fileSystem;
  700. }
  701. }
  702.  
  703.  
  704. interface blIInputStream {
  705. public function open( $path );
  706. public function readAll();
  707.  
  708. }
  709.  
  710.  
  711. class blInputStreamFile implements blIInputStream {
  712. /**
  713. * @var blFileSystem
  714. */
  715. private $_fileSystem = null;
  716.  
  717. /**
  718. * @var blFile
  719. */
  720. private $_openedFile = null;
  721.  
  722. public function __construct() {
  723.  
  724. }
  725.  
  726. /**
  727. * (non-PHPdoc)
  728. * @see blIInputStream::open()
  729. */
  730. public function open( $path ) {
  731. $file = $this->_getFileSystem()->openFile($path);
  732. $this->_setFile( $file );
  733. return $this;
  734. }
  735.  
  736. public function readAll() {
  737. return $this->_getFile()->readAllAndClose();
  738. }
  739.  
  740. /*----------------------------------------------------------------------------*/
  741. /* SETTERS AND GETTERS
  742. /*----------------------------------------------------------------------------*/
  743. private function _setFileSystem( blFileSystem $fileSystem ) {
  744. $this->_fileSystem = $fileSystem;
  745. }
  746.  
  747. /**
  748. * @return blFileSystem
  749. */
  750. private function _getFileSystem() {
  751. if( $this->_fileSystem == null ) {
  752. $this->_setFileSystem( new blFileSystem() );
  753. }
  754.  
  755. return $this->_fileSystem;
  756. }
  757.  
  758. private function _setFile( blFile $file ) {
  759. $this->_openedFile = $file;
  760. }
  761.  
  762. /**
  763. * @return blFile
  764. */
  765. private function _getFile() {
  766. return $this->_openedFile;
  767. }
  768. }
  769.  
  770.  
  771. class blInputStreamHttp implements blIInputStream {
  772. /**
  773. * @var blDownloader
  774. */
  775. private $_downloader = null;
  776. private $_pageContent = '';
  777.  
  778. public function open( $path ) {
  779. $content = $this->_getDownloader()->getContent( $path );
  780. $this->_setPageContent( $content );
  781. }
  782.  
  783. public function readAll() {
  784. return $this->_getPageContent();
  785. }
  786.  
  787. /**
  788. * @return blDownloader
  789. */
  790. private function _getDownloader() {
  791. if( $this->_downloader == null ) {
  792. $this->_downloader = new blDownloader();
  793. }
  794.  
  795. return $this->_downloader;
  796. }
  797.  
  798. private function _setPageContent( $pageContent ) {
  799. $this->_pageContent = $pageContent;
  800. }
  801.  
  802. private function _getPageContent() {
  803. return $this->_pageContent;
  804. }
  805. }
  806.  
  807.  
  808. class blInputStreamAdapteour implements blIInputStream {
  809. /**
  810. *
  811. * @var blIInputStream
  812. */
  813. private $_inputStream = null;
  814.  
  815. /*----------------------------------------------------------------------------*/
  816. /* PUBLIC FUNCTIONS
  817. /*----------------------------------------------------------------------------*/
  818.  
  819. public function open( $path ) {
  820. $this->_createInputStream( $path );
  821. $this->_getInputStream()->open($path);
  822. return $this;
  823. }
  824.  
  825. public function readAll() {
  826. return $this->_getInputStream()->readAll();
  827. }
  828.  
  829. /*----------------------------------------------------------------------------*/
  830. /* PRIVATE FUNCTIONS
  831. /*----------------------------------------------------------------------------*/
  832. private function _createInputStream( $path ) {
  833. if( strpos( $path, 'http://') !== false ) {
  834. $this->_setInputStream( new blInputStreamHttp() );
  835. } else {
  836. $this->_setInputStream( new blInputStreamFile() );
  837. }
  838. }
  839. /*----------------------------------------------------------------------------*/
  840. /* SETTERS AND GETTERS
  841. /*----------------------------------------------------------------------------*/
  842.  
  843. private function _setInputStream( blIInputStream $inputStream ) {
  844. $this->_inputStream = $inputStream;
  845. }
  846.  
  847. private function _getInputStream() {
  848. return $this->_inputStream;
  849. }
  850. }
  851.  
  852.  
  853. class fImgOneData {
  854. public $path = null;
  855. public $url = null;
  856. public $filename = null;
  857. public $width = null;
  858. public $height = null;
  859. public $timestamp = null;
  860. public $crop = null;
  861. }
  862.  
  863. class fImgData {
  864. /**
  865. *
  866. * @var fImgOneData
  867. */
  868. public $new = null;
  869.  
  870. /**
  871. *
  872. * @var fImgOneData
  873. */
  874. public $old = null;
  875.  
  876. public $remote = false;
  877. public $ready = false;
  878.  
  879. public function __construct() {
  880. $this->new = new fImgOneData();
  881. $this->old = new fImgOneData();
  882. }
  883.  
  884. }
  885.  
  886.  
  887.  
  888.  
  889. class fImgDeliverer {
  890. /**
  891. * @var blFileSystem
  892. */
  893. private $_fileSystem = null;
  894.  
  895.  
  896. /**
  897. *
  898. * @var blInputStreamAdapteour
  899. */
  900. private $_inputStream = null;
  901.  
  902. /**
  903. * @var blImgCache
  904. */
  905. private $_imgCache = null;
  906.  
  907. /**
  908. *
  909. * @var fImgPathPredictor
  910. */
  911. private $_imgPredictor = null;
  912.  
  913.  
  914. /**
  915. *
  916. * @var blImgDownloader
  917. */
  918. private $_imgDownloader = null;
  919.  
  920. private $_uploadDir = null;
  921.  
  922. private $_uploadUrl = null;
  923.  
  924. /**
  925. *
  926. * @var fImgNamer
  927. */
  928. private $_imgNamer = null;
  929.  
  930. public function __construct( blFileSystem $fileSystem, $inputStream, blImgCache $imgCache, $uploadDir, $uploadUrl ) {
  931. $this->_setFileSystem( $fileSystem );
  932. $this->_setInputStream( $inputStream );
  933. $this->_setImgCache( $imgCache);
  934. $this->_setUploadDir( $uploadDir );
  935. $this->_setUploadUrl( $uploadUrl );
  936. }
  937.  
  938. /**
  939. *
  940. * @param fImgData $imgData
  941. * @return fImgData
  942. */
  943. public function deliveryImage( fImgData $imgData ) {
  944. $result = $this->_deliveryFromCache( $imgData );
  945. //$result = false;
  946. if( $result === false )
  947. $result = $this->_deliveryFromLocal( $imgData );
  948. if( $result === false )
  949. $result = $this->_deliveryFromRemote( $imgData );
  950.  
  951. return $result;
  952. }
  953.  
  954. private function _deliveryFromCache( fImgData $imgData ) {
  955.  
  956. $cacheInfo = $this->_getImgCache()->getCacheInfo( $imgData->new->url );
  957.  
  958. if( $cacheInfo == null ) return false;
  959.  
  960. $imgData->new->path = $this->_getUploadDirPath( $imgData->new->filename );
  961. $cacheValidity = $this->_checkCacheValidity( $imgData, $cacheInfo );
  962. if( $cacheValidity === false ) return false;
  963.  
  964. $imgData->ready = true;
  965.  
  966. return $imgData;
  967. }
  968.  
  969. private function _checkCacheValidity( fImgData $imgData, $cacheInfo ) {
  970. if( $cacheInfo->valid == true ) return true;
  971.  
  972. if( !file_exists( $cacheInfo->pathOld) ) {
  973. $this->_getImgCache()->deleteCacheInfo( $imgData->new->url );
  974. $this->_getFileSystem()->deleteFile( $imgData->new->path );
  975. return false;
  976. }
  977. $newTS = $cacheInfo->timestamp;
  978. $oldTS = filemtime( $cacheInfo->pathOld );
  979. if( $newTS > $oldTS ) {
  980. $this->_getImgCache()->touchCachedFile( $imgData->new->url );
  981. return true;
  982. } else {
  983. $this->_getImgCache()->deleteCacheInfo( $imgData->new->url );
  984. $this->_getFileSystem()->deleteFile( $imgData->new->path );
  985. return false;
  986. }
  987. }
  988.  
  989. private function _deliveryFromLocal( fImgData $imgData ) {
  990. $path = $this->_getImgPredictor()->predictPath( $imgData->old->url );
  991.  
  992. if( $path != null ) {
  993. $imgData->old->path = $path;
  994. if ($this->_doesntHasAlphaLayer($path) && false ) {
  995. $imgData->new->filename = $this->_getImgNamer()->renameToJpg( $imgData->new->filename );
  996. $imgData->new->url = $this->_getImgNamer()->renameToJpg( $imgData->new->url );
  997. }
  998. $imgData->new->path = $this->_getUploadDirPath( $imgData->new->filename );
  999. $imgData->ready = false;
  1000.  
  1001. return $imgData;
  1002. } else {
  1003. return false;
  1004. }
  1005. }
  1006.  
  1007. private function _doesntHasAlphaLayer( $path ) {
  1008. if( file_exists( $path )) {
  1009. $pathInfo = pathInfo($path);
  1010.  
  1011. if( $pathInfo['extension'] != 'png') {
  1012. return false;
  1013. }
  1014.  
  1015. $firstChars = $this->_getFileSystem()->openFile($path)->read(26);//->pointerTo(25)->read(1);
  1016. //var_dump($firstChars);
  1017. $char = $firstChars[25];
  1018. $pngType = (ord($char));
  1019. // var_dump($pngType);
  1020. }
  1021. if( $pngType == 6 ) {
  1022. return false;
  1023. } else {
  1024. return true;
  1025. }
  1026. }
  1027.  
  1028. private function _deliveryFromRemote( fImgData $imgData ) {
  1029.  
  1030. $remoteFilename = $this->_getImgNamer()->getRemoteImageName( $imgData->old->url );
  1031. $remotePath = $this->_getUploadDirPath('remote/' . $remoteFilename);
  1032. $remoteUrl = $this->_getUploadUrlPath('remote/' . $remoteFilename);
  1033.  
  1034. $remoteFileCacheInfo = $this->_getImgCache()
  1035. ->getRemoteCacheInfo($imgData->old->url);
  1036. //->getCacheInfo( $remotePath);
  1037.  
  1038. if( $remoteFileCacheInfo != null && $remoteCacheInfo->valid == false ) {
  1039.  
  1040. $remoteFileCacheInfo = null;
  1041. $this->_getImgCache()->deleteRemoteCacheInfo( $remoteUrl );
  1042. $this->_getFileSystem()->deleteFile( $remotePath );
  1043. }
  1044.  
  1045. if( $remoteFileCacheInfo == null ) {
  1046.  
  1047. $this->_getImgDownloader()->downloadImage($imgData->old->url, $remotePath );
  1048. if( file_exists( $remotePath) ) {
  1049. $this->_getImgCache()->addCachedFileRemote($remoteUrl, $remotePath, $imgData->old->url);
  1050. //$this->_getImgCache()->addCachedFile($urlNew, $urlOld, $pathNew, $pathOld, $remote)
  1051. }
  1052. }
  1053. $imgData->new->path = $this->_getUploadDirPath( $imgData->new->filename );
  1054. $imgData->old->url = $remoteUrl;
  1055. $imgData->old->path = $remotePath;
  1056. $imgData->ready = false;
  1057. $imgData->remote = true;
  1058.  
  1059. return $imgData;
  1060. }
  1061.  
  1062.  
  1063.  
  1064. /*----------------------------------------------------------------------------*/
  1065. /* SETTERS AND GETTERS
  1066. /*----------------------------------------------------------------------------*/
  1067. private function _getUploadDirPath( $path ) {
  1068. return $this->_getUploadDir() . $path;
  1069. }
  1070.  
  1071. private function _getUploadUrlPath ( $path ) {
  1072. return $this->_getUploadUrl() .'/'. $path;
  1073. }
  1074.  
  1075.  
  1076. private function _getUploadUrl() {
  1077. return $this->_uploadUrl;
  1078. }
  1079.  
  1080. private function _setUploadUrl( $uploadUrl ) {
  1081. $this->_uploadUrl = $uploadUrl;
  1082. }
  1083. private function _getImgDownloader() {
  1084. if( $this->_imgDownloader == null ) {
  1085. $this->_imgDownloader = new blImgDownloader( $this->_getFileSystem(), $this->_getInputStream() );
  1086. }
  1087.  
  1088. return $this->_imgDownloader;
  1089. }
  1090.  
  1091. private function _getImgNamer() {
  1092. if( $this->_imgNamer == null ) {
  1093. $this->_imgNamer = new fImgNamer();
  1094. }
  1095.  
  1096. return $this->_imgNamer;
  1097. }
  1098.  
  1099. private function _setUploadDir( $uploadDir ) {
  1100. $this->_uploadDir = $uploadDir;
  1101. }
  1102.  
  1103. private function _getUploadDir() {
  1104. return $this->_uploadDir;
  1105. }
  1106.  
  1107. /**
  1108. * @return fImgPathPredictor
  1109. */
  1110. private function _getImgPredictor() {
  1111. if( $this->_imgPredictor == null ) {
  1112. $this->_imgPredictor = new fImgPathPredictor();
  1113. }
  1114.  
  1115. return $this->_imgPredictor;
  1116. }
  1117.  
  1118. private function _setImgCache( blImgCache $imgCache ) {
  1119. $this->_imgCache = $imgCache;
  1120. }
  1121.  
  1122. /**
  1123. *
  1124. * @return blImgCache
  1125. */
  1126. private function _getImgCache(){
  1127. return $this->_imgCache;
  1128. }
  1129. private function _setFileSystem( blFileSystem $fileSystem ) {
  1130. $this->_fileSystem = $fileSystem;
  1131. }
  1132.  
  1133. /**
  1134. * @return blFileSystem
  1135. */
  1136. private function _getFileSystem() {
  1137. return $this->_fileSystem;
  1138. }
  1139.  
  1140. private function _setInputStream( blInputStreamAdapteour $inputStream ) {
  1141. $this->_inputStream = $inputStream;
  1142. }
  1143.  
  1144. /**
  1145. * @return blInputStreamAdapteour
  1146. */
  1147. private function _getInputStream() {
  1148. return $this->_inputStream;
  1149. }
  1150. }
  1151.  
  1152.  
  1153. class fImgNamer {
  1154. private $_defaultUrl = null;
  1155. private $_temporaryPathInfo = null;
  1156.  
  1157. public function __construct( $defaultUrl = null ) {
  1158. $this->_setDefaultUrl( $defaultUrl );
  1159. }
  1160. public function getNewImageName( $oldUrl, $width, $height = false, $crop = false, $remote = false) {
  1161. $newUrl = '';
  1162.  
  1163. $partRemote = ( $remote ) ? 'remote/' : '';
  1164. $partWidth = '-'.$width;
  1165. $partHeight = ( $height ) ? '-'.$height : '';
  1166. $partCrop = ( $crop ) ? '-c' : '';
  1167.  
  1168. //$newUrl .= $this->_getDefaultUrl() .'/';
  1169. $newUrl .= $partRemote;
  1170. $newUrl .= $this->_getUrlHash( $oldUrl ) . '_' ;
  1171. $newUrl .= $this->_getImgName( $oldUrl );
  1172. $newUrl .= $partWidth;
  1173. $newUrl .= $partHeight;
  1174. $newUrl .= $partCrop;
  1175. $newUrl .= $this->_getImgExtension();
  1176.  
  1177. return $newUrl;
  1178. }
  1179. public function renameToJpg( $path ) {
  1180. $pathInfo = ( pathinfo($path));
  1181. //var_dump( $pathInfo['dirname'] == '.');
  1182. ( $pathInfo['dirname'] == '.' ) ? $dirname = '' : $dirname = $pathInfo['dirname'] .'/';
  1183. $toReturn =$dirname . $pathInfo['filename'] . '.jpg';
  1184. //var_dump($toReturn);
  1185. return $toReturn;
  1186. }
  1187. public function getNewImageUrl( $oldUrl, $width, $height = false, $crop = false, $remote = false ) {
  1188. /**
  1189. * http://defaulturl(freshizer)/[remote]/$oldUrlHash_imgFilename-width[-height][-c(rop)].ext
  1190. */
  1191. $newUrl = '';
  1192.  
  1193. $partRemote = ( $remote ) ? 'remote/' : '';
  1194. $partWidth = '-'.$width;
  1195. $partHeight = ( $height ) ? '-'.$height : '';
  1196. $partCrop = ( $crop ) ? '-c' : '';
  1197.  
  1198. $newUrl .= $this->_getDefaultUrl() .'/';
  1199. $newUrl .= $partRemote;
  1200. $newUrl .= $this->_getUrlHash( $oldUrl ) . '_' ;
  1201. $newUrl .= $this->_getImgName( $oldUrl );
  1202. $newUrl .= $partWidth;
  1203. $newUrl .= $partHeight;
  1204. $newUrl .= $partCrop;
  1205. $newUrl .= $this->_getImgExtension();
  1206.  
  1207.  
  1208. return $newUrl;
  1209. }
  1210.  
  1211. public function getRemoteImageName( $url ) {
  1212. $pathInfo = pathinfo( $url );
  1213. $newName = '';
  1214. $newName .= $this->_getUrlHash( $url );
  1215. $newName .= '-'.$pathInfo['filename'].'.'.$pathInfo['extension'];
  1216.  
  1217. return $newName;
  1218. }
  1219.  
  1220. private function _getImgExtension() {
  1221. $pathInfo = $this->_getTemporaryPathInfo();
  1222. return '.'.$pathInfo['extension'];
  1223. }
  1224.  
  1225. private function _getImgName( $oldUrl ) {
  1226. $pathInfo = pathinfo( $oldUrl );
  1227. $this->_setTemporaryPathInfo( $pathInfo );
  1228. return $pathInfo['filename'];
  1229. }
  1230.  
  1231. private function _setTemporaryPathInfo( $pathInfo ) {
  1232. $this->_temporaryPathInfo = $pathInfo;
  1233. }
  1234.  
  1235. private function _getTemporaryPathInfo() {
  1236. return $this->_temporaryPathInfo;
  1237. }
  1238.  
  1239. private function _getUrlHash( $url ) {
  1240. return md5($url);
  1241. }
  1242.  
  1243. private function _setDefaultUrl( $defaultUrl ) {
  1244. $this->_defaultUrl = $defaultUrl;
  1245. }
  1246.  
  1247. private function _getDefaultUrl() {
  1248. return $this->_defaultUrl;
  1249. }
  1250. }
  1251.  
  1252.  
  1253. interface fIImgPathPredictor {
  1254. public function predictPath( $url );
  1255.  
  1256. /*private function _predictionJunction();
  1257. private function _predictUploads();
  1258. private function _predictThemes();
  1259. private function _predictPlugins();*/
  1260. }
  1261.  
  1262. class fAImgPathPredictor implements fIImgPathPredictor {
  1263. public function predictPath( $url ) {}
  1264.  
  1265. protected function _predictUploads() {}
  1266. protected function _predictThemes() {}
  1267. protected function _predictPlugins() {}
  1268. }
  1269.  
  1270.  
  1271. class fImgPathPredictor {
  1272.  
  1273. /**
  1274. *
  1275. * @var fIImgPathPredictor
  1276. */
  1277. private $_predictor = null;
  1278.  
  1279. public function predictPath( $url ) {
  1280. return $this->_getPredictor()->predictPath( $url );
  1281. }
  1282.  
  1283.  
  1284. private function _initializePredictor() {
  1285. global $blog_id;
  1286.  
  1287. if( is_multisite() && $blog_id != 1) {
  1288. $this->_setPredictor( new fImgPathPredictor_Multisite() );
  1289. } else {
  1290. $this->_setPredictor( new fImgPathPredictor_Single() );
  1291. }
  1292. }
  1293.  
  1294. private function _setPredictor( fIImgPathPredictor $predictor) {
  1295. $this->_predictor = $predictor;
  1296. }
  1297.  
  1298. /**
  1299. * @return fIImgPathPredictor
  1300. */
  1301. private function _getPredictor() {
  1302. if( $this->_predictor == null ) {
  1303. $this->_initializePredictor();
  1304. }
  1305.  
  1306. return $this->_predictor;
  1307. }
  1308. }
  1309. class fImgPathPredictor_Multisite extends fAImgPathPredictor implements fIImgPathPredictor {
  1310. private $_imgUrl = null;
  1311. private $_predictedPath = null;
  1312.  
  1313. public function predictPath( $url ) {
  1314. $this->_setImgUrl( $url );
  1315. $this->_predictionJunction();
  1316.  
  1317. return $this->_getPredictedPath();
  1318. }
  1319.  
  1320. protected function _predictionJunction() {
  1321. //echo $this->_getImgUrl().'xxxx';
  1322. //return;
  1323. $uploadDir = wp_upload_dir();
  1324.  
  1325. if( strpos( $this->_getImgUrl(), $uploadDir['baseurl']) !== false ) {
  1326. $this->_predictUploads();
  1327. } else if ( strpos( $this->_getImgUrl(), 'wp-content/themes') !== false ) {
  1328. $this->_predictThemes();
  1329. } else if ( strpos( $this->_getImgUrl(), 'wp-content/themes') !== false ) {
  1330. $this->_predictPlugins();
  1331. }
  1332.  
  1333. }
  1334.  
  1335. protected function _predictUploads() {
  1336. $uploadDir = wp_upload_dir();
  1337. $uploadSubpath = str_replace( $uploadDir['baseurl'],'', $this->_getImgUrl());
  1338.  
  1339. $newRelPath = $uploadDir['basedir'].$uploadSubpath;
  1340.  
  1341. if( file_exists( $newRelPath) ) {
  1342. $this->_setPredictedPath( $newRelPath );
  1343. }
  1344.  
  1345.  
  1346. }
  1347. protected function _predictThemes() {
  1348. $splitedUrl = explode('themes/', $this->_getImgUrl() ); //explode() $this->_getImgUrl();
  1349. $splitedPath = explode('themes/', get_template_directory() );
  1350. $newRelPath = $splitedPath[0].'themes/'.$splitedUrl[1];
  1351.  
  1352. if( file_exists( $newRelPath )) {
  1353. $this->_setPredictedPath( $newRelPath );
  1354. }
  1355. }
  1356. protected function _predictPlugins() {
  1357. $imgPluginDirSplitted = explode('wp-content/plugins', $this->_getImgUrl() );
  1358. $imgAfterPluginDir = $imgPluginDirSplited[1];
  1359.  
  1360. $pluginDir = WP_PLUGIN_DIR;
  1361. $newRelPath = $pluginDir . $imgAfterPluginDir;
  1362.  
  1363. if( file_exists( $newRelPath ) ) {
  1364. $this->_setPredictedPath( $newRelPath );
  1365. }
  1366.  
  1367.  
  1368.  
  1369. }
  1370.  
  1371.  
  1372. private function _getPredictedPath() {
  1373. return $this->_predictedPath;
  1374. }
  1375.  
  1376. private function _setPredictedPath( $predictedPath ) {
  1377. $this->_predictedPath = $predictedPath;
  1378. }
  1379.  
  1380. private function _setImgUrl( $imgUrl ) {
  1381. $this->_imgUrl = $imgUrl;
  1382. }
  1383.  
  1384. private function _getImgUrl() {
  1385. return $this->_imgUrl;
  1386. }
  1387. }
  1388.  
  1389. class fImgPathPredictor_Single extends fAImgPathPredictor implements fIImgPathPredictor {
  1390. private $_imgUrl = null;
  1391. private $_predictedPath = null;
  1392.  
  1393. public function predictPath( $url ) {
  1394. $this->_setImgUrl( $url );
  1395. $this->_predictionJunction();
  1396. return $this->_getPredictedPath();
  1397. }
  1398.  
  1399. protected function _predictionJunction() {
  1400. if( strpos( $this->_getImgUrl(), 'wp-content/uploads') !== false ) {
  1401. $this->_predictUploads();
  1402. } else if ( strpos( $this->_getImgUrl(), 'wp-content/themes') !== false ) {
  1403. $this->_predictThemes();
  1404. } else if ( strpos( $this->_getImgUrl(), 'wp-content/themes') !== false ) {
  1405. $this->_predictPlugins();
  1406. }
  1407.  
  1408. }
  1409.  
  1410. protected function _predictUploads() {
  1411. $imgUploadDirSplited = explode('wp-content/uploads', $this->_getImgUrl() );
  1412. $imgAfterUploadDir = $imgUploadDirSplited[1];
  1413. $wpUploadDir = wp_upload_dir();
  1414. $baseDir = $wpUploadDir['basedir'];
  1415.  
  1416. $newRelPath = $baseDir . $imgAfterUploadDir;
  1417.  
  1418. if( file_exists( $newRelPath) ) {
  1419. $this->_setPredictedPath( $newRelPath );
  1420. }
  1421.  
  1422.  
  1423. }
  1424. protected function _predictThemes() {
  1425. $imgThemeDirSplited = explode('wp-content/themes', $this->_getImgUrl() );
  1426. $imgAfterThemeDir = $imgThemeDirSplited[1];
  1427.  
  1428. $currentThemeDirSplited = explode( 'wp-content/themes', get_template_directory());
  1429. $currentThemeFolder = $currentThemeDirSplited[0];
  1430.  
  1431. $newRelPath = $currentThemeFolder . 'wp-content/themes' . $imgAfterThemeDir;
  1432.  
  1433. if( file_exists( $newRelPath )) {
  1434. $this->_setPredictedPath( $newRelPath );
  1435. }
  1436. }
  1437. protected function _predictPlugins() {
  1438. $imgPluginDirSplitted = explode('wp-content/plugins', $this->_getImgUrl() );
  1439. $imgAfterPluginDir = $imgPluginDirSplited[1];
  1440.  
  1441. $pluginDir = WP_PLUGIN_DIR;
  1442. $newRelPath = $pluginDir . $imgAfterPluginDir;
  1443.  
  1444. if( file_exists( $newRelPath ) ) {
  1445. $this->_setPredictedPath( $newRelPath );
  1446. }
  1447.  
  1448. }
  1449.  
  1450.  
  1451. private function _getPredictedPath() {
  1452. return $this->_predictedPath;
  1453. }
  1454.  
  1455. private function _setPredictedPath( $predictedPath ) {
  1456. $this->_predictedPath = $predictedPath;
  1457. }
  1458.  
  1459. private function _setImgUrl( $imgUrl ) {
  1460. $this->_imgUrl = $imgUrl;
  1461. }
  1462.  
  1463. private function _getImgUrl() {
  1464. return $this->_imgUrl;
  1465. }
  1466. }
  1467.  
  1468.  
  1469. class fImgResizer {
  1470.  
  1471. /**
  1472. *
  1473. * @var blFileSystem
  1474. */
  1475. private $_fileSystem = null;
  1476.  
  1477. /**
  1478. * @var fImgResizerCalculator
  1479. */
  1480. private $_imgResizerCalculator = null;
  1481.  
  1482. private $_imgHasAlfaChannel = null;
  1483.  
  1484. public function __construct( blFileSystem $fileSystem ) {
  1485. $this->_setFileSystem($fileSystem);
  1486. }
  1487.  
  1488. public function resize( fImgData $imgData ) {// stdClass $pathInfo, stdClass $imgInfo ) {
  1489. //var_dump($imgData);
  1490. $imageOld = $this->_openImage( $imgData->old->path );
  1491. $orig = $this->_getImgDimensions( $imgData->old->path );
  1492.  
  1493.  
  1494. $newDimensions = $this->_getImgResizerCalculator()->calculateNewDimensions( $orig->width,
  1495. $orig->height,
  1496. $imgData->new->width,
  1497. $imgData->new->height,
  1498. $imgData->new->crop );
  1499.  
  1500. $imageNew = $this->_createImage( $newDimensions['dst']['w'],$newDimensions['dst']['h']);
  1501.  
  1502.  
  1503. imagecopyresampled($imageNew, $imageOld, $newDimensions['dst']['x'],
  1504. $newDimensions['dst']['y'],
  1505. $newDimensions['src']['x'],
  1506. $newDimensions['src']['y'],
  1507. $newDimensions['dst']['w'],
  1508. $newDimensions['dst']['h'],
  1509. $newDimensions['src']['w'],
  1510. $newDimensions['src']['h']);
  1511. if(function_exists('imageantialias') ) {
  1512. imageantialias( $imageNew, true );
  1513. }
  1514. $pathInfo = pathinfo($imgData->old->path);
  1515.  
  1516. if ( $pathInfo['extension'] == 'png' && function_exists('imageistruecolor') && !imageistruecolor( $imageOld ) ) {
  1517. imagetruecolortopalette( $imageNew, false, imagecolorstotal( $imageOld ) );
  1518. }
  1519. $this->_getFileSystem()->saveImage( $imageNew, $imgData->new->path );
  1520.  
  1521. imagedestroy($imageOld);
  1522. imagedestroy($imageNew);
  1523. $this->_imgHasAlfaChannel = null;
  1524. $dimToReturn = array();
  1525.  
  1526. }
  1527.  
  1528. private function _openImage( $path ) {
  1529. $imageString = $this->_getFileSystem()->openFile( $path )->readAllAndClose();
  1530. $pathInfo = pathinfo($path);
  1531. if( $pathInfo['extension'] == 'png' && ord($imageString[25]) == 6 )
  1532. $this->_imgHasAlfaChannel = true;
  1533. //return (ord(@file_get_contents($fn, NULL, NULL, 25, 1)) == 6);
  1534. @ini_set( 'memory_limit', '256M' );
  1535. $image = imagecreatefromstring( $imageString );
  1536. return $image;
  1537. }
  1538.  
  1539.  
  1540. private function _getImgDimensions( $path ) {
  1541. $dim = getimagesize( $path );
  1542. $result = new stdClass();
  1543. $result->width = $dim[0];
  1544. $result->height = $dim[1];
  1545. return $result;
  1546. }
  1547. private function _createImage ($width, $height) {
  1548. $img = imagecreatetruecolor($width, $height);
  1549. if ( is_resource($img) && $this->_imgHasAlfaChannel && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
  1550. imagealphablending($img, false);
  1551. imagesavealpha($img, true);
  1552. }
  1553. return $img;
  1554. }
  1555.  
  1556. /*----------------------------------------------------------------------------*/
  1557. /* SETTERS AND GETTERS
  1558. /*----------------------------------------------------------------------------*/
  1559. private function _getImgResizerCalculator() {
  1560. if( $this->_imgResizerCalculator == null ) {
  1561. $this->_imgResizerCalculator = new fImgResizerCalculator();
  1562. }
  1563.  
  1564. return $this->_imgResizerCalculator;
  1565. }
  1566.  
  1567. /**
  1568. * @return blFileSystem
  1569. */
  1570. private function _getFileSystem() {
  1571. return $this->_fileSystem;
  1572. }
  1573.  
  1574. private function _setFileSystem( $fileSystem ) {
  1575. $this->_fileSystem = $fileSystem;
  1576. }
  1577. }
  1578.  
  1579.  
  1580. class fImgResizerCalculator {
  1581. public function calculateNewDimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
  1582.  
  1583. if ( $crop ) {
  1584.  
  1585. // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
  1586. $aspect_ratio = $orig_w / $orig_h;
  1587. $new_w =$dest_w;// min($dest_w, $orig_w);
  1588. $new_h =$dest_h;// min($dest_h, $orig_h);
  1589.  
  1590. if ( !$new_w ) {
  1591. $new_w = intval($new_h * $aspect_ratio);
  1592. }
  1593.  
  1594. if ( !$new_h ) {
  1595. $new_h = intval($new_w / $aspect_ratio);
  1596. }
  1597.  
  1598. $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
  1599.  
  1600. $crop_w = round($new_w / $size_ratio);
  1601. $crop_h = round($new_h / $size_ratio);
  1602.  
  1603. $s_x = floor( ($orig_w - $crop_w) / 2 );
  1604. $s_y = floor( ($orig_h - $crop_h) / 2 );
  1605. } else {
  1606. // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
  1607.  
  1608. $crop_w = $orig_w;
  1609. $crop_h = $orig_h;
  1610.  
  1611. $s_x = 0;
  1612. $s_y = 0;
  1613.  
  1614.  
  1615. list( $new_w, $new_h ) = $this->constrainNewDimensions( $orig_w, $orig_h, $dest_w, $dest_h );
  1616. }
  1617. $to_return = array();
  1618. $to_return['src']['x'] = (int)$s_x;
  1619. $to_return['src']['y'] = (int)$s_y;
  1620. $to_return['src']['w'] = (int)$crop_w;
  1621. $to_return['src']['h'] = (int)$crop_h;
  1622.  
  1623. $to_return['dst']['x'] = 0;
  1624. $to_return['dst']['y'] = 0;
  1625. $to_return['dst']['w'] = (int)$new_w;
  1626. $to_return['dst']['h'] = (int)$new_h;
  1627.  
  1628. return $to_return;
  1629. }
  1630.  
  1631. /**
  1632. * This function has been take over from wordpress core. It calculate the best proportion to uncropped image
  1633. */
  1634. public function constrainNewDimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
  1635.  
  1636. if ( !$max_width and !$max_height )
  1637. return array( $current_width, $current_height );
  1638.  
  1639.  
  1640. $width_ratio = $height_ratio = 1.0;
  1641. $did_width = $did_height = false;
  1642.  
  1643. if ( $max_width > 0 && $current_width > 0 )
  1644. {
  1645. $width_ratio = $max_width / $current_width;
  1646. $did_width = true;
  1647. }
  1648.  
  1649. if ( $max_height > 0 && $current_height > 0 )
  1650. {
  1651. $height_ratio = $max_height / $current_height;
  1652. $did_height = true;
  1653. }
  1654.  
  1655. // Calculate the larger/smaller ratios
  1656. $smaller_ratio = min( $width_ratio, $height_ratio );
  1657. $larger_ratio = max( $width_ratio, $height_ratio );
  1658.  
  1659. if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
  1660. // The larger ratio is too big. It would result in an overflow.
  1661. $ratio = $smaller_ratio;
  1662. else
  1663. // The larger ratio fits, and is likely to be a more "snug" fit.
  1664. $ratio = $larger_ratio;
  1665. //echo $current_width;
  1666. if( $max_width > $current_width && $max_height == 0 ) {
  1667. $ratio = $larger_ratio;
  1668. }
  1669.  
  1670. $w = intval( $current_width * $ratio );
  1671. $h = intval( $current_height * $ratio );
  1672.  
  1673. // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
  1674. // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
  1675. // Thus we look for dimensions that are one pixel shy of the max value and bump them up
  1676. if ( $did_width && $w == $max_width - 1 )
  1677. $w = $max_width; // Round it up
  1678. if ( $did_height && $h == $max_height - 1 )
  1679. $h = $max_height; // Round it up
  1680.  
  1681. return array( $w, $h );
  1682. }
  1683. }
  1684.  
  1685. class fImg {
  1686. /**
  1687. * @var fImg
  1688. */
  1689. private static $_instance = null;
  1690.  
  1691. /**
  1692. * @var fImgNamer
  1693. */
  1694. private $_imgNamer = null;
  1695.  
  1696. /**
  1697. * @var blFileSystem
  1698. */
  1699. private $_fileSystem = null;
  1700.  
  1701. /**
  1702. * @var blImgCache
  1703. */
  1704. private $_imgCache = null;
  1705.  
  1706. /**
  1707. * @var fImgDeliverer
  1708. */
  1709. private $_imgDeliverer = null;
  1710.  
  1711. /**
  1712. * @var blInputStreamAdapteour
  1713. */
  1714. private $_inputStream = null;
  1715.  
  1716. /**
  1717. * @var fImgResizer
  1718. */
  1719. private $_imgResizer = null;
  1720.  
  1721.  
  1722. private $_defaultUrl = null;
  1723.  
  1724. private $_defaultDir = null;
  1725.  
  1726. /*----------------------------------------------------------------------------*/
  1727. /* PUBLIC FUNCTIONS
  1728. /*----------------------------------------------------------------------------*/
  1729.  
  1730. public function __construct() {
  1731. $this->_createDefaultUrlAndDir();
  1732. $this->_setImgCache( new blImgCache( $this->_getFileSystem(), $this->_getDefaultDir() ) );
  1733. $this->_setImgDeliverer( new fImgDeliverer( $this->_getFileSystem(), $this->_getInputStream(), $this->_getImgCache(), $this->_getDefaultDir(), $this->_getDefaultUrl() ) );
  1734. $this->_setImgResizer( new fImgResizer( $this->_getFileSystem() ) );
  1735. }
  1736.  
  1737. public function getInstance() {
  1738. if( self::$_instance == null ) {
  1739. self::$_instance = new fImg();
  1740. }
  1741.  
  1742. return self::$_instance;
  1743. }
  1744.  
  1745.  
  1746. public static function ResizeC( $url, $width, $height = false, $crop = false, $returnImgSize = false ) {
  1747. $width = (int)$width;
  1748. $height = (int)$height;
  1749.  
  1750. return self::getInstance()->_resize($url, $width, $height, $crop, $returnImgSize);
  1751. }
  1752.  
  1753.  
  1754. public static function resize( $url, $width, $height = false, $crop = false, $returnImgSize = false ) {
  1755. $width = (int)$width;
  1756. $height = (int)$height;
  1757.  
  1758. return self::getInstance()->_resize($url, $width, $height, $crop, $returnImgSize = false);
  1759. }
  1760.  
  1761. public static function getImgSize( $url ) {
  1762. return self::getInstance()->_getImgSize( $url );
  1763. }
  1764.  
  1765. private function _getImgSize( $url ) {
  1766. $imgData = $this->_getImgData($url, 0, false, false);
  1767. $imgData = $this->_getImgDeliverer()->deliveryImage($imgData);
  1768. return $imgData;
  1769. }
  1770.  
  1771. private function _resize( $url, $width, $height = false, $crop = false, $returnImgSize = false ) {
  1772. if( empty($url) ) return null;
  1773.  
  1774. $imgData = $this->_getImgData($url, $width, $height, $crop);
  1775. $imgData = $this->_getImgDeliverer()->deliveryImage( $imgData );
  1776.  
  1777. if( $imgData == false ) {
  1778. echo 'Image :'.$url.' cannot be opened';
  1779. return false;
  1780. }
  1781.  
  1782. if( $imgData->ready == false ) {
  1783.  
  1784. $this->_getImgResizer()->resize( $imgData );
  1785. $this->_getImgCache()
  1786. ->addCachedFile( $imgData->new->url, $imgData->old->url, $imgData->new->path, $imgData->old->path, $imgData->remote);
  1787. }
  1788. if( $returnImgSize ) {
  1789. $size = getimagesize($imgData->new->path);
  1790.  
  1791. $toReturn = array();
  1792. $toReturn['width'] = $size[0];
  1793. $toReturn['height'] = $size[1];
  1794. $toReturn['url'] = $imgData->new->url;
  1795.  
  1796. return $toReturn;
  1797. } else {
  1798. return $imgData->new->url;
  1799. }
  1800.  
  1801. }
  1802. /*----------------------------------------------------------------------------*/
  1803. /* PRIVATE FUNCTIONS
  1804. /*----------------------------------------------------------------------------*/
  1805. /**
  1806. * @return fImgData
  1807. */
  1808. private function _getImgData( $url, $width, $height, $crop ) {
  1809. $imgData = $this->_getImageInfoAsClass($url, $width, $height, $crop);
  1810. $imgData->new->url = $this->_getImgNamer()->getNewImageUrl( $url, $width, $height, $crop);
  1811. $imgData->new->filename = $this->_getImgNamer()->getNewImageName($url, $width, $height, $crop);
  1812.  
  1813. return $imgData;
  1814. }
  1815.  
  1816. /**
  1817. *
  1818. * @return fImgData
  1819. */
  1820. private function _getImageInfoAsClass( $url, $width, $height,$crop) {
  1821. $imgData = new fImgData();
  1822. $imgData->old->url = $url;
  1823. $imgData->new->width = (int)$width;
  1824. $imgData->new->height = (int)$height;
  1825. $imgData->new->crop = $crop;
  1826.  
  1827. return $imgData;
  1828. }
  1829.  
  1830. private function _createDefaultUrlAndDir() {
  1831. $wpUploadDir = wp_upload_dir();
  1832. $this->_setDefaultUrl( $wpUploadDir['baseurl'].'/freshizer');
  1833. $this->_setDefaultDir( $wpUploadDir['basedir'].'/freshizer/');
  1834. }
  1835.  
  1836. /*----------------------------------------------------------------------------*/
  1837. /* SETTERS AND GETTERS
  1838. /*----------------------------------------------------------------------------*/
  1839. private function _setImgResizer( fImgResizer $imgResizer ) {
  1840. $this->_imgResizer = $imgResizer;
  1841. }
  1842. /**
  1843. * @return fImgResizer
  1844. */
  1845. private function _getImgResizer() {
  1846. return $this->_imgResizer;
  1847. }
  1848.  
  1849. private function _setImgDeliverer( fImgDeliverer $imgDeliverer ) {
  1850. $this->_imgDeliverer = $imgDeliverer;
  1851. }
  1852.  
  1853. /**
  1854. * @return fImgDeliverer
  1855. */
  1856. private function _getImgDeliverer() {
  1857. return $this->_imgDeliverer;
  1858. }
  1859.  
  1860. private function _setInputStream( blInputStreamAdapteour $inputStream ) {
  1861. $this->_inputStream = $inputStream;
  1862. }
  1863.  
  1864. /**
  1865. *
  1866. * @return blInputStreamAdapteour
  1867. */
  1868. private function _getInputStream() {
  1869. if( $this->_inputStream == null ) {
  1870. $this->_inputStream = new blInputStreamAdapteour() ;
  1871. }
  1872. return $this->_inputStream;
  1873. }
  1874.  
  1875. private function _setDefaultUrl( $defaultUrl ) {
  1876. $this->_defaultUrl = $defaultUrl;
  1877. }
  1878. private function _getDefaultUrl() {
  1879. return $this->_defaultUrl;
  1880. }
  1881.  
  1882. private function _getImgNamer() {
  1883. if( $this->_imgNamer == null ) {
  1884. $this->_imgNamer = new fImgNamer($this->_getDefaultUrl());
  1885. }
  1886. return $this->_imgNamer;
  1887. }
  1888.  
  1889. private function _getImgCache() {
  1890. return $this->_imgCache;
  1891. }
  1892.  
  1893. private function _setImgCache( blImgCache $imgCache ) {
  1894. $this->_imgCache = $imgCache;
  1895. }
  1896.  
  1897. private function _getFileSystem() {
  1898. if( $this->_fileSystem == null ) {
  1899. $this->_fileSystem = new blFileSystem();
  1900. }
  1901. return $this->_fileSystem;
  1902. }
  1903.  
  1904. private function _setDefaultDir( $defaultDir) {
  1905. $this->_defaultDir = $defaultDir;
  1906. }
  1907.  
  1908. private function _getDefaultDir() {
  1909. return $this->_defaultDir;
  1910. }
  1911.  
  1912. public static function DeleteCache() {
  1913.  
  1914. }
  1915. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement