/** * @param RokGallery_Model_File $file * @throws RokGallery_Job_Exception */ protected function createInitialAdminSlice(RokGallery_Model_File &$file) { $default_x = RokGallery_Config::DEFAULT_ADMIN_THUMB_XSIZE; $default_y = RokGallery_Config::DEFAULT_ADMIN_THUMB_YSIZE; $admin_slice = RokGallery_Model_Slice::createNew($file, 'Admin Thumbnail', 'Admin Thumbnail', false, true); $modifications = array(); // Create the Admin Slice $source_aspect_ratio = $file->xsize / $file->ysize; $desired_aspect_ratio = $default_x / $default_y; if (!($file->xsize < $default_x && $file->ysize < $default_y)) { $resize_width = $default_x; $resize_height = $default_y; $crop_left = 0; $crop_top = 0; if ($source_aspect_ratio > $desired_aspect_ratio) { $resize_height = $default_y; $resize_width = (int) round($default_y * $source_aspect_ratio); $crop_left = (int) round(($resize_width - $default_x) / 2); } elseif ($source_aspect_ratio < $desired_aspect_ratio) { $resize_width = $default_x; $resize_height = (int) round($default_x / $source_aspect_ratio); $crop_top = (int) round(($resize_height - $default_y) / 2); } $modifications[] = new RokGallery_Manipulation_Action_Resize(array('width' => $resize_width, 'height' => $resize_height)); $modifications[] = new RokGallery_Manipulation_Action_Crop(array('left' => $crop_left, 'top' => $crop_top, 'width' => $default_x, 'height' => $default_y)); } $admin_slice->manipulations = $modifications; }
/** * @param RokGallery_Model_Gallery $gallery */ public function updateSlicesForGallery(RokGallery_Model_Gallery &$gallery) { try { $manipulations = array(); if ($gallery->keep_aspect) { $gallery_aspect_ratio = $gallery->width / $gallery->height; $image_aspect_ratio = $this->xsize / $this->ysize; $resize_height = $gallery->height; $resize_width = $gallery->width; if ($image_aspect_ratio < $gallery_aspect_ratio) { $resize_height = $gallery->height; $resize_width = (int) round($gallery->height * $image_aspect_ratio); } elseif ($image_aspect_ratio > $gallery_aspect_ratio) { $resize_width = $gallery->width; $resize_height = (int) round($gallery->width / $image_aspect_ratio); } $manipulations[] = new RokGallery_Manipulation_Action_Resize(array('width' => $resize_width, 'height' => $resize_height)); } else { // Create thumbnail but dont make thumbnail keep the aspect ratio $source_aspect_ratio = $this->xsize / $this->ysize; $desired_aspect_ratio = $gallery->width / $gallery->height; $temp_width = $gallery->width; $temp_height = $gallery->height; $temp_left = 0; $temp_top = 0; if ($source_aspect_ratio > $desired_aspect_ratio) { $temp_height = $gallery->height; $temp_width = (int) round($gallery->height * $source_aspect_ratio); $temp_left = (int) round(($temp_width - $gallery->width) / 2); $temp_top = 0; } elseif ($source_aspect_ratio < $desired_aspect_ratio) { $temp_width = $gallery->width; $temp_height = (int) round($gallery->width / $source_aspect_ratio); $temp_left = 0; $temp_top = (int) round(($temp_height - $gallery->height) / 2); } $manipulations[] = new RokGallery_Manipulation_Action_Resize(array('width' => $temp_width, 'height' => $temp_height)); $manipulations[] = new RokGallery_Manipulation_Action_Crop(array('left' => $temp_left, 'top' => $temp_top, 'width' => $gallery->width, 'height' => $gallery->height)); } $slices = $this->getSlicesForGallery($gallery->id); if ($slices === false) { $slices = array(); $slice = RokGallery_Model_Slice::createNew($this, $this->title, $this->description, $gallery->auto_publish); $slice->Gallery = $gallery; $slices[] = $slice; } foreach ($slices as &$slice) { // skip changing manipulations if force_image_size is set and image size is the same position could have been modified if (!$gallery->keep_aspect && $gallery->force_image_size && $gallery->width == $slice->xsize && $gallery->height == $slice->ysize) { $manipulations = $slice->manipulations; } if ($slice->Gallery->id == $gallery->id) { $slice->manipulations = $manipulations; $slice->save(); if (!file_exists($slice->getFullPath()) || $gallery->thumb_xsize != $slice->thumb_xsize || $gallery->thumb_ysize != $slice->thumb_ysize || $gallery->thumb_keep_aspect != $slice->thumb_keep_aspect || $gallery->thumb_background != $slice->thumb_background) { $slice->thumb_xsize = $gallery->thumb_xsize; $slice->thumb_ysize = $gallery->thumb_ysize; $slice->thumb_keep_aspect = $gallery->thumb_keep_aspect; $slice->thumb_background = $gallery->thumb_background; $slice->generateThumbnail($gallery->thumb_xsize, $gallery->thumb_ysize, $gallery->thumb_keep_aspect, $gallery->thumb_background); $slice->save(); } } } } catch (Exception $e) { throw $e; } }
/** * Update the slice * $params object should be a json like * <code> * { * 'id': 1 * 'slice':{'title':'new title','description':'new description'} * } * </code> * * @param $params * @return RokCommon_Ajax_Result */ public function create($params) { $result = new RokCommon_Ajax_Result(); try { $file = RokGallery_Model_FileTable::getSingle($params->fileId); /** @var $slice RokGallery_Model_Slice */ $slice =& RokGallery_Model_Slice::createNew($file); if ($slice === false) { throw new RokCommon_Ajax_Exception(rc__('ROKGALLERY_UNABLE_TO_FIND_SLICE_N', $params->id)); } foreach ($params->slice as $field => $value) { // change the manipulation format if passed if ($field == 'manipulations') { $value = RokGallery_Manipulation_Helper::unserializeFromJson($value); } // Add any tags if they are passed if ($field == 'Tags') { if (!empty($value) && is_array($value)) { foreach ($value as $tag) { $slice->addTag($tag); } } continue; } $slice->{$field} = $value; } $slice->save(); $slice->populateFilterInfo(); $slice->manipulations = RokGallery_Manipulation_Helper::prepSerializedForJson($slice->manipulations); $slice->Tags; $slice->FileTags; $slice->File; $result->setPayload(array('slice' => $slice->toJsonableArray())); } catch (Exception $e) { throw $e; } return $result; }