Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-10-09 08:26:21

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Overwrite the thumbnail size with a URL parameter

Hi I just posted this question in the abl_droploader plugin topic , but not sure if it has anything to do with it or not so I will post it here too.

I will like to know if I can change the thumbnail size using URL parameters please?
Something like when I’m visiting the images tab from a specific link that will add a parameter to the URL for example: &thumbsize=200x100
then somehow it will overwrite the pref with the values from the parameter while uploading new images.

(If it does has to do with the abl_droploader plugin then please delete this post)

Last edited by THE BLUE DRAGON (2013-10-09 08:27:38)

Offline

#2 2013-10-09 11:22:13

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

You could probably register_callback('your function', 'image', 'image_edit', 1) and modify global $prefs['thumb_h'] and $prefs['thumb_w'] in your_function accordingly to the value of gps('thumbsize'). But it’s only a guess.

Offline

#3 2013-10-09 12:19:37

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

Thanks Oleg,
I’m slow when it comes to PHP, is this is the right syntax to go with please?

<?php
	if(strlen(gps('thumbsize')) > 0 && strpos(gps('thumbsize'), 'x') !== false){

		register_callback('customThumbnailSize', 'image', 'image_edit', 1);

		function customThumbnailSize(){
			global $prefs['thumb_w'], $prefs['thumb_h'];

			$ctsize = explode('x',gps('thumbsize'));

			$prefs['thumb_w'] = intval($ctsize[0]);
			$prefs['thumb_h'] = intval($ctsize[1]);
		}

	}
?>

Offline

#4 2013-10-09 13:00:48

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Overwrite the thumbnail size with a URL parameter

Almost. There is a syntax error (global keyword line), and you could enclose the whole code to the handler or a class.

/**
 * Gets the default thumbnail size from the requested URL.
 */

class Abc_CustomThumbnailSize
{
    /**
     * Constructor.
     *
     * Registers the setSize method to image.image_edit event.
     */

    public function __construct()
    {
        register_callback(array($this, 'setSize'), 'image', 'image_edit', 1);
    }

    /**
     * Sets specified thumbnail size to 'thumbsize' HTTP query parameter.
     */

    public function setSize()
    {
        global $prefs;

        $size = gps('thumbsize');

        if (strpos($size, 'x'))
        {
            $size = explode('x', $size);
            $prefs['thumb_w'] = (int) $size[0];
            $prefs['thumb_h'] = (int) $size[1];
        }
    }
}

Last edited by Gocom (2013-10-09 13:02:44)

Offline

#5 2013-10-09 13:12:13

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

Append something like

$abc = new Abc_CustomThumbnailSize;

after the class definition, make it into a admin-side plugin, and you are set. I suspect it will only work for images without already created thumbnails, though.

Last edited by etc (2013-10-09 13:18:19)

Offline

#6 2013-10-09 14:49:47

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

That looks cool, thanks Jukka and Oleg.
I added an “else” statement so if there isn’t a “thumbsize” parameter it will go back to the regular 100×100

			if (strpos($size, 'x'))
			{
				$size = explode('x', $size);
				$prefs['thumb_w'] = (int) $size[0];
				$prefs['thumb_h'] = (int) $size[1];
			}else{
				$prefs['thumb_w'] = 100;
				$prefs['thumb_h'] = 100;
			}

Now, Oleg you said to make it an admin side plugin, how am I gonna do that please?
I was thinking of using it inside a rah_external_output form and call it using AJAX, or just modify the core txp_image.php file and drop it there.
but if it easy to make it as a plugin I will love to know how please.

I’m gonna use it together with bot_image_upload and abl_droploader plugins, so when you click on a bot_image_upload button in the write tab, it will open it’s lightbox iframe with the thumbsize parameter taken from the button data-thumbsize attribute.

Offline

#7 2013-10-09 16:02:59

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

THE BLUE DRAGON wrote:

Now, Oleg you said to make it an admin side plugin, how am I gonna do that please?

You can install ied_plugin_composer, create a new admin plugin and simply copy/paste this code. The plugin will fire on image edit pages.

I was thinking of using it inside a rah_external_output form and call it using AJAX

If you do it in Javascript, why would you modify $prefs? Just parse the URL and assign the appropriate values to thumbnail height and width inputs.

I’m gonna use it together with bot_image_upload and abl_droploader plugins, so when you click on a bot_image_upload button in the write tab, it will open it’s lightbox iframe with the thumbsize parameter taken from the button data-thumbsize attribute.

Here you loose me (never used them). :)

Offline

#8 2013-10-09 16:18:04

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

1.
Thanks I created an admin plugin using ied_plugin_composer, but unfortunately it doesn’t change anything.
I added an echo to the if statement just to test and see if it outputs anything and it does, but the values are not changing.

			if (strpos($size, 'x'))
			{
				$size = explode('x', $size);
				$prefs['thumb_w'] = (int) $size[0];
				$prefs['thumb_h'] = (int) $size[1];
				echo '<h1>'.$size[0].'x'.$size[1].'</h1>';
			}

2.
I want to modify $prefs from the main images page, not from the editing, it’s for that when I’m uploading images they will get crop in the right size base on section from the bot_image_upload button I was clicking (the parameter).

Offline

#9 2013-10-09 16:29:51

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

It works for me on image edit page, but I have probably misunderstood your question. If you want to save prefs for future use, try

register_callback(array($this, 'setSize'), 'image', '', 1);

and then add

set_pref('thumb_w', (int) $size[0], 'image');
set_pref('thumb_h', (int) $size[1], 'image');

after setting the new $prefs values;

Last edited by etc (2013-10-09 16:34:10)

Offline

#10 2013-10-09 17:04:24

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

Works great now!
thank you very much, I will do all the other things with bot_image_upload and will post it as a tip for others to enjoy it too :)

Offline

#11 2013-10-09 17:14:26

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

You are welcome! You could then check if new values are different from the old ones before calling set_pref, to avoid unnecessary db queries.

Offline

#12 2013-10-09 17:56:00

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

Will adding it like this to the “if” statement will be good?

if (strpos($size, 'x') && $size != $prefs['thumb_w'].'x'.$prefs['thumb_h'])

Offline

Board footer

Powered by FluxBB