External image block

chopinhauer

Joined: 2004-06-21
Posts: 9
Posted: Sat, 2005-05-21 11:40

While adding the G2 image block to my site I noticed that the imageblock:External options are quite minimal.

For example my original size pictures are all taken with a 4MPix camera so I cannot put them in an image block (it would be a waste of bandwidth for both the server the client) nor want I put just the thumbnail (cf. my Main Page for what I use image block for).

Another misfeature is that 'fullSize' gives you the original image and not the preferred source, so pictures that are rotated look horrible.

Personally I modified the ImageBlockHelper.class to select the preferred source if you give the 'fullSize' parameter. If you don't use 'fullSize' but you give a 'maxSize' it selects the biggest resize that matches the restriction (but maybe the smallest resize that is bigger than 'maxSize' would be more appropriate).

I include the diff against beta3. Feel free to use it if you consider it useful.

Piotr

PS: Thank you for the great job with G2. Even if I know little of PHP, to modify the source code is very easy and intuitive.

Index: ImageBlockHelper.class
===================================================================
RCS file: /home/karwasz/cvsroot/karwasz.org/gallery2/modules/imageblock/classes/ImageBlockHelper.class,v
retrieving revision 1.1.1.2
retrieving revision 1.2
diff -r1.1.1.2 -r1.2
86c86,87
<                  $itemType, $order, $parentId, $fullSize);
---
>                  $itemType, $order, $parentId, $fullSize,
>                  (isset($params['maxSize']) ? $params['maxSize'] : 0));
131c132,133
<     function _addBlock(&$blocks, $title, $itemType, $order, $parentId=null, $fullSize=false) {
---
>     function _addBlock(&$blocks, $title, $itemType, $order, $parentId=null,
>                        $fullSize=false, $maxSize=false) {
238,239c240,260
<               $image = $item;
<           } else {
---
>               list ($ret, $preferred) = GalleryCoreApi::fetchPreferredSource($item);
>               if ($ret->isError()) {
>                   return GalleryStatus::success();
>               }
>               $image = $preferred;
>           } else if ($maxSize) {
>               list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds(array($id));
>               if ($ret->isError()) {
>                   return $ret->wrap(__FILE__, __LINE__);
>               }
>
>               $bestSize = 0;
>               foreach ($resizes[$id] as $resize) {
>                   $size = max($resize->getWidth(), $resize->getHeight());
>                   if (($size <= $maxSize) and ($size >= $bestSize)) {
>                       $image = $resize;
>                       $bestSize = $size;
>                   }
>               }
>           }
>           if (!isset($image)) {
 
nonZero

Joined: 2006-03-15
Posts: 1
Posted: Sat, 2006-03-18 17:59

Hi!

This is exactly what I need...
... any chance you have migrated this to v.2?

Thanx,
nonZero

 
popoutdoor

Joined: 2006-10-20
Posts: 5
Posted: Fri, 2006-10-20 20:15

This is my first patch to Gallery2, thanks chopinhauer!

Gallery 2.1.2 core 1.1.0.2, ImageBlock 1.0.8

95c95,96
<               $itemType, $order, $count, $parentId, $fullSize,
---
>               $itemType, $order, $count, $parentId, $fullSize,
>               isset($params['maxSize']) ? $params['maxSize'] : 0,
150c151
<                          $parentId=null, $fullSize=false, $userId=null) {
---
>                          $parentId=null, $fullSize=false, $maxSize=false, $userId=null) {
268a270,299
>           } else if (!$fullSize && $maxSize) {
>               list ($ret, $varSizes) = GalleryCoreApi::fetchResizesByItemIds(array($id));
>               if ($ret) {
>                   return array($ret->wrap(__FILE__, __LINE__), null);
>               }
>               if (isset($varSizes[$id])) {
>                   $bestSize = 0;
>                   $size = $maxSize;
>                   foreach ($varSizes[$id] as $resize) {
>                       $size = max($resize->getWidth(), $resize->getHeight());
>                       if (($size <= $maxSize) and ($size >= $bestSize)) {
>                           $image = $resize;
>                           $bestSize = $size;
>                       }
>                   }
>               } else {        /* No resized version, fallback */
>                   list ($ret, $preferred) = GalleryCoreApi::fetchPreferredsByItemIds(array($id));
>                   if ($ret) {
>                       return array($ret->wrap(__FILE__, __LINE__), null);
>                   }
>                   $image = isset($preferred[$id]) ? $preferred[$id] : $item;
>                   $size = max($image->getWidth(), $image->getHeight());
>                   if ($size > $maxSize) {
>                       list ($ret, $thumbnail) = GalleryCoreApi::fetchThumbnailsByItemIds(array($id));
>                       if ($ret) {
>                           return array($ret->wrap(__FILE__, __LINE__), null);
>                       }
>                       $image = $thumbnail[$id];
>                   }
>               }

[edit] Add code to deal with none resized version.

Note: Current code only works with Image mode

 
popoutdoor

Joined: 2006-10-20
Posts: 5
Posted: Wed, 2006-10-25 10:12

It's hard for newcomers to get info about the api $parm...

Here comes the "error message free" update, only deals with 'GalleryDataItem', i.e. image only.

95c95,96
<               $itemType, $order, $count, $parentId, $fullSize,
---
>               $itemType, $order, $count, $parentId, $fullSize,
>               isset($params['maxSize']) ? $params['maxSize'] : 0,
150c151
<                          $parentId=null, $fullSize=false, $userId=null) {
---
>                          $parentId=null, $fullSize=false, $maxSize=false, $userId=null) {
268a270,290
>           } else if (!$fullSize && $maxSize && GalleryUtilities::isA($item, 'GalleryDataItem')) {
>               list ($ret, $ReSizes) = GalleryCoreApi::fetchResizesByItemIds(array($id));
>               if ($ret) {
>                   return array($ret->wrap(__FILE__, __LINE__), null);
>               }
>               /* No resized version, fallback */
>               if (!isset($ReSizes[$id])) {
>                   list ($ret, $ReSizes) = GalleryCoreApi::fetchDerivativesByItemIds(array($id));
>                   if ($ret) {
>                       return array($ret->wrap(__FILE__, __LINE__), null);
>                   }
>               }
>               $bestSize = 0;
>               $size = $maxSize;
>               foreach ($ReSizes[$id] as $resize) {
>                   $size = max($resize->getWidth(), $resize->getHeight());
>                   if (($size <= $maxSize) and ($size >= $bestSize)) {
>                       $image = $resize;
>                       $bestSize = $size;
>                   }
>               }
 
popoutdoor

Joined: 2006-10-20
Posts: 5
Posted: Wed, 2006-10-25 10:21

Since I'm only good at copy & paste for minor functional mods, with minimal programming skills.

Can someone help me to understand how to get 'GalleryDataItem' from 'GalleryAlbumItem'?