creating mini avatars

cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Thu, 2005-10-27 23:27

I would like for to be able to have a cache of a minature version of all the highligted photos in my user albums.

Currently I use these photos for the user profiles and as an avatar for their posts on the forum.
My thumbnail size is 111 px

I have a place in other parts of my CMS however where I would like a 60px version of these.
i don't really want these sizes to show up in the gallery though as an option for viewing.

Any suggestions?

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Sun, 2005-10-30 21:42

Is there a way I can use the G2 API in order to create these mini avatars and save them to a directory of my choice?

Is there a way I could intercept changes made to the highlighted image in the UserAlbums and trigger a process to update the mini avatars?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-10-31 00:05

maybe look at the mediawiki integration or the wp integration. i guess at least the mediawiki integration creates some resizes. not sure though.
you'll have to create derivative images.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-10-31 04:12

Is there an explanation somewhere on how to create "derivative images" ?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-10-31 13:51

as usual, there are examples.

see modules/core/ItemEditAlbum.inc

search for resize ->
newFactoryInstanceByHint('GalleryDerivativeImage',

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Thu, 2005-12-08 07:54

In order to do this properly I can see that I am going to have to trigger the mini_avatar generation when the thumbnail is generated.

This probably means every time a user changes the highlight image for their gallery or changes the thumbnail itself with a crop.

Is it currently possible for me to create a module which would create a mini_thumbnail when either of these things happens?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-12-08 12:52

sure.

but IMO you should create your mini-avatar when the user visits the profile in your CMS.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Thu, 2005-12-08 19:58

the 48px mini avatars will be used all over the site.
in various user directories, who's online, etc..
the user profile actually uses the standard gallery sized thumbnail of 111px.

I could potentially make a "recreate avatar" link within the profile page perhaps.
but this would be an extra step for the users who usually expect to see results of changes immediately.

Is there any documentation I can find anywhere about how to make a gallery2 module like this?

IE. How do I get in there to tell gallery when to execute my code?

The mini_avatar only need be created whenever the "highlited image" thumbnail changes.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-12-08 20:40

your module needs to liste for Entity::save events:

http://gallery.menalto.com/node/36885#comment-134279

you can learn from other modules...

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Fri, 2005-12-16 10:29

for the time being i will be creating the avatars myself with php function.

list ($ret,$thumbnails) = GalleryCoreApi::fetchThumbnailsByItemIds($Ids);
list($id,$thumbnail)=each($thumbnails);
$avatarID = $thumbnail->getId();

How do I get the file path?
It doesn't seem to work to open them as urls.

$handle = imagecreatefromjpeg($url);

Warning: imagecreatefromjpeg(): 'http://dev.tribalharmonix.org/phpgallery/main.php?g2_view=core.DownloadItem&g2_itemId=2582' is not a valid JPEG file in

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-12-16 11:15

GalleryCoreApi::fetchThumbnailsByItemIds($Ids);

will not generate thumbnails for you. it will only return thumbnails that already exist. but most items should already have a thumbnail...

list($id,$thumbnail)=each($thumbnails);

$thumbnails as returned by fetchThumbnailsByItemIds is an array($id => $thumbnail, $id => $thumbnail, ...) for each $id in array $ids. so the above code doesn't make sense.

$avatarID = $thumbnail->getId();

is fine, if $thumbnail was a thumbnail object. but because of your previous line, this is probably wrong.

once you have the thumbnail id, to generate url, use:
global $gallery;
$urlGen =& $gallery->getUrlgenerator();
$url = $urlGen->generateUrl(array('view' => 'core.DownloadItem', 'itemId' => $thumbnailId));

and
$handle = imagecreatefromjpeg($url);

doesn't make any sense at all. or...i guess you want to generate create a mini thumbnail based on the thumbnail of g2.
if so, there's sure a better way.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Fri, 2005-12-16 19:53
Quote:
GalleryCoreApi::fetchThumbnailsByItemIds($Ids);
will not generate thumbnails for you. it will only return thumbnails that already exist. but most items should already have a thumbnail...

i realize that. that line is to fetch the thumbnail objects.

Quote:
$avatarID = $thumbnail->getId();

is fine, if $thumbnail was a thumbnail object. but because of your previous line, this is probably wrong.

actually $thumbnail is a thumbnail object.. i do get the $avatarID without a problem.

the thing is I don't wan to generate the URL.
I want to get the physical location of the thumbnail and use it in imagecreatefromjpeg because the url doesn't work. It is supposed to be able to take a file path or an URL. but it seems that http://dev.tribalharmonix.org/phpgallery/main.php?g2_view=core.DownloadItem&g2_itemId=2582
is not a recognized jpeg by imagecreatefromjpeg() for some reason.

If I can open the image then i can resize it and save it where I want.
If there is a better way to do this please let me know what it is.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Fri, 2005-12-16 22:39

soo...

rather than..

$url = $urlGen->generateUrl(array('view' => 'core.DownloadItem', 'itemId' => $thumbnailId));

how do i get the path to the actual physical file on the hard drive?

i'm really trying to get this done as soon as i can now..

also.. what was that better way to do this?

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-12-19 20:33

I still can't figure out why this doesn't work...

$handle=imagecreatefromjpeg("http://dev.tribalharmonix.org/phpgallery/main.php?g2_view=core.DownloadItem&g2_itemId=2582");

I have a feeling the "better way" has something to do with this.

Quote:
- Derivatives:
Thumnails and resizes have something in common. They are both generated from the original photos. Using another term, they are both "derived" from an original. Thus, we call them both "derivative". In OOP, you often try to collect the attributes and features of similar things (classes) in a more general thing. And this is here the case. thumbnails and resizes are very similar, thus we introduced the derivatives.
There are several types of derivatives: Thumbnails, resizes and preferred resizes. Preferred resizes are generazed for non-webviewable data items, if possible. e.g. a same-size (jpeg) image of a PDF, of a TIFF, of a RAW file, etc. other resizes and thumbnails can then be generated from the preferred resize instead of from the non-webviewable format.
The mentioned derivatives are all derivatives of a photo. But in G2, you can have derivatives of any file type. Well, there isn't any other derivatives than the mentioned ones, but you could extend G2 with a module to create a smaller movie of a long movie, or a low bit rate version of a high fidelity song.
Thus we have the GalleryDerivative entity, which can be anything. And we have the GalleryDerivativeImage which is either a thumb, a resize or a preferred resize. A GalleryDerivativeImage extends the GalleryDerivative (OOP), so it has all the properties and functions of a GalleryDerivative too.

Given a $GalleryItemID or even a thumbnail object..

How do I create a 48px square image and save it in $DOCUMENT_ROOT/image_db/minivatars/ ?

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-12-19 20:34

duplicate. oops

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-12-19 21:56

if you use G2's api to create resizes (the 48px mini avatar would be a resize and not a thumbnail) then it would be stored in G2's storage directory. you'd have to use the $url = $urlGenerator->generateUrl(array('view' => 'core.DownloadItem', 'itemId' => $miniAvatarId));

how to create a mini avatar resize?
see: modules/core/ItemEditPhoto.inc

1. get the $item object for the photo
2. starting from line 105 and get the $operations from above etc.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-12-19 23:32

if it will store them in the gallery database and put them in the G2 storage directory then this is not what i want to do.
It sounds like I really do need to generate the images myself.

how do i get the path to the actual physical thumbnail file on the hard drive?

This does not work even though it supposesedly points to a valid jpeg.

Quote:
$handle=imagecreatefromjpeg("http://dev.tribalharmonix.org/phpgallery/main.php?g2_view=core.DownloadItem&g2_itemId=2582");

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Tue, 2005-12-20 01:19

The reason I definately want to modify the thumbnail as opposed to creating a new resize is because the user then has the option to choose the crop for the thumbnail.

I know I've asked this a number of times already but... i still really want to know.

how do i get the path to the actual physical thumbnail file on the hard drive?

The url does not seem to do it. unless you can tell me why the above imagecreatefromjpeg would not work.

thanks.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-12-20 02:05

get the thumbnail object
$thumbnail->fetchPath() is the path

see modules/core/classes/GalleryDerivative.class

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Tue, 2005-12-20 06:28

Is there anything weird about the jpg's generated by G2 for thumbnails besides the extension?

Have any ideas why I would get this error?

Warning: imagecreatefromjpeg(): '/website/gallerydata/cache/derivative/2/5/2582.dat' is not a valid JPEG file in

the file exists.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Tue, 2005-12-20 06:38

ah.. i see.. because that particular one is a png

does gallery make thumbnails png's if the main file is a png?

if so, is there a way to make it a consistent file type?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-12-20 08:58

g2 makes thumbnail png's and jpg's i guess that's it. maybe also gif. you shouldn't care about it. and no, there's no way to force to generate .jpg for png originals.

use $derivative->getMimeType() to get the mimetype of the derivative.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Tue, 2005-12-20 09:10

i wrote my own function that i am creating the thumbnails with and putting them my /image_db/mini_avatars/ dir rather than in the g2 system.
i didn't really want these little ones to be browseable in the gallery.

but my function makes all the mini_thumbnails jpegs.
i can see how it might be practical though to preserve the format to handle things like animated gifs and transparent pixels.
i may end up switching to using the gallery API after all.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-12-20 09:30

after all, you could do:
1. generate resize with G2 API
2. copy the file (if you don't want them to be browsable)
3. remove the resize in G2 with the API

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Tue, 2005-12-20 09:38

oh but wait.. will a resize contain the user's modified crop preference?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-12-20 09:53

please study ItemEditPhoto.inc function handleRequest().

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-12-26 18:27

sorry, I don't see what I am supposed to be looking for.

Can i make resizes of my 111px user cropped thumbnail or do I have to go from the original?

does this take into account the user's thumbnail crop preferences?

GalleryCoreApi::newFactoryInstanceByHint

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-12-26 18:39

you'll use the original.
it takes the thumbnail preferences into account if you use the code from handleRequest.

 
cosmicelf

Joined: 2005-08-21
Posts: 153
Posted: Mon, 2005-12-26 19:58

i don't really understand.

say i have a 640 x 480 jpeg.
my cropped thumbnail is 111px x 111px

i want a 48px x 48px version of the thumbnail (not the original).

If I use the original to make a resize (as opposed to the thumbnail) that is 48px x 48px how does it know where i told it to crop the thumbnail?
why will it look at that if i am asking it for a resize and not a thumbnail?

is that what this does?

GalleryCoreApi::fetchPreferredSource($item)

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-12-26 21:27

then stop talking about thumbnails.

as i explained before, you'll have to copy code from modules/core/ItemEditPhoto.inc function handleRequest.

then you can run

list ($ret, $derivative, $wasBuilt) =
GalleryCoreApi::rebuildDerivativeCacheIfNotCurrent($resize->getId(), true);
if ($ret) {
return array($ret->wrap(__FILE__, __LINE__), null, null);
}

to generate the binary image file.
finally, you can copy the generated resize file and then call $resize->delete();