Example #1
0
 /**
  * Create thumbnails and update the corresponding Product records
  *
  * Scans the Products with the given IDs.  If a non-empty picture string
  * with a reasonable extension is encountered, determines whether
  * the corresponding thumbnail is available and up to date or not.
  * If not, tries to load the file and to create a thumbnail.
  * If it succeeds, it also updates the picture field with the base64
  * encoded entry containing the image width and height.
  * Note that only single file names are supported!
  * Also note that this method returns a string with information about
  * problems that were encountered.
  * It skips records which contain no or invalid image
  * names, thumbnails that cannot be created, and records which refuse
  * to be updated!
  * The reasoning behind this is that this method is currently only called
  * from within some {@link _import()} methods.  The focus lies on importing
  * Products; whether or not thumbnails can be created is secondary, as the
  * process can be repeated if there is a problem.
  * @param   integer     $arrId      The array of Product IDs
  * @return  boolean                 True on success, false on any error
  * @global  ADONewConnection  $objDatabase    Database connection object
  * @global  array
  * @static
  * @author  Reto Kohli <*****@*****.**>
  */
 static function makeThumbnailsById($arrId)
 {
     global $_ARRAYLANG;
     if (!is_array($arrId)) {
         return false;
     }
     $error = false;
     $objImageManager = new \ImageManager();
     foreach ($arrId as $product_id) {
         if ($product_id <= 0) {
             \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_INVALID_PRODUCT_ID'], $product_id));
             $error = true;
             continue;
         }
         $objProduct = Product::getById($product_id);
         if (!$objProduct) {
             \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_INVALID_PRODUCT_ID'], $product_id));
             $error = true;
             continue;
         }
         $imageName = $objProduct->pictures();
         $imagePath = \Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopPath() . '/' . $imageName;
         // only try to create thumbs from entries that contain a
         // plain text file name (i.e. from an import)
         if ($imageName == '' || !preg_match('/\\.(?:jpg|jpeg|gif|png)$/i', $imageName)) {
             \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_UNSUPPORTED_IMAGE_FORMAT'], $product_id, $imageName));
             $error = true;
             continue;
         }
         // if the picture is missing, skip it.
         if (!file_exists($imagePath)) {
             \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_MISSING_PRODUCT_IMAGE'], $product_id, $imageName));
             $error = true;
             continue;
         }
         $thumbResult = true;
         $width = 0;
         $height = 0;
         // If the thumbnail exists and is newer than the picture,
         // don't create it again.
         $thumb_name = \ImageManager::getThumbnailFilename($imagePath);
         if (file_exists($thumb_name) && filemtime($thumb_name) > filemtime($imagePath)) {
             //$this->addMessage("Hinweis: Thumbnail fuer Produkt ID '$product_id' existiert bereits");
             // Need the original size to update the record, though
             list($width, $height) = $objImageManager->_getImageSize($imagePath);
         } else {
             // Create thumbnail, get the original size.
             // Deleting the old thumb beforehand is integrated into
             // _createThumbWhq().
             $thumbResult = $objImageManager->_createThumbWhq(\Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopPath() . '/', \Cx\Core\Core\Controller\Cx::instanciate()->getWebsiteImagesShopWebPath() . '/', $imageName, \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_max_width', 'Shop'), \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_max_height', 'Shop'), \Cx\Core\Setting\Controller\Setting::getValue('thumbnail_quality', 'Shop'));
             $width = $objImageManager->orgImageWidth;
             $height = $objImageManager->orgImageHeight;
         }
         // The database needs to be updated, however, as all Products
         // have been imported.
         if ($thumbResult) {
             $shopPicture = base64_encode($imageName) . '?' . base64_encode($width) . '?' . base64_encode($height) . ':??:??';
             $objProduct->pictures($shopPicture);
             $objProduct->store();
         } else {
             \Message::error(sprintf($_ARRAYLANG['TXT_SHOP_ERROR_CREATING_PRODUCT_THUMBNAIL'], $product_id, $imageName));
             $error = true;
         }
     }
     return $error;
 }
Example #2
0
 /**
  * Rotates an image clockwise by 90�
  *
  * @global    ADONewConnection
  * @global    array
  * @global    array
  */
 function rotatePicture($intImageId)
 {
     global $objDatabase, $_ARRAYLANG, $_CONFIG;
     if (!function_exists('imagerotate')) {
         $this->strErrMessage = $_ARRAYLANG['TXT_GALLERY_GD_LIB_NOT_INSTALLED'];
         return false;
     }
     $objResult = $objDatabase->Execute('SELECT     path,
                                                 quality,
                                                 size_type,
                                                 size_proz,
                                                 size_abs_h,
                                                 size_abs_w
                                         FROM     ' . DBPREFIX . 'module_gallery_pictures
                                         WHERE     id=' . $intImageId);
     $strImagename = $objResult->fields['path'];
     $strOrgPath = $this->strImagePath;
     $strWebpath = $this->strImageWebPath;
     $strThumbPath = $this->strThumbnailPath;
     $strThumbWebpath = $this->strThumbnailWebPath;
     $objImage = new \ImageManager();
     $objImage->loadImage($strOrgPath . $strImagename);
     //Rotate the clockwise
     if ($objImage->rotateImage(270)) {
         //To save the Rotated image
         if ($objImage->saveNewImage($strOrgPath . $strImagename, true)) {
             \Cx\Lib\FileSystem\FileSystem::delete_file($strThumbPath . $strImagename);
         }
         $imageSize = $objImage->_getImageSize($strOrgPath . $strImagename);
         $intOldWidth = $imageSize[0];
         $intOldHeight = $imageSize[1];
         if ($objResult->fields['size_type'] == 'abs') {
             $intNewWidth = intval($this->arrSettings['standard_width_abs']);
             $intNewHeight = intval($this->arrSettings['standard_height_abs']);
             if ($intNewWidth == 0) {
                 // exception if width and height or 0!
                 if ($intNewHeight == 0) {
                     $intNewHeight = 100;
                 }
                 $intNewWidth = round($intOldWidth * $intNewHeight / $intOldHeight, 0);
             } else {
                 if ($intNewHeight == 0) {
                     $intNewHeight = round($intOldHeight * $intNewWidth / $intOldWidth, 0);
                 }
             }
         } else {
             $intNewWidth = round($objResult->fields['size_proz'] / 100 * $intOldWidth, 0);
             $intNewHeight = round($objResult->fields['size_proz'] / 100 * $intOldHeight, 0);
         }
         //Resize the Rotated image
         if ($objImage->resizeImageSave($strOrgPath, $strWebpath, $strImagename, $intNewWidth, $intNewHeight, $objResult->fields['quality'], $strThumbPath, $strThumbWebpath, $strImagename)) {
             if ($objResult->fields['size_type'] == 'abs') {
                 $objDatabase->Execute('    UPDATE     ' . DBPREFIX . 'module_gallery_pictures
                                 SET     size_abs_h=' . $intNewHeight . ',
                                         size_abs_w=' . $intNewWidth . '
                                 WHERE     id=' . $intImageId);
             }
         }
     }
 }