public function sGetImageLink($hash, $imageSize = null) { if (empty($hash)) { return ""; } $mediaService = Shopware()->Container()->get('shopware_media.media_service'); // get the image directory $imageDir = 'media/image/'; // if no imageSize was set, return the full image if (null === $imageSize) { return $mediaService->getUrl($imageDir . $hash); } // get filename and extension in order to insert thumbnail size later $extension = pathinfo($hash, PATHINFO_EXTENSION); $fileName = pathinfo($hash, PATHINFO_FILENAME); $thumbDir = $imageDir . 'thumbnail/'; // get thumbnail sizes $sizes = $this->articleMediaAlbum->getSettings()->getThumbnailSize(); foreach ($sizes as $key => &$size) { if (strpos($size, 'x') === 0) { $size = $size . 'x' . $size; } } if (isset($sizes[$imageSize])) { return $mediaService->getUrl($thumbDir . $fileName . '_' . $sizes[(int) $imageSize] . '.' . $extension); } return ""; }
public function sGetImageLink($hash, $imageSize = null) { if (!empty($hash)) { $sql = "SELECT articleID FROM s_articles_img WHERE img =?"; $articleId = Shopware()->Db()->fetchOne($sql, array($hash)); $imageSize = intval($imageSize); $image = $this->getArticleRepository()->getArticleCoverImageQuery($articleId)->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); if (empty($image)) { return ""; } //first we get all thumbnail sizes of the article album $sizes = $this->articleMediaAlbum->getSettings()->getThumbnailSize(); //now we get the configured image and thumbnail dir. $imageDir = 'http://' . $this->shop->getHost() . $this->request->getBasePath() . '/media/image/'; $thumbDir = $imageDir . 'thumbnail/'; foreach ($sizes as $key => $size) { if (strpos($size, 'x') === 0) { $size = $size . 'x' . $size; } $imageData[$key] = $thumbDir . $image['path'] . '_' . $size . '.' . $image['extension']; } if (!empty($imageData)) { return $imageData[$imageSize]; } } return ""; }
public function sGetImageLink($hash, $imageSize = null) { if (empty($hash)) { return ""; } // get the image directory $imageDir = 'http://' . $this->shop->getHost() . $this->shop->getBasePath() . '/media/image/'; // if no imageSize was set, return the full image if (null === $imageSize) { return $imageDir . $hash; } // get filename and extension in order to insert thumbnail size later $extension = pathinfo($hash, PATHINFO_EXTENSION); $fileName = pathinfo($hash, PATHINFO_FILENAME); $thumbDir = $imageDir . 'thumbnail/'; // get thumbnail sizes $sizes = $this->articleMediaAlbum->getSettings()->getThumbnailSize(); foreach ($sizes as $key => &$size) { if (strpos($size, 'x') === 0) { $size = $size . 'x' . $size; } } if (isset($sizes[$imageSize])) { return $thumbDir . $fileName . '_' . $sizes[(int) $imageSize] . '.' . $extension; } return ""; }
/** * @param Album $album * @return bool */ private function hasNoThumbnails($album) { $sizes = $album->getSettings()->getThumbnailSize(); return empty($sizes) || empty($sizes[0]) || $album->getMedia()->count() === 0; }
/** * Loads the thumbnails paths via the configured thumbnail sizes. * If the thumbnail file of a configured thumbnail size don't exists, it will be created. * @return array */ public function loadThumbnails() { if ($this->type !== self::TYPE_IMAGE) { return array(); } $sizes = array(); //concat default sizes foreach ($this->defaultThumbnails as $size) { if (count($size) === 1) { $sizes[] = $size . 'x' . $size; } else { $sizes[] = $size[0] . 'x' . $size[1]; } } //Check if the album has loaded correctly. if ($this->album !== null && $this->album->getSettings() !== null && $this->album->getSettings()->getCreateThumbnails() === 1) { $sizes = array_merge($this->album->getSettings()->getThumbnailSize(), $sizes); $sizes = array_unique($sizes); } $thumbnails = array(); //iterate thumbnail sizes foreach ($sizes as $size) { if (strpos($size, 'x') === false) { $size = $size . 'x' . $size; } $path = $this->getThumbnailDir() . str_replace('.' . $this->extension, '_' . $size . '.' . $this->extension, $this->getFileName()); //create the thumbnail if not exist. if (!file_exists($path)) { $data = explode('x', $size); $this->createThumbnail((int) $data[0], (int) $data[1]); } $path = str_replace(Shopware()->OldPath(), '', $path); if (DIRECTORY_SEPARATOR !== '/') { $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); } $thumbnails[$size] = $path; } return $thumbnails; }
/** * Creates the thumbnail files in the different sizes which configured in the album settings. * * @param \Shopware\Models\Media\Album $album * @return void */ public function createAlbumThumbnails(Album $album) { //is image media? if ($this->type !== self::TYPE_IMAGE) { return; } //Check if the album has loaded correctly and should be created for the album thumbnails? if ($album === null || $album->getSettings() === null || !$album->getSettings()->getCreateThumbnails()) { return; } //load the configured album thumbnail sizes $sizes = $album->getSettings()->getThumbnailSize(); //iterate the sizes and create the thumbnails foreach ($sizes as $size) { //split the width and height (example: $size = 70x70) $data = explode('x', $size); //check if the album thumbnail is already configured over the default thumbnails, so we don't have to create the same thumbnail again. if (in_array($data, $this->defaultThumbnails)) { continue; } // To avoid any confusing, we're mapping the index based to an association based array and remove the index based elements. $data['width'] = $data[0]; $data['height'] = $data[1]; unset($data[0]); unset($data[1]); //continue if configured size is not numeric if (!is_numeric($data['width'])) { continue; } //if no height configured, set 0 $data['height'] = (isset($data['height'])) ? $data['height'] : 0; //create thumbnail with the configured size $this->createThumbnail((int) $data['width'], (int) $data['height']); } }
/** * {@inheritDoc} */ public function setManyToOne($data, $model, $property) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setManyToOne', array($data, $model, $property)); return parent::setManyToOne($data, $model, $property); }
/** * Checks if the position changed and update the position of the old and new level. * If the parent changed the old and next level must updated. * OldLevel => from OldPosition to MAX -1 <br> * NewLevel => from newPosition to MAX +1 * * @param Shopware\Models\Media\Album $album * @param $newPosition * @param $newParent */ private function changePosition(Album $album, $newPosition, $newParent) { //position or level not changed? if ($newParent == $album->getParentId() && $newPosition == $album->getPosition()) { return; } //0 can not be used, because Doctrine would otherwise check if the album with the id 0 exists. $newParent = ($newParent == 0) ? null : $newParent; //moved up or down? down => -1 / up = +1 $step = ($newPosition < $album->getPosition()) ? 1 : -1; //Start and end values ​​determined. $fromPosition = ($step == 1) ? $newPosition - 1 : $album->getPosition(); $toPosition = ($step == 1) ? $album->getPosition() : $newPosition + 1; //same parent? Then change only the position of the current album level. if ($newParent == $album->getParentId()) { $this->changePositionBetween($fromPosition, $toPosition, $step, $album->getParentId()); } else { //change the position of the old level $this->changePositionBetween($album->getPosition(), 99999, -1, $album->getParentId()); //change the position of the new level $this->changePositionBetween($newPosition - 1, 99999, +1, $newParent); } }
/** * Returns an array of all thumbnail paths the media object can have * * @param bool $highDpi - If true, returns the file path for the high dpi thumbnails instead * @return array */ public function getThumbnailFilePaths($highDpi = false) { if ($this->type !== self::TYPE_IMAGE) { return array(); } $sizes = array(); //concat default sizes foreach ($this->defaultThumbnails as $size) { if (count($size) === 1) { $sizes[] = $size . 'x' . $size; } else { $sizes[] = $size[0] . 'x' . $size[1]; } } //Check if the album has loaded correctly. if ($this->album !== null && $this->album->getSettings() !== null && $this->album->getSettings()->getCreateThumbnails() === 1) { $sizes = array_merge($this->album->getSettings()->getThumbnailSize(), $sizes); $sizes = array_unique($sizes); } $thumbnails = array(); $suffix = $highDpi ? '@2x' : ''; //iterate thumbnail sizes foreach ($sizes as $size) { if (strpos($size, 'x') === false) { $size = $size . 'x' . $size; } $fileName = str_replace('.' . $this->extension, '_' . $size . $suffix . '.' . $this->extension, $this->getFileName()); $path = $this->getThumbnailDir() . $fileName; $path = str_replace(Shopware()->OldPath(), '', $path); if (DIRECTORY_SEPARATOR !== '/') { $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); } $thumbnails[$size] = $path; } return $thumbnails; }
/** * Converts the album properties into tree node properties. * If the album has sub-albums, iterates the children recursively. * * @param Shopware\Models\Media\Album $album * @return array */ private function getAlbumNodeProperties(\Shopware\Models\Media\Album $album) { /** @var $repository \Shopware\Models\Media\Repository */ $repository = Shopware()->Models()->Media(); $query = $repository->getAlbumMediaQuery($album->getId()); $paginator = $this->getModelManager()->createPaginator($query); //returns the total count of the query $totalResult = $paginator->count(); $parentId = null; if ($album->getParent()) { $parentId = $album->getParent()->getId(); } $node = array('id' => $album->getId(), 'text' => $album->getName(), 'position' => $album->getPosition(), 'mediaCount' => $totalResult, 'parentId' => $parentId); //to get fresh album settings from new albums too $settingsQuery = $repository->getAlbumWithSettingsQuery($album->getId()); $albumData = $settingsQuery->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); $settings = $albumData["settings"]; if (!empty($settings) && $settings !== null) { $node["iconCls"] = $settings["icon"]; $node["createThumbnails"] = $settings["createThumbnails"]; $node["thumbnailHighDpi"] = $settings["thumbnailHighDpi"]; $node["thumbnailQuality"] = $settings["thumbnailQuality"]; $node["thumbnailHighDpiQuality"] = $settings["thumbnailHighDpiQuality"]; $thumbnails = explode(";", $settings["thumbnailSize"]); $node["thumbnailSize"] = array(); $count = count($thumbnails); //convert the thumbnail to an array width the index and value for ($i = 0; $i <= $count; $i++) { if ($thumbnails[$i] === '' || $thumbnails[$i] === null) { continue; } $node["thumbnailSize"][] = array('id' => $i, 'index' => $i, 'value' => $thumbnails[$i]); } } //has sub-albums, then iterate and add the media count to the parent album. if (count($album->getChildren()) > 0) { $node['data'] = $this->toTree($album->getChildren(), $node); $node['leaf'] = false; } else { $node['leaf'] = true; } return $node; }