MW gurus,
I am working on an API module to an extension and would like to create thumbnails programmatically for some images (if they don't already have them.)
The includes/filerepo/File.php file contains a createThumb() method, which looks like it's what I want. From the comment block directly above the aforementioned method: /** * Create a thumbnail of the image having the specified width/height. * The thumbnail will not be created if the width is larger than the * image's width. Let the browser do the scaling in this case. * The thumbnail is stored on disk and is only computed if the thumbnail * file does not exist OR if it is older than the image. * Returns the URL. * * .... */
However, this method always returns the url of the file itself and not the thumb. From what I can tell it never generates the thumbnail (it's not in the filesystem repo in any directory.) My code is:
<?php
# ...query to get a list of recently uploaded images (quite simple) .... $result = $dbr->select();
# loop through them and get a thumbnail & url foreach ( $result as $row ) { $title = Title::newFromText( $row->page_title, NS_FILE ); $file = wfFindFile( $title ); if ( !$file ) { continue; } $thumbnail_url = $file->createThumb( 80 ); # width in pixels ...add to the API result... } ...return...
?>
I'm sure that my query return valid page titles/namespaces, and that the files exist (both in the wiki and in the filesystem.) They are all local, and some are quite large. I'd hate to have to send the entire image and make the browser do the scaling, as the thumbnail will get reused and the resizing is only done once.
Any ideas fellow MW gurus? What am I missing?
-Daniel Renfro
Since no one has replied at this time, I thought I'd give a crack at an answer (I really don't know if this will work).
Some goodies from a quick scan of thumb.php:
$img = wfLocalFile( $fileName ); $params = array( 'width' => 100, 'height' => 100, ); $img->transform( $params, File::RENDER_NOW ); $localpath = $thumb->getPath(); $thumbPath = $img->getThumbPath( $thumbName );
That's my guess. I havn't tested it or anything. - Hunter F.
On Tue, Mar 27, 2012 at 7:03 PM, Daniel Renfro drenfro@vistaprint.comwrote:
MW gurus,
I am working on an API module to an extension and would like to create thumbnails programmatically for some images (if they don't already have them.)
The includes/filerepo/File.php file contains a createThumb() method, which looks like it's what I want. From the comment block directly above the aforementioned method: /** * Create a thumbnail of the image having the specified width/height. * The thumbnail will not be created if the width is larger than the * image's width. Let the browser do the scaling in this case. * The thumbnail is stored on disk and is only computed if the thumbnail * file does not exist OR if it is older than the image. * Returns the URL. * * .... */
However, this method always returns the url of the file itself and not the thumb. From what I can tell it never generates the thumbnail (it's not in the filesystem repo in any directory.) My code is:
<?php # ...query to get a list of recently uploaded images (quite simple) .... $result = $dbr->select(); # loop through them and get a thumbnail & url foreach ( $result as $row ) { $title = Title::newFromText( $row->page_title, NS_FILE ); $file = wfFindFile( $title ); if ( !$file ) { continue; } $thumbnail_url = $file->createThumb( 80 ); # width in pixels ...add to the API result... } ...return... ?>
I'm sure that my query return valid page titles/namespaces, and that the files exist (both in the wiki and in the filesystem.) They are all local, and some are quite large. I'd hate to have to send the entire image and make the browser do the scaling, as the thumbnail will get reused and the resizing is only done once.
Any ideas fellow MW gurus? What am I missing?
-Daniel Renfro
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Don't use RENDER_NOW. It's the complete opposite of what he's asking for. In fact NEVER use RENDER_NOW unless you are absolutely sure that you absolutely need it. The only valid use case for RENDER_NOW I can think of is if you intend to open the file yourself and do extra image processing on it. If you want to give a user a URL then you NEVER need it.
My input on this subject: - If your thumbnail size is larger than the image's size then the original image's url will be returned because it's already big enough (this IS what you want) - If you have setup a 404 handler files will only be generated when they are requested by the user. This IS what you want.
We support generating thumbnails when a file is not found. If a path looks like a thumbnail it gets sent to thumb.php where it's then rendered, sent to the user, and saved to the filesystem for future requests. This has a number of advantages: - You can delete all your thumbnails if you think you have piles ones not being used anymore. Since they will all safely be regenerated when used. - You can use thumbnails inside of css since you know the thumb will always be usable. - It speeds up your wiki. Rendering thumbnails takes time. And during the time that thumbnails are being generated php is frozen. That means that thumbnail render time adds to your wiki's page load time on upload, save, previews, view, etc... By using a 404 handler the wiki is capable of skipping the rendering serving a url and deferring that load time to a process dedicated to rendering the thumbnail. - It also lets you dedicate machines to image thumbnailing and allow your webservers to focus on serving webpages instead of spending cpu time on image processing.
One of these days I'd like to see if I could write a fuse filesystem that exposes a cache as a filesystem. ;) then you can mount that as your filesystem for thumbs/ and have all old thumbnails purged automatically. (We don't have the capability to delete thumbnails that are no longer used)
On Tue, 27 Mar 2012 22:13:23 -0700, Hunter Fernandes h.g.fern@gmail.com wrote:
Since no one has replied at this time, I thought I'd give a crack at an answer (I really don't know if this will work).
Some goodies from a quick scan of thumb.php:
$img = wfLocalFile( $fileName ); $params = array( 'width' => 100, 'height' => 100, ); $img->transform( $params, File::RENDER_NOW ); $localpath = $thumb->getPath(); $thumbPath = $img->getThumbPath( $thumbName );
That's my guess. I havn't tested it or anything.
- Hunter F.
On Tue, Mar 27, 2012 at 7:03 PM, Daniel Renfro drenfro@vistaprint.comwrote:
MW gurus,
I am working on an API module to an extension and would like to create thumbnails programmatically for some images (if they don't already have them.)
The includes/filerepo/File.php file contains a createThumb() method, which looks like it's what I want. From the comment block directly above the aforementioned method: /** * Create a thumbnail of the image having the specified width/height. * The thumbnail will not be created if the width is larger than the * image's width. Let the browser do the scaling in this case. * The thumbnail is stored on disk and is only computed if the thumbnail * file does not exist OR if it is older than the image. * Returns the URL. * * .... */
However, this method always returns the url of the file itself and not the thumb. From what I can tell it never generates the thumbnail (it's not in the filesystem repo in any directory.) My code is:
<?php # ...query to get a list of recently uploaded images (quite simple) .... $result = $dbr->select(); # loop through them and get a thumbnail & url foreach ( $result as $row ) { $title = Title::newFromText( $row->page_title, NS_FILE ); $file = wfFindFile( $title ); if ( !$file ) { continue; } $thumbnail_url = $file->createThumb( 80 ); # width in pixels ...add to the API result... } ...return... ?>
I'm sure that my query return valid page titles/namespaces, and that the files exist (both in the wiki and in the filesystem.) They are all local, and some are quite large. I'd hate to have to send the entire image and make the browser do the scaling, as the thumbnail will get reused and the resizing is only done once.
Any ideas fellow MW gurus? What am I missing?
-Daniel Renfro
I must be missing something.
My test image is 400px (width) by 300px (height.) I'd like to create a thumbnail of max-width 100px. I can get the image's url and path quite easily, but I can't seem to get the url of the thumbnail. All the methods return the path and url for the *image*, not the *thumbnail*. The only thing that comes close is the call to thumbName(), which will return the thumbnail's name (but not the url.) The createThumb() method does not do what I expect in the least -- it always seems to return a url to the *image*, and does not create anything, let along return a url to that nothing.
I checked and I've got one 404 handler, but it is very specific to employee pictures (showing a generic image when an employee has no picture on their userpage.) I'd rather not have to mess with Apache (I hate writing rewrite rules,) and would rather have MW just generate the thumbnail when it's url is requested. Either way the user is going to wait (unless the thumbnail already exists, which I'm planning on happening in the large majority of the cases.)
This code shows what I am talking about: The output of the var_dump shows the same url for both the image and it's thumbnail, when (what I expect) they should be different. ------------------------------------------------------------------------------------------------- // get the image and the thumbnail $img = wfLocalFile( $title ); $params = array( 'width' => 100, 'height' => 100, ); $thumb = $img->transform( $params );
// dump some output to test things var_dump( $img->getName(), # Example.jpg $img->getExtension(), # jpg $img->getUrl(), # /w/images/a/a9/Example.jpg $img->getPath(), # /home/wiki/wiki/images/a/a9/Example.jpg $img->canRender(), # bool(true) $img->thumbName( $params ), # 100px-Example.jpg $img->createThumb( 100 ), # /w/images/a/a9/Example.jpg $thumb->getUrl(), # /w/images/a/a9/Example.jpg $thumb->getPath() # /home/wiki/wiki/images/a/a9/Example.jpg ); die(); -------------------------------------------------------------------------------------------------
What am I missing here?
-Daniel
-----Original Message----- From: wikitech-l-bounces@lists.wikimedia.org [mailto:wikitech-l-bounces@lists.wikimedia.org] On Behalf Of Daniel Friesen Sent: Wednesday, March 28, 2012 2:31 AM To: wikitech-l@lists.wikimedia.org Subject: Re: [Wikitech-l] Creating a thumbnail with File::createThumb()
Don't use RENDER_NOW. It's the complete opposite of what he's asking for. In fact NEVER use RENDER_NOW unless you are absolutely sure that you absolutely need it. The only valid use case for RENDER_NOW I can think of is if you intend to open the file yourself and do extra image processing on it. If you want to give a user a URL then you NEVER need it.
My input on this subject: - If your thumbnail size is larger than the image's size then the original image's url will be returned because it's already big enough (this IS what you want) - If you have setup a 404 handler files will only be generated when they are requested by the user. This IS what you want.
We support generating thumbnails when a file is not found. If a path looks like a thumbnail it gets sent to thumb.php where it's then rendered, sent to the user, and saved to the filesystem for future requests. This has a number of advantages: - You can delete all your thumbnails if you think you have piles ones not being used anymore. Since they will all safely be regenerated when used. - You can use thumbnails inside of css since you know the thumb will always be usable. - It speeds up your wiki. Rendering thumbnails takes time. And during the time that thumbnails are being generated php is frozen. That means that thumbnail render time adds to your wiki's page load time on upload, save, previews, view, etc... By using a 404 handler the wiki is capable of skipping the rendering serving a url and deferring that load time to a process dedicated to rendering the thumbnail. - It also lets you dedicate machines to image thumbnailing and allow your webservers to focus on serving webpages instead of spending cpu time on image processing.
One of these days I'd like to see if I could write a fuse filesystem that exposes a cache as a filesystem. ;) then you can mount that as your filesystem for thumbs/ and have all old thumbnails purged automatically. (We don't have the capability to delete thumbnails that are no longer used)
On Tue, 27 Mar 2012 22:13:23 -0700, Hunter Fernandes h.g.fern@gmail.com wrote:
Since no one has replied at this time, I thought I'd give a crack at an answer (I really don't know if this will work).
Some goodies from a quick scan of thumb.php:
$img = wfLocalFile( $fileName ); $params = array( 'width' => 100, 'height' => 100, ); $img->transform( $params, File::RENDER_NOW ); $localpath = $thumb->getPath(); $thumbPath = $img->getThumbPath( $thumbName );
That's my guess. I havn't tested it or anything.
- Hunter F.
On Tue, Mar 27, 2012 at 7:03 PM, Daniel Renfro drenfro@vistaprint.comwrote:
MW gurus,
I am working on an API module to an extension and would like to create thumbnails programmatically for some images (if they don't already have them.)
The includes/filerepo/File.php file contains a createThumb() method, which looks like it's what I want. From the comment block directly above the aforementioned method: /** * Create a thumbnail of the image having the specified width/height. * The thumbnail will not be created if the width is larger than the * image's width. Let the browser do the scaling in this case. * The thumbnail is stored on disk and is only computed if the thumbnail * file does not exist OR if it is older than the image. * Returns the URL. * * .... */
However, this method always returns the url of the file itself and not the thumb. From what I can tell it never generates the thumbnail (it's not in the filesystem repo in any directory.) My code is:
<?php # ...query to get a list of recently uploaded images (quite simple) .... $result = $dbr->select(); # loop through them and get a thumbnail & url foreach ( $result as $row ) { $title = Title::newFromText( $row->page_title, NS_FILE ); $file = wfFindFile( $title ); if ( !$file ) { continue; } $thumbnail_url = $file->createThumb( 80 ); # width in pixels ...add to the API result... } ...return... ?>
I'm sure that my query return valid page titles/namespaces, and that the files exist (both in the wiki and in the filesystem.) They are all local, and some are quite large. I'd hate to have to send the entire image and make the browser do the scaling, as the thumbnail will get reused and the resizing is only done once.
Any ideas fellow MW gurus? What am I missing?
-Daniel Renfro
-- ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
_______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
On Wed, Mar 28, 2012 at 10:33 AM, Daniel Renfro drenfro@vistaprint.comwrote:
$img = wfLocalFile( $title ); $params = array( 'width' => 100, 'height' => 100, ); $thumb = $img->transform( $params );
// dump some output to test things var_dump( $img->createThumb( 100 ), # /w/images/a/a9/Example.jpg $thumb->getUrl(), # /w/images/a/a9/Example.jpg $thumb->getPath() # /home/wiki/wiki/images/a/a9/Example.jpg );
By any chance do you not have image scaling enabled? It might well be giving you the original image and scaling client-side.
-- brion
$wgUseImageResize is set to true, which means dynamic server-side image resizing of thumbnails should be enabled. As far as I can tell, everything *should* be setup for image-resizing/ thumbnail-generation. Brion, you're describing what is currently happening -- the server sends the url of the "thumbnail" (which is really the url of the original image), and then the browser is scaling it to fit. I'd rather not send 1Mb files across the wire when I can send a 10Kb thumbnail. I'll keep working on it, thanks for the help gurus.
Btw, here's Special:Vesion: MediaWiki 1.18.2 (r19114) PHP 5.3.3 (apache2handler) MySQL 5.0.77 Linux 2.6.18-274.17.1.el5
-Daniel
-----Original Message----- From: wikitech-l-bounces@lists.wikimedia.org [mailto:wikitech-l-bounces@lists.wikimedia.org] On Behalf Of Brion Vibber Sent: Wednesday, March 28, 2012 1:37 PM To: Wikimedia developers Subject: Re: [Wikitech-l] Creating a thumbnail with File::createThumb()
On Wed, Mar 28, 2012 at 10:33 AM, Daniel Renfro drenfro@vistaprint.comwrote:
$img = wfLocalFile( $title ); $params = array( 'width' => 100, 'height' => 100, ); $thumb = $img->transform( $params );
// dump some output to test things var_dump( $img->createThumb( 100 ), # /w/images/a/a9/Example.jpg $thumb->getUrl(), # /w/images/a/a9/Example.jpg $thumb->getPath() # /home/wiki/wiki/images/a/a9/Example.jpg );
By any chance do you not have image scaling enabled? It might well be giving you the original image and scaling client-side.
-- brion _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
OMG nevermind!
I found that $wgUseImageResize was being set to false in LocalSettings! I was looking at the wrong LocalSettings.....ah! Thanks anyway, everything working OK now, -Daniel
-----Original Message----- From: wikitech-l-bounces@lists.wikimedia.org [mailto:wikitech-l-bounces@lists.wikimedia.org] On Behalf Of Daniel Renfro Sent: Wednesday, March 28, 2012 2:00 PM To: Wikimedia developers Subject: Re: [Wikitech-l] Creating a thumbnail with File::createThumb()
$wgUseImageResize is set to true, which means dynamic server-side image resizing of thumbnails should be enabled. As far as I can tell, everything *should* be setup for image-resizing/ thumbnail-generation. Brion, you're describing what is currently happening -- the server sends the url of the "thumbnail" (which is really the url of the original image), and then the browser is scaling it to fit. I'd rather not send 1Mb files across the wire when I can send a 10Kb thumbnail. I'll keep working on it, thanks for the help gurus.
Btw, here's Special:Vesion: MediaWiki 1.18.2 (r19114) PHP 5.3.3 (apache2handler) MySQL 5.0.77 Linux 2.6.18-274.17.1.el5
-Daniel
-----Original Message----- From: wikitech-l-bounces@lists.wikimedia.org [mailto:wikitech-l-bounces@lists.wikimedia.org] On Behalf Of Brion Vibber Sent: Wednesday, March 28, 2012 1:37 PM To: Wikimedia developers Subject: Re: [Wikitech-l] Creating a thumbnail with File::createThumb()
On Wed, Mar 28, 2012 at 10:33 AM, Daniel Renfro drenfro@vistaprint.comwrote:
$img = wfLocalFile( $title ); $params = array( 'width' => 100, 'height' => 100, ); $thumb = $img->transform( $params );
// dump some output to test things var_dump( $img->createThumb( 100 ), # /w/images/a/a9/Example.jpg $thumb->getUrl(), # /w/images/a/a9/Example.jpg $thumb->getPath() # /home/wiki/wiki/images/a/a9/Example.jpg );
By any chance do you not have image scaling enabled? It might well be giving you the original image and scaling client-side.
-- brion _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
_______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
wikitech-l@lists.wikimedia.org