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: [email protected] 
[mailto:[email protected]] On Behalf Of Daniel Friesen
Sent: Wednesday, March 28, 2012 2:31 AM
To: [email protected]
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 <[email protected]>
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
> <[email protected]>wrote:
>
>> 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
[email protected]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

_______________________________________________
Wikitech-l mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l

Reply via email to