Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Its simple class for resizing (all) (products) images in Prestashop
  5. * There is often a problem with resizing through dummy "Resize button" - PHP has timeout,
  6. * if not then gateway timeoout ...
  7. * So here it is, use it with PHP-CLI ( max_exec_time set to unlimited ... )
  8. */
  9.  
  10. // TODO Only products images
  11. // TODO Regenerating all product images
  12.  
  13. // !!!! YOU MUST EDIT THIS
  14. $PS_HOME = "../public_html/"; // directory wheres your PrestaShop installed
  15. $PS_IMG_PATH = $PS_HOME . "img/p/"; // directory wheres your images
  16. $PS_PREFIX = "ps";
  17.  
  18. $DB_USER = "some_user";
  19. $DB_PASS = "some_password";
  20. $DB_HOST = "localhost";
  21. $DB_NAME = "some_dbName";
  22.  
  23. // DO NOT EDIT THIS BABY, UNLESS YOU KNOW WHAT ARE YOU DOING
  24. require_once $PS_HOME . "config/config.inc.php";
  25.  
  26. class psThumbResizer {
  27.  
  28. const version = 0.1;
  29.  
  30. private $psImgDir;
  31. private $dbConn;
  32. private $tablePrefix;
  33.  
  34. function __contruct() {
  35.  
  36. }
  37.  
  38. function __destructI() {
  39.  
  40. }
  41.  
  42. /**
  43. * @param bool $afterDate - NOT IMPLEMENTED
  44. * @return array|bool - false if not success, array of IDs (strings) if success
  45. */
  46. private function getImageIds( $afterDate = false) {
  47. if( !$afterDate ) {
  48. $sql = "SELECT `id_image` FROM `" . $this->tablePrefix . "_image`";
  49. $result = $this->dbConn->query($sql);
  50.  
  51. if ( ( is_object( $result ) ) && ( $result->num_rows > 0 ) ) {
  52. while($row = $result->fetch_row()) {
  53. $rows[] = $row[0];
  54. }
  55. return $rows;
  56. }
  57. return false;
  58. }
  59. }
  60.  
  61. private function createPathToImage( $imageId ) {
  62. $chars = str_split( $imageId );
  63. foreach($chars as $char){
  64. $ret[] = '/' . $char;
  65. }
  66. return implode( $ret );
  67. }
  68.  
  69. /**
  70. * @param $psImgDir - relative path to your PS_INSTALL_DIR/img/p
  71. */
  72. public function setPsImgDir( $psImgDir ) {
  73. if ( !is_dir( $psImgDir ) ) {
  74. die( $psImgDir . " is not a valid directory" );
  75. }
  76. $this->psImgDir = realpath( $psImgDir );
  77. }
  78.  
  79. public function regenerateImages() {
  80. // get Ids
  81. $arrImgIds = $this->getImageIds();
  82. $realImgPath = realpath($this->psImgDir);
  83.  
  84.  
  85. foreach ( $arrImgIds as $imagePath ) {
  86. $imgPath = $realImgPath . $this->createPathToImage( $imagePath ) . '/' . str_replace('/','', $imagePath) . ".jpg";
  87. $types = ImageType::getImagesTypes( "products" );
  88. if ( file_exists( $imgPath ) && filesize( $imgPath ) )
  89. {
  90. foreach ($types as $imageType)
  91. //echo substr($imgPath, 0, strrpos( $imgPath, '.' ) ) . '-' . stripslashes($imageType[ 'name' ] ) . '.jpg' . PHP_EOL;
  92. if (!ImageManager::resize( $imgPath, substr($imgPath, 0, strrpos( $imgPath, '.' ) ) . '-'
  93. . stripslashes($imageType[ 'name' ] ) . '.jpg' ,
  94. ( int ) ( $imageType[ 'width' ] ),
  95. ( int ) ( $imageType[ 'height' ] ) ) )
  96. {
  97. echo "ERROR: Original image " . $imagePath . " is corrupt or has bad permission on folder" . PHP_EOL;
  98. }
  99. else {
  100. echo "Regenerated image: " . substr($imgPath, 0, strrpos( $imgPath, '.' ) )
  101. . '-' . stripslashes($imageType[ 'name' ] ) . '.jpg' . PHP_EOL;
  102. }
  103. }
  104. else {
  105. echo "ERROR: Original image: " . $imgPath . " does not exists" . PHP_EOL;
  106. }
  107.  
  108. }
  109. }
  110. public function dbConnect( $dbUser, $dbPass, $dbHost, $dbName, $tablePrefix ) {
  111.  
  112. $this->dbConn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
  113. // Check connection
  114. if ($this->dbConn->connect_error) {
  115. die( "Connection to database failed: " . $this->dbConn->connect_error );
  116. }
  117.  
  118. $this->tablePrefix = $tablePrefix;
  119. $this->dbConn->set_charset('utf8');
  120. }
  121. }
  122.  
  123. $pstr = new psThumbResizer();
  124. $pstr->setPsImgDir( $PS_IMG_PATH );
  125. $pstr->dbConnect( $DB_USER, $DB_PASS, $DB_HOST, $DB_NAME, $PS_PREFIX);
  126. $pstr->regenerateImages();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement