Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 2013-10-12 21:39:27

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

Re: Overwrite the thumbnail size with a URL parameter

Thank you Jukka
and thanks Uli for the prefix.

Here’s the new code, have I done all the changes right?
(it is working)

One thing that I still don’t understand is:
I don’t know how to set a global variable of the database table name for use in SQL queries,
currently I’m setting it over and over again in each function which is stupid.
$gts_table = safe_pfx('gil_thumb_size');
what is the right syntax for setting a global variable outside the functions and use it inside them please?

BTW: I will also fix some issues that you have mentioned, in the ebl_image_edit plugin now that we are aware of them.

class gil_thumb_size{
	// Constructor
	public function __construct(){
		if(@txpinterface == 'admin'){
			add_privs('gil_thumb_size', '1');
			register_tab('extensions', 'gil_thumb_size', 'Thumbnail Size');
			register_callback(array($this, 'gil_thumb_size_gui'), 'gil_thumb_size');
			register_callback(array($this, 'gil_thumb_size_js'), 'article');
			register_callback(array($this, 'gil_thumb_size_php'), 'image');
		}
	}

	// Creates the gui
	public function gil_thumb_size_gui(){
		$step = gps('step');
		$title = 'gil_thumb_size Preferences';

		$steps = array(
			'gil_thumb_save'   => true,
			'gil_thumb_delete' => true
		);

		if($step && bouncer($step, $steps)){
			$message = $this->$step();
			$this->$step();
			$ptop = pagetop($title, $message);
		}else{
			$ptop = pagetop($title);
		}

		echo $ptop.n.
			$this->gil_thumb_sizes();
	}

	// Set database table and gui list
	public function gil_thumb_sizes(){
		$gts_table = safe_pfx('gil_thumb_size');
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` varchar(64) NOT NULL,".
				"`width` int(4) NOT NULL,".
				"`height` int(4) NOT NULL,".
				"`section` int(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

		// Create H1 and table head
		echo n.hed(gTxt('Thumbnail Sizes'), 1).
			n.n.startTable('list','','txp-list').
			n.'<thead>'.tr(
				n.hCell('Field ID or Section name').
				n.hCell(gTxt('width')).
				n.hCell().
				n.hCell(gTxt('height')).
				n.hCell(gTxt('section')).
				n.hCell(gTxt('delete'))
			).'</thead>';

		// Building the saved sizes as table rows
		$rs = safe_rows_start('*', $gts_table, '1=1 ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){ 
				$isChecked = ($a['section'] == 1) ? 'yes' : 'no';
				$out .= n.tr(
					td( htmlspecialchars($a['name'])).
					td( htmlspecialchars($a['width'])).
					td( htmlspecialchars(' X ')).
					td( htmlspecialchars($a['height'])).
					td( $isChecked).
					td( dLink('gil_thumb_size', 'gil_thumb_delete', 'name', $a['name']))
				);
			}
		}
		echo form($out);

		// Create a form row for creating new sizes
		$gtb_save = $this->gil_thumb_save();
		echo n.tr(
			form(
				td( fInput('text', 'name', '', 'edit','','',20) ).
				td( fInput('text', 'width', '', 'edit','','',5) ).
				td( htmlspecialchars(' X ')).
				td( fInput('text', 'height', '', 'edit','','',5) ).
				td( '<input name="gts_section" class="checkbox" type="checkbox">').
				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
				n.eInput('gil_thumb_size').
				n.sInput('gil_thumb_save')
			)
		);
		echo n.endTable();
	}

	// Gets the saves sizes
	public function gil_thumb_select($section = '0'){
		$gts_table = safe_pfx('gil_thumb_size');
		$rs = safe_rows_start('*', $gts_table, 'section='.$section.' ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){
				$width	= escape_js($a['width']);
				$height	= escape_js($a['height']);
				$name 	= escape_js($a['name']);
				$out .= "case '".$name."' : thumbnail = '".$width."x".$height."'; break;".n;
			}
		}

		return $out;
	}

	// Save new sizes
	public function gil_thumb_save(){
		$gts_table = safe_pfx('gil_thumb_size');
		extract(doSlash(psa(array('name','width','height','gts_section'))));

		if($name && $width && $height){ 
			$gts_section = ($gts_section == "on") ? 1 : 0;
			$rs = @safe_insert($gts_table, "
				`name`	 = '$name',
				`width`	 = '$width',
				`height` = '$height',
				`section`  = '$gts_section'");

			return ($rs) ?
				'New thumb size created' : 
				'<b>Error:</b> Duplicate Name';
		}else{
			return '<b>Width</b> and <b>Height</b> must be numeric values';
		}

		return FALSE;
	}

	// Delete sizes
	public function gil_thumb_delete(){
		$gts_table = safe_pfx('gil_thumb_size');
		$name = ps('name');
		return (safe_delete($gts_table, "`name` = '".doSlash($name)."'")) ? 
			"<b>Deleted</b> $name" : 
			"<b>Error</b> Unable to delete $name"; 
	}

	// Write tab javascript
	public function gil_thumb_size_js(){
		$gts_fields = $this->gil_thumb_select(0);
		$gts_sections = $this->gil_thumb_select(1);

		echo script_js("
				$(document).ready(function(){

					// Set the 'thumbsize' variable with default thumbnail size value
					var thumbsize = '100x100';

					// Set thumbnail size base on field ID
					$('.bot_add_image').each(function(){
						switch($(this).parents('p').find('input').attr('id')){
	$gts_fields
						}
						$(this).parents('p').find('.bot_add_image').attr('data-thumbsize',thumbsize);
					});

					// Set thumbnail size base on section name
					// This overwrites the previous settings which were base on field ID
					$('#section').change(function(){
						switch($(this).val()){
	$gts_sections
						}
						$('.bot_add_image').attr('data-thumbsize',thumbsize);
					}).change();

					// Assign the thumbsize parameter with the value to bot_image_upload iframe
					var check_for_bot_iu_iframe;
					$('.bot_add_image').click(function(){
						check_for_bot_iu_iframe = setInterval(function(){
							if($('#bot_iu_iframe').length > 0){
								$('#bot_iu_iframe').attr('src',$('#bot_iu_iframe').attr('src')+'&thumbsize='+thumbsize);
								clearInterval(check_for_bot_iu_iframe);
							}
						},100);
					});

				});
		");
	}

	// Images tab PHP
	public function gil_thumb_size_php(){
		global $prefs;
		$defaultSize = '100x100';
		$currentSize = $prefs['thumb_w'].'x'.$prefs['thumb_h'];
		$newSize = gps('thumbsize');

		if(strpos($newSize,'x') && $newSize != $currentSize){
			$newSize = explode('x',$newSize);
			set_pref('thumb_w', (int) $newSize[0], 'image');
			set_pref('thumb_h', (int) $newSize[1], 'image');
		}else if(strpos($defaultSize,'x') && $currentSize != $defaultSize){
			$defaultSize = explode('x',$defaultSize);
			set_pref('thumb_w', (int) $defaultSize[0], 'image');
			set_pref('thumb_h', (int) $defaultSize[1], 'image');
		}
	}

}

$gtb = new gil_thumb_size;

Offline

#26 2013-10-13 02:25:46

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Overwrite the thumbnail size with a URL parameter

@Gil – would still be a great write up for TXP Tips if you have time – maybe on how to compose a plugin?

Offline

#27 2013-10-13 07:04:12

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

Re: Overwrite the thumbnail size with a URL parameter

THE BLUE DRAGON wrote:

I don’t know how to set a global variable of the database table name for use in SQL queries

You don’t use global variables. When using a class, you may use a property, but usage of global variables should always be avoided as it pollutes the global namespace. Global variables and the keyword are just PHP’s legacy features that predates actual namespaces. But. You don’t have to do what you are doing.

$rs = safe_rows_start('*', $gts_table, 'section='.$section.' ORDER BY `name`');

In queries itself, but not when there is dedicated table argument; these functions that have a table argument do prefixing for you (otherwise there wouldn’t be this limited argument syntax).

$message = $this->$step(); $this->$step();

This runs the method twice.

"`section` int(1) NOT NULL default '0',".

That’s 4 bytes for single bit. TINYINT field will fit one bit just fine. Lowers storage requirement by marginal 3 bytes per row.

$width = escape_js($a['width']); $height = escape_js($a['height']);

These are always integers. If you really wanted to escape these, you would be casting them, but you don’t have to do anything.

$gtb = new gil_thumb_size;

That variable is unnecessary (and not prefixed).

public function gil_thumb_size_gui(){

You don’t have to prefix all methods if you don’t want to. They are not in global namespace.

// Constructor

There are differences between the few comment syntaxes, and we use different ones for reasons. The ones I had there are meant for documentation, and are interpreted by tokenizer differently from the rest, and used by tools such as PHPDoc, and IDEs to provide documentation.

Last edited by Gocom (2013-10-13 07:06:50)

Offline

#28 2013-10-13 10:49:24

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

Re: Overwrite the thumbnail size with a URL parameter

Thanks

- looks much better.

- fixed a bug in my JS, and added a conditional statement for checking if there are any “bot_image_upload” fields.

- combined the two UI functions into a single one, so now there are 6 main functions/methods which are:
  • ui (Creates the ui and steps, set database table and list size items)
  • get (Gets the saved sizes)
  • save (Save new sizes)
  • delete (Delete sizes)
  • js (Write tab javascript)
  • set (Images tab PHP, gets the “thumbsize” parameter from the URL and set it values in the pref)

The new code with the changes:

class gil_thumb_size {
	/**
	 * Constructor
	 */
	public function __construct(){
		if(@txpinterface == 'admin'){
			add_privs('gil_thumb_size', '1');
			register_tab('extensions', 'gil_thumb_size', 'Thumbnail Size');
			register_callback(array($this, 'ui'), 'gil_thumb_size');
			register_callback(array($this, 'js'), 'article');
			register_callback(array($this, 'set'), 'image');
		}
	}

	/**
	 * Creates the ui and steps
	 *
	 * Set database table and list size items
	 */
	public function ui(){
		$step = gps('step');
		$steps = array('save' => true, 'delete' => true);
		$title = 'gil_thumb_size Preferences';
		$gts_table = safe_pfx('gil_thumb_size');

		// Steps and message
		if($step && bouncer($step, $steps)){
			$message = $this->$step();
			$ptop = pagetop($title, $message);
		}else{
			$ptop = pagetop($title);
		}
		echo $ptop;

		// Create DB table
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` VARCHAR(64) NOT NULL,".
				"`width` INT(4) NOT NULL,".
				"`height` INT(4) NOT NULL,".
				"`section` TINYINT(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

		// Create H1 and table head
		echo n.hed(gTxt('Thumbnail Sizes'), 1).
			n.n.startTable('list','','txp-list').
			n.'<thead>'.tr(
				n.hCell('Field ID or Section name').
				n.hCell(gTxt('width')).
				n.hCell().
				n.hCell(gTxt('height')).
				n.hCell(gTxt('section')).
				n.hCell(gTxt('delete'))
			).'</thead>';

		// Building the saved sizes as table rows
		$rs = safe_rows_start('*', 'gil_thumb_size', '1=1 ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){ 
				$isChecked = ($a['section'] == 1) ? 'yes' : 'no';
				$out .= n.tr(
					td( htmlspecialchars($a['name'])).
					td( htmlspecialchars($a['width'])).
					td( htmlspecialchars(' X ')).
					td( htmlspecialchars($a['height'])).
					td( $isChecked).
					td( dLink('gil_thumb_size', 'delete', 'name', $a['name']))
				);
			}
		}
		echo form($out);

		// Create a form row for creating new sizes
		$gtb_save = $this->save();
		echo n.tr(
			form(
				td( fInput('text', 'name', '', 'edit','','',20) ).
				td( fInput('text', 'width', '', 'edit','','',5) ).
				td( htmlspecialchars(' X ')).
				td( fInput('text', 'height', '', 'edit','','',5) ).
				td( '<input name="gts_section" class="checkbox" type="checkbox">').
				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
				n.eInput('gil_thumb_size').
				n.sInput('save')
			)
		);
		echo n.endTable();
	}

	/**
	 * Gets the saved sizes
	 */
	public function get($section = '0'){
		$rs = safe_rows_start('*', 'gil_thumb_size', 'section='.$section.' ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){
				$width	= $a['width'];
				$height	= $a['height'];
				$name 	= escape_js($a['name']);
				$out .= "case '".$name."' : thumbsize = '".$width."x".$height."'; break;".n;
			}
		}

		return $out;
	}

	/**
	 * Save new sizes
	 */
	public function save(){
		extract(doSlash(psa(array('name','width','height','gts_section'))));

		if($name && $width && $height){ 
			$gts_section = ($gts_section == "on") ? 1 : 0;
			$rs = @safe_insert('gil_thumb_size', "
				`name`	 = '$name',
				`width`	 = '$width',
				`height` = '$height',
				`section`  = '$gts_section'");

			return ($rs) ?
				'New thumb size created' : 
				'<b>Error:</b> Duplicate Name';
		}else{
			return '<b>Width</b> and <b>Height</b> must be numeric values';
		}

		return FALSE;
	}

	/**
	 * Delete sizes
	 */
	public function delete(){
		$name = ps('name');
		return (safe_delete('gil_thumb_size', "`name` = '".doSlash($name)."'")) ? 
			"<b>Deleted</b> $name" : 
			"<b>Error</b> Unable to delete $name"; 
	}

	/**
	 * Write tab javascript
	 */
	public function js(){
		$gts_by_fields = $this->get(0);
		$gts_by_sections = $this->get(1);

		echo script_js("
				$(document).ready(function(){

					// Set the 'thumbsize' variable
					var thumbsize = '';

					// Check if 'bot_image_upload' plugin fields exists
					if($('.bot_add_image').length > 0){

						// Set thumbnail size base on field ID
						$('.bot_add_image').each(function(){
							switch($(this).parents('p').find('input').attr('id')){
		$gts_by_fields
							}
							$(this).parents('p').find('.bot_add_image').attr('data-thumbsize',thumbsize);
						});

						// Set thumbnail size base on section name
						// This overwrites the previous settings which were base on field ID
						$('#section').change(function(){
							switch($(this).val()){
		$gts_by_sections
							}
							$('.bot_add_image').attr('data-thumbsize',thumbsize);
						}).change();

						// Assign the thumbsize parameter with the value to bot_image_upload iframe
						var check_for_bot_iu_iframe;
						$('.bot_add_image').click(function(){
							check_for_bot_iu_iframe = setInterval(function(){
								if($('#bot_iu_iframe').length > 0){
									$('#bot_iu_iframe').attr('src',$('#bot_iu_iframe').attr('src')+'&thumbsize='+thumbsize);
									clearInterval(check_for_bot_iu_iframe);
								}
							},100);
						});

					}

				});
		");
	}

	/**
	 * Images tab PHP
	 *
	 * Gets the "thumbsize" parameter from the URL and set it values in the pref.
	 */
	public function set(){
		global $prefs;
		$defaultSize = '100x100';
		$currentSize = $prefs['thumb_w'].'x'.$prefs['thumb_h'];
		$newSize = gps('thumbsize');

		if(strpos($newSize,'x') && $newSize != $currentSize){
			$newSize = explode('x',$newSize);
			set_pref('thumb_w', (int) $newSize[0], 'image');
			set_pref('thumb_h', (int) $newSize[1], 'image');
		}else if(strpos($defaultSize,'x') && $currentSize != $defaultSize){
			$defaultSize = explode('x',$defaultSize);
			set_pref('thumb_w', (int) $defaultSize[0], 'image');
			set_pref('thumb_h', (int) $defaultSize[1], 'image');
		}
	}

}

new gil_thumb_size;

Edit oops posted an old code, now it’s the new.

Last edited by THE BLUE DRAGON (2013-10-13 10:57:01)

Offline

#29 2013-10-13 12:42:03

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: Overwrite the thumbnail size with a URL parameter

THE BLUE DRAGON wrote:

BTW: I will also fix some issues that you have mentioned, in the ebl_image_edit plugin now that we are aware of them.

Gil, I’ve altered and improved that plugin enormously during the last few months:
  • resizing & cropping in one go, hence all resizing/cropping/thumbnailing can happen in only one panel. Side effect: The cropping frame for creating the thumbnail can simply be reused for creating the final image.
  • all of the above steps can be done even if the image is squished by a too small window
  • individual rotation with the aid of an angle measurement tool
  • individual sharpening
  • individual lightening/darkening
  • undo last step
  • scrolls down to thumbnail on thumbnail creation
  • plays nice with adi_image_tab: updates its dimension display
  • massive help support via title tags

and would like to keep all changes (like yours and Marco’s png watermark) in one plugin file and of course publish the result. But I’ve not the level of coding skills that you’re discussing on here and needed at least a diff in order to follow what you’ve done.

Hence: Could you please keep one version of ebl_ie from immediately before these changes and after. I can, of course, offer you my version of the plugin to apply the alterations immediately there, but that’s only a proposal you don’t need to accept.

Last edited by uli (2013-10-13 17:46:39)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#30 2013-10-13 13:52:43

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

Re: Overwrite the thumbnail size with a URL parameter

uli wrote:

Gil, I’ve altered and improved that plugin enormously over the last few months:

wow that’s sounds great!
will contact you about it probably next weekend or maybe sometime before :)

Offline

#31 2013-10-14 12:14:52

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

Re: Overwrite the thumbnail size with a URL parameter

New code:
  1. Changed the PHP to only modify the pref if there is a thumbsize parameter, not like before where I added an else statement to go back to the default size.
  2. Changed the JS:
    • Now working with “abl_droploader” plugin too (base on section, as it’s a single button).
    • Changed the way to set the size in the pref, now it’s using AJAX to do that, and not anymore modify the “bot_image_upload” iframe source.
      $.post('?event=image','thumbsize='+thumbsize);
    • It’s now easy to add more plugins to work with it as I set two variables name “gts_by_sections_button_elm” and “gts_by_fields_button_elm” which is where you can add more button elements of other plugins.
      currently are:
      • By fields ID: $gts_by_fields_button_elm = '.bot_add_image';
      • By sections: $gts_by_sections_button_elm = '.bot_add_image, #abl-droploader-open';

Do you find any other issues that I should fix please?
really thank you for all the time you spends on supporting on this.

Have a great week :)

class gil_thumb_size {
	/**
	 * Constructor
	 */
	public function __construct(){
		if(@txpinterface == 'admin'){
			add_privs('gil_thumb_size', '1');
			register_tab('extensions', 'gil_thumb_size', 'Thumbnail Size');
			register_callback(array($this, 'ui'), 'gil_thumb_size');
			register_callback(array($this, 'js'), 'article');
			register_callback(array($this, 'set'), 'image');
		}
	}

	/**
	 * Creates the ui and steps
	 *
	 * Set database table and list size items
	 */
	public function ui(){
		$step = gps('step');
		$steps = array('save' => true, 'delete' => true);
		$title = 'gil_thumb_size Preferences';
		$gts_table = safe_pfx('gil_thumb_size');

		// Steps and message
		if($step && bouncer($step, $steps)){
			$message = $this->$step();
			$ptop = pagetop($title, $message);
		}else{
			$ptop = pagetop($title);
		}
		echo $ptop;

		// Create DB table
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` VARCHAR(64) NOT NULL,".
				"`width` INT(4) NOT NULL,".
				"`height` INT(4) NOT NULL,".
				"`section` TINYINT(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

		// Create H1 and table head
		echo n.hed(gTxt('Thumbnail Sizes'), 1).
			n.n.startTable('list','','txp-list').
			n.'<thead>'.tr(
				n.hCell('Field ID or Section name').
				n.hCell(gTxt('width')).
				n.hCell().
				n.hCell(gTxt('height')).
				n.hCell(gTxt('section')).
				n.hCell(gTxt('delete'))
			).'</thead>';

		// Building the saved sizes as table rows
		$rs = safe_rows_start('*', 'gil_thumb_size', '1=1 ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){ 
				$isChecked = ($a['section'] == 1) ? 'yes' : 'no';
				$out .= n.tr(
					td( htmlspecialchars($a['name'])).
					td( htmlspecialchars($a['width'])).
					td( htmlspecialchars(' X ')).
					td( htmlspecialchars($a['height'])).
					td( $isChecked).
					td( dLink('gil_thumb_size', 'delete', 'name', $a['name']))
				);
			}
		}
		echo form($out);

		// Create a form row for creating new sizes
		$gtb_save = $this->save();
		echo n.tr(
			form(
				td( fInput('text', 'name', '', 'edit','','',20) ).
				td( fInput('text', 'width', '', 'edit','','',5) ).
				td( htmlspecialchars(' X ')).
				td( fInput('text', 'height', '', 'edit','','',5) ).
				td( '<input name="gts_section" class="checkbox" type="checkbox">').
				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
				n.eInput('gil_thumb_size').
				n.sInput('save')
			)
		);
		echo n.endTable();
	}

	/**
	 * Gets the saved sizes
	 */
	public function get($section = '0'){
		$rs = safe_rows_start('*', 'gil_thumb_size', 'section='.$section.' ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){
				$width	= $a['width'];
				$height	= $a['height'];
				$name 	= escape_js($a['name']);
				$out .= "case '".$name."' : thumbsize = '".$width."x".$height."'; break;".n;
			}
		}

		return $out;
	}

	/**
	 * Save new sizes
	 */
	public function save(){
		extract(doSlash(psa(array('name','width','height','gts_section'))));

		if($name && $width && $height){ 
			$gts_section = ($gts_section == "on") ? 1 : 0;
			$rs = @safe_insert('gil_thumb_size', "
				`name`	 = '$name',
				`width`	 = '$width',
				`height` = '$height',
				`section`  = '$gts_section'");

			return ($rs) ?
				'New thumb size created' : 
				'<b>Error:</b> Duplicate Name';
		}else{
			return '<b>Width</b> and <b>Height</b> must be numeric values';
		}

		return FALSE;
	}

	/**
	 * Delete sizes
	 */
	public function delete(){
		$name = ps('name');
		return (safe_delete('gil_thumb_size', "`name` = '".doSlash($name)."'")) ? 
			"<b>Deleted</b> $name" : 
			"<b>Error</b> Unable to delete $name"; 
	}

	/**
	 * Write tab javascript
	 */
	public function js(){
		$gts_by_fields = $this->get(0);
		$gts_by_sections = $this->get(1);
		$gts_by_fields_button_elm = '.bot_add_image';
		$gts_by_sections_button_elm = '.bot_add_image, #abl-droploader-open';

		echo script_js("
				$(document).ready(function(){

					// Set the 'thumbsize' variable
					var thumbsize = '';

					// Set thumbnail size base on plugins button field ID
					$('$gts_by_fields_button_elm').each(function(){
						switch($(this).parents('p').find('input').attr('id')){
$gts_by_fields
						}
						$(this).parents('p').find('$gts_by_fields_button_elm').attr('data-thumbsize',thumbsize);
					});

					// Set thumbnail size base on section name
					// This overwrites the previous settings which were base on field ID
					$('#section').change(function(){
						switch($(this).val()){
$gts_by_sections
						}
						$('$gts_by_sections_button_elm').attr('data-thumbsize',thumbsize);
					}).change();

					// Set the thumbnail size in the pref on click using AJAX
					$('$gts_by_sections_button_elm').click(function(){
						thumbsize = $(this).attr('data-thumbsize');
						$.post('?event=image','thumbsize='+thumbsize);
					});

				});
		");
	}

	/**
	 * Images tab PHP
	 *
	 * Gets the "thumbsize" parameter from the URL and set it values in the pref.
	 */
	public function set(){
		global $prefs;
		$currentSize = $prefs['thumb_w'].'x'.$prefs['thumb_h'];
		$newSize = gps('thumbsize');

		if(strpos($newSize,'x') && $newSize != $currentSize){
			$newSize = explode('x',$newSize);
			set_pref('thumb_w', (int) $newSize[0], 'image');
			set_pref('thumb_h', (int) $newSize[1], 'image');
		}
	}

}

new gil_thumb_size;

Offline

#32 2013-10-14 21:42:07

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

Re: Overwrite the thumbnail size with a URL parameter

Just tested the plugin on a fresh TXP install and the production status was on “testing”, I received an error, so I changed it to debugging and this is what it says:

Internal error “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘gil_thumb_size’ at line 1”
in C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_db.php at line 95.

adminErrorHandler()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_db.php:95 trigger_error()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_misc.php(812) : eval()’d code:36 safe_query()
gil_thumb_size->ui()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_misc.php:854 call_user_func_array()

I’m sorry. I’m afraid I can’t do that; gil_thumb_size is an unsafe operation.

Any help with this will be much appreciated please.
It has to do with this part, what’s wrong with it?
how I can simply check if the table exist and if not to create it, in a safe way that will not throw an error like above?

		// Create DB table
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` VARCHAR(64) NOT NULL,".
				"`width` INT(4) NOT NULL,".
				"`height` INT(4) NOT NULL,".
				"`section` TINYINT(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

Offline

Board footer

Powered by FluxBB