Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
8 posts Page 1 of 1
I have a site, where users can download images by clicking on a thumbnail. Unfortunately I underestimated the amount of images (and the file sizes!) Basically, I didn't create a thumbnail option for the images to display to the user, so they could click on it to download the large version image. I was just scaling down the large image for the thumbnail. Unfortunately, someone else was in charge of uploading the images, and there are now over 300, with file sizes up to 4Mb :o
I've tried the <cms:thumbnail> tag, but the amount and size of images is causing the site to slow down.

So my question is, is there a way to access the automatically generated thumbnail that the kc_finder uses in the backend? If I could access this to display on the front end, it would save me a lot of work! Obviously I can insert a thumbnail field now, but I would have to go back through the 300 images and generate the thumbnail. (Or someone else would have to do it, and she isn't happy!)
Is this possible? Thanks in advance!
Hi,

I think <cms:thumbnail> is the right solution to this problem.

Don't worry about the perceived slowdown - it is a one-time thing for each image without a thumbnail. Once <cms:thumbnail> has created the missing thumbnail, the process won't be repeated for that image and the static thumbnail would be returned.
Thanks KK. It did still slow down a lot when I used the <cms:thumbnail> (even after the thumbs had been generated), but it may have been something else causing it. I just hoped the admin thumbnail might be a quick fix (or would prove to me that the problem was elsewhere!).
Thanks again,
Ewan
Ewan,

I think I have a better solution for you -
Obviously I can insert a thumbnail field now, but I would have to go back through the 300 images and generate the thumbnail. (Or someone else would have to do it, and she isn't happy!)

I totally agree. So let's create an automated script that will go through the 300 pages and create the missing thumbnails for us :)

For that, we'll adapt the technique we used in the 'CSV importer' (viewtopic.php?f=5&t=8803).

Define a new thumbnail region in the template.
If you edit any existing page in admin-panel, this thumbnail will show up empty.
Simply hitting 'save' on the page, sadly, will not create this thumbnail as it is associated with your existing main image. Couch will recreate all thumbnails associated with an image only when it senses that the main image itself has changed.

So, as a test, if we set the main image as blank, save, set the image back to the original image and save a second time you'll see that the thumbnail will get created.

Of course we are not going to do this manually for 300 pages :)
We'll use the script we are going to create next.

Create a new template (say named 'import.php') place the following code within it and register it by accessing it as super-admin.
Code: Select all
<?php require_once ('couch/cms.php'); ?>
<cms:template title='Import' ></cms:template>

<cms:set mystart="<cms:gpc 'import' method='get' />" />

<cms:if mystart >
    <cms:pages masterpage='gallery.php' paginate='1' limit='2'>

        <cms:if k_paginated_top >
            <cms:if k_paginate_link_next >
                <script language="JavaScript" type="text/javascript">
                    var myVar;
                    myVar = window.setTimeout( 'location.href="<cms:show k_paginate_link_next />";', 1000 );
                </script>
                <button onclick="clearTimeout(myVar);">Stop</button>
            <cms:else />
                <h1>Done!</h1>
            </cms:if>
           
            <h3><cms:show k_current_page /> / <cms:show k_total_pages /> pages (Total <cms:show k_total_records /> records. Showing <cms:show k_paginate_limit /> records per page)</hr>
         </cms:if>   
       
        <h3><cms:show k_page_title /></h3>

       
        <!-- MAIN PROCESSING START -->

        <!-- 1. save existing main-image name in a variable -->
        <cms:set my_orig_image = gg_image />
       
       
        <!-- 2. set main-image to blank -->
        <cms:db_persist
            _masterpage=k_template_name
            _mode='edit'
            _page_id=k_page_id
           
            gg_image=''
        >

            <cms:if k_error>
                <strong style="color:red;">ERROR:</strong> <cms:show k_error/>
            </cms:if>
        </cms:db_persist>
       
       
        <!-- 3. set main-image back to original data.. this will recreate the new thumbnail associated with it -->
        <cms:db_persist
            _masterpage=k_template_name
            _mode='edit'
            _page_id=k_page_id
           
            gg_image=my_orig_image
        >

            <cms:if k_error>
                <strong style="color:red;">ERROR:</strong> <cms:show k_error/>
            </cms:if>
        </cms:db_persist>

       <!-- MAIN PROCESSING END -->
       
       
        <cms:if k_paginated_bottom > 
            <cms:paginator />
        </cms:if>
    </cms:pages>
<cms:else/>
    <button onclick='location.href="<cms:add_querystring k_page_link 'import=1' />"'>Start!</button>
</cms:if>

<?php COUCH::invoke(); ?>

You'll have to make slight changes to the code above to make it work with your template that needs the thumbnail. Change the 'masterpage' parameter of <cms:pages> to point to your template. Also the code above is assuming that the main-image's name is 'gg_image' (a norm with gallery templates). Replace the name with the name of your type 'image' main image that needs the associated thumbnail (this name has been used at *three* locations - make sure you change all).

Explaining what the code does - the shim code simply loads 2 pages at a time, does the processing and moves on to the next 2 pages (as explained in CSV importer viewtopic.php?f=5&t=8803).

The main processing is what interests us.
It simply saves each existing page twice (using <cms:db_persist>) - once by setting the main-image to blank and then again by setting the image back to its original value which creates the thumbnail.

N.B. It might be a good idea to take a database backup of your site before embarking on any operation that involves large number of pages.

Once you have used this importer, remove it from your system by deleting the template.

Incidentally this technique can come in useful for almost any kind of batch operations involving huge number of cloned pages in Couch.

Hope it helps. Do let me know.
KK, that worked perfectly!
Thanks so much, saved me (or someone else) a lot of time. You and your wonderful Couch strike again! :D
I'm glad it helped, Ewan :)
This just worked for me. Fantastic script to recreate thumbnails - thank you!
@theacidfrog, I am glad it helped :)
8 posts Page 1 of 1