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?
Posts: 153
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?
Posts: 32509
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.
Posts: 153
Is there an explanation somewhere on how to create "derivative images" ?
Posts: 32509
as usual, there are examples.
see modules/core/ItemEditAlbum.inc
search for resize ->
newFactoryInstanceByHint('GalleryDerivativeImage',
Posts: 153
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?
Posts: 32509
sure.
but IMO you should create your mini-avatar when the user visits the profile in your CMS.
Posts: 153
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.
Posts: 32509
your module needs to liste for Entity::save events:
http://gallery.menalto.com/node/36885#comment-134279
you can learn from other modules...
Posts: 153
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
Posts: 32509
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.
Posts: 153
i realize that. that line is to fetch the thumbnail objects.
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.
Posts: 153
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?
Posts: 153
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.
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/ ?
Posts: 153
duplicate. oops
Posts: 32509
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.
Posts: 153
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.
Posts: 153
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.
Posts: 32509
get the thumbnail object
$thumbnail->fetchPath() is the path
see modules/core/classes/GalleryDerivative.class
Posts: 153
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.
Posts: 153
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?
Posts: 32509
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.
Posts: 153
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.
Posts: 32509
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
Posts: 153
oh but wait.. will a resize contain the user's modified crop preference?
Posts: 32509
please study ItemEditPhoto.inc function handleRequest().
Posts: 153
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
Posts: 32509
you'll use the original.
it takes the thumbnail preferences into account if you use the code from handleRequest.
Posts: 153
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)
Posts: 32509
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();