Beispiel #1
0
 /**
  * Creates images from the original one or moves the existing ones
  * into the folders of their category.
  *
  * Required parameters are the first two ($row and $origimage) with $row being the data object with image
  * information and $origimage being the path and filename of the image to migrate. $origimage will be the one
  * stored in the original images directory of JoomGallery.
  * You can also specify $detailimage and $thumbnail which will store them in the respective folders. If you
  * don't specify them they will be created from the original image.
  *
  * [jimport('joomla.filesystem.file') has to be called afore]
  *
  * @param   object  $row          Holds information about the new image
  * @param   string  $origimage    The original image
  * @param   string  $detailimage  The detail image
  * @param   string  $thumbnail    The thumbnail
  * @param   boolean $newfilename  True if a new file name shall be generated for the files
  * @param   boolean $copy         True if the image shall be copied into the new directory, not moved
  * @param   boolean $checkOwner   Determines whether the owner ID shall be checked against the existing users
  * @return  boolean True on success, false otherwise
  * @since   1.5.0
  */
 public function moveAndResizeImage($row, $origimage, $detailimage = null, $thumbnail = null, $newfilename = true, $copy = null, $checkOwner = null)
 {
     if (is_null($copy)) {
         $copy = $this->copyImages;
     }
     if (is_null($checkOwner)) {
         $checkOwner = $this->checkOwner;
     }
     // Some checks
     if (!isset($row->id) || $row->id < 1) {
         $this->setError('Invalid image ID');
         return false;
     }
     if (!isset($row->imgfilename)) {
         $this->setError('Image file name wasn\'t found.');
         return false;
     }
     if (!isset($row->catid) || $row->catid < 1) {
         $this->setError('Invalid category ID');
         return false;
     } else {
         // If image with category ID 1 comes in we have to set
         // the category ID to the newly created one because
         // category with ID 1 is the ROOT category in JoomGallery
         if ($row->catid == 1) {
             $row->catid = $this->newCatid;
         }
     }
     if (!isset($row->catpath)) {
         $row->catpath = JoomHelper::getCatpath($row->catid);
         if (!$row->catpath) {
             $this->setError('Category with ID ' . $row->catid . ' does not exist for image with ID ' . $row->id . '. Image cannot be migrated.');
             return false;
         }
     }
     if (!isset($row->imgtitle)) {
         $row->imgtitle = str_replace(JFile::getExt($row->imgfilename), '', $row->imgfilename);
     }
     if (!isset($row->alias)) {
         // Will be created later on
         $row->alias = '';
     }
     if (!isset($row->imgauthor)) {
         $row->imgauthor = '';
     }
     if (!isset($row->imgtext)) {
         $row->imgtext = '';
     }
     if (!isset($row->imgdate) || is_numeric($row->imgdate)) {
         $date = JFactory::getDate();
         $row->imgdate = $date->toSQL();
     }
     if (!isset($row->hits)) {
         $row->hits = 0;
     }
     if (!isset($row->downloads)) {
         $row->downloads = 0;
     }
     if (!isset($row->imgvotes)) {
         $row->imgvotes = 0;
     }
     if (!isset($row->imgvotesum)) {
         $row->imgvotesum = 0;
     }
     if (!isset($row->access) || $row->access < 1) {
         $row->access = $this->_mainframe->getCfg('access');
     }
     if (!isset($row->published)) {
         $row->published = 0;
     }
     if (!isset($row->hidden)) {
         $row->hidden = 0;
     }
     if (!isset($row->imgthumbname)) {
         $row->imgthumbname = $row->imgfilename;
     }
     if (!isset($row->checked_out)) {
         $row->checked_out = 0;
     }
     if (!isset($row->owner) || !is_numeric($row->owner) || $row->owner < 1 || $checkOwner && !JUser::getTable()->load($row->owner)) {
         $row->owner = 0;
     }
     if (!isset($row->approved)) {
         $row->approved = 1;
     }
     if (!isset($row->useruploaded)) {
         $row->useruploaded = 0;
     }
     if (!isset($row->ordering)) {
         $row->ordering = 0;
     }
     if (!isset($row->params)) {
         $row->params = '';
     }
     if (!isset($row->metakey)) {
         $row->metakey = '';
     }
     if (!isset($row->metadesc)) {
         $row->metadesc = '';
     }
     // Check whether one of the images to migrate already exist in the destination directory
     $orig_exists = false;
     $img_exists = false;
     $thumb_exists = false;
     $neworigimage = $this->_ambit->getImg('orig_path', $row);
     $newdetailimage = $this->_ambit->getImg('img_path', $row);
     $newthumbnail = $this->_ambit->getImg('thumb_path', $row);
     if (JFile::exists($neworigimage)) {
         $orig_exists = true;
     }
     if (JFile::exists($newdetailimage)) {
         $img_exists = true;
     }
     if (JFile::exists($newthumbnail)) {
         $thumb_exists = true;
     }
     // Generate a new file name if requested or if a file with the current name already exists
     if ($newfilename || $orig_exists || $img_exists || $thumb_exists) {
         $row->imgfilename = $this->genFilename($row->imgtitle, $origimage, $row->catid);
         $row->imgthumbname = $row->imgfilename;
     }
     $result = array();
     // Copy or move original image into the folder of the original images
     if (!$orig_exists) {
         // If it doesn't already exists with another name try to copy or move from source directory
         if (!JFile::exists($origimage)) {
             $this->setError('Original image not found: ' . $origimage);
             return false;
         }
         $neworigimage = $this->_ambit->getImg('orig_path', $row);
         if ($copy) {
             $result['orig'] = JFile::copy(JPath::clean($origimage), JPath::clean($neworigimage));
             if (!$result['orig']) {
                 $this->setError('Could not copy original image from ' . $origimage . ' to ' . $neworigimage);
                 return false;
             }
         } else {
             $result['orig'] = JFile::move(JPath::clean($origimage), JPath::clean($neworigimage));
             if (!$result['orig']) {
                 $this->setError('Could not move original image from ' . $origimage . ' to ' . $neworigimage);
                 return false;
             }
         }
     } else {
         // If it already exists with another name copy it to a file with the new name
         if (!JFile::copy($neworigimage, $this->_ambit->getImg('orig_path', $row))) {
             $this->setError('Could not copy original image from ' . $neworigimage . ' to ' . $this->_ambit->getImg('orig_path', $row));
             return false;
         }
         // Populate the new original file name and path because it will be
         // necessary for deleting it of deleting original images is configured
         $neworigimage = $this->_ambit->getImg('orig_path', $row);
     }
     if (!$img_exists) {
         // If it doesn't already exists with another name try to copy or move from source directory or create a new one
         $newdetailimage = $this->_ambit->getImg('img_path', $row);
         if (is_null($detailimage) || !JFile::exists($detailimage)) {
             // Create new detail image
             $debugoutput = '';
             $result['detail'] = JoomFile::resizeImage($debugoutput, $neworigimage, $newdetailimage, false, $this->_config->get('jg_maxwidth'), false, $this->_config->get('jg_thumbcreation'), $this->_config->get('jg_thumbquality'), true, 0);
             if (!$result['detail']) {
                 $this->setError('Could not create detail image ' . $newdetailimage);
             }
         } else {
             // Copy or move existing detail image
             if ($copy) {
                 $result['detail'] = JFile::copy(JPath::clean($detailimage), JPath::clean($newdetailimage));
                 if (!$result['detail']) {
                     $this->setError('Could not copy detail image from ' . $detailimage . ' to ' . $newdetailimage);
                 }
             } else {
                 $result['detail'] = JFile::move(JPath::clean($detailimage), JPath::clean($newdetailimage));
                 if (!$result['detail']) {
                     $this->setError('Could not move detail image from ' . $detailimage . ' to ' . $newdetailimage);
                 }
             }
         }
     } else {
         // If it already exists with another name copy it to a file with the new name
         $result['detail'] = JFile::copy($newdetailimage, $this->_ambit->getImg('img_path', $row));
         if (!$result['detail']) {
             $this->setError('Could not copy detail image from ' . $newdetailimage . ' to ' . $this->_ambit->getImg('img_path', $row));
         }
     }
     if (!$thumb_exists) {
         // If it doesn't already exists with another name try to copy or move from source directory or create a new one
         $newthumbnail = $this->_ambit->getImg('thumb_path', $row);
         if (is_null($thumbnail) || !JFile::exists($thumbnail)) {
             // Create new thumbnail
             $debugoutput = '';
             $result['thumb'] = JoomFile::resizeImage($debugoutput, $neworigimage, $newthumbnail, $this->_config->get('jg_useforresizedirection'), $this->_config->get('jg_thumbwidth'), $this->_config->get('jg_thumbheight'), $this->_config->get('jg_thumbcreation'), $this->_config->get('jg_thumbquality'), false, $this->_config->get('jg_cropposition'));
             if (!$result['thumb']) {
                 $this->setError('Could not create thumbnail ' . $newthumbnail);
             }
         } else {
             // Copy or move existing thumbnail
             if ($copy) {
                 $result['thumb'] = JFile::copy(JPath::clean($thumbnail), JPath::clean($newthumbnail));
                 if (!$result['thumb']) {
                     $this->setError('Could not copy thumbnail from ' . $thumbnail . ' to ' . $newthumbnail);
                 }
             } else {
                 $result['thumb'] = JFile::move(JPath::clean($thumbnail), JPath::clean($newthumbnail));
                 if (!$result['thumb']) {
                     $this->setError('Could not move thumbnail from ' . $thumbnail . ' to ' . $newthumbnail);
                 }
             }
         }
     } else {
         // If it already exists with another name copy it to a file with the new name
         $result['thumb'] = JFile::copy($newthumbnail, $this->_ambit->getImg('thumb_path', $row));
         if (!$result['thumb']) {
             $this->setError('Could not copy thumbnail from ' . $newthumbnail . ' to ' . $this->_ambit->getImg('thumb_path', $row));
         }
     }
     // Delete original image if configured in JoomGallery
     if ($this->_config->get('jg_delete_original') == 1) {
         $result['delete_orig'] = JFile::delete($neworigimage);
         if (!$result['delete_orig']) {
             $this->setError('Could not delete original image ' . $neworigimage);
         }
     }
     // Create database entry
     $query = $this->_db->getQuery(true)->insert(_JOOM_TABLE_IMAGES)->columns('id, catid, imgtitle, alias, imgauthor, imgtext, imgdate, hits, downloads, imgvotes, imgvotesum, access, published, hidden, imgfilename, imgthumbname, checked_out, owner, approved, useruploaded, ordering, params, metakey, metadesc')->values((int) $row->id . ',' . (int) $row->catid . ',' . $this->_db->quote($row->imgtitle) . ',' . $this->_db->quote($row->alias) . ',' . $this->_db->quote($row->imgauthor) . ',' . $this->_db->quote($row->imgtext) . ',' . $this->_db->quote($row->imgdate) . ',' . (int) $row->hits . ',' . (int) $row->downloads . ',' . (int) $row->imgvotes . ',' . (int) $row->imgvotesum . ',' . (int) $row->access . ',' . (int) $row->published . ',' . (int) $row->hidden . ',' . $this->_db->quote($row->imgfilename) . ',' . $this->_db->quote($row->imgthumbname) . ',' . (int) $row->checked_out . ',' . (int) $row->owner . ',' . (int) $row->approved . ',' . (int) $row->useruploaded . ',' . (int) $row->ordering . ',' . $this->_db->quote($row->params) . ',' . $this->_db->quote($row->metakey) . ',' . $this->_db->quote($row->metadesc));
     $this->_db->setQuery($query);
     $result[] = $this->runQuery();
     // Create asset and alias
     $table = JTable::getInstance('joomgalleryimages', 'Table');
     $table->load($row->id);
     if ($table->check()) {
         $result['db'] = $table->store();
         if (!$result['db']) {
             $this->setError($table->getError(), true);
         }
     }
     if (!in_array(false, $result)) {
         $this->writeLogfile('Image successfully migrated: ' . $row->id . ' Title: ' . $row->imgtitle);
         return true;
     } else {
         $this->writeLogfile('-> Error migrating image: ' . $row->id . ' Title: ' . $row->imgtitle);
         return false;
     }
 }