Category Archives: Web

Changing XenServer bonding mode back to default (or reducing bond switchover up-delay)

You need to get the pifs again (or ideally, find the lines you used to change the mode in your bash history) – see here for getting pifs.

Then use the following:
xe pif-param-set uuid=bd26334f-f7ff-3206-e5b2-4c017c51907c other-config:bond-mode=balance-slb other-config:bond-updelay=200

This will set your bond back to balance-slb, and reduce the up-delay time (see here for the issue – although I can’t actually confirm right now that the issue still exists in XenServer 5.6, will try and find out and post here if I do*).

*ps, just doing cat /proc/net/bonding/bond0 will tell you.

Pool memory errors in apache

I was getting this error a lot on a Drupal site:

Warning: include(): Unable to allocate memory for pool.

Turns out it’s not the apache or php settings, but APC (pecl module).

To fix, add the following to your php.ini file:

apc.shm_size = 64 (adjust value to your server capabilities)

 

Transparent tiled watermark using GD with PHP

This is a basic method for dynamically adding tiled watermarks to images on your site.  It uses the .htaccess file to give the images to the PHP script which returns the image plus watermarking.

1) Create the image you want to be the watermark as a png with the opacity you want to be used (we’re not lowering the opacity later), and make sure to add extra space around the image according to how big you want the tiles.

2) Add this script to your site as watermark.php:

<?php

$watermark = imagecreatefrompng('/path/to/your/watermark.png');
$imagesrc = '/path/to/image/' . $_GET['src'];
$type = mime_content_type($imagesrc);
switch($type){
  case 'image/jpeg':
    $image = imagecreatefromjpeg($imagesrc);
    break;
  case 'image/png':
    $image = imagecreatefrompng($imagesrc);
    break;
  case 'image/gif':
    $image = imagecreatefromgif($imagesrc);
    break;
  default:
    $image = null;
}
if($image){
  # set dimensions
  $image_width = imagesx($image);
  $image_height = imagesy($image);
  imagealphablending($watermark, TRUE);

  # create the tile and overlay
  imagesettile($image,$watermark);
  imagefilledrectangle($image,0,0,$image_width,$image_height,IMG_COLOR_TILED);

  # output image
  header('content-type: image/jpeg');
  imagejpeg($image);

  #clean up
  imagedestroy($image);
  imagedestroy($watermark);
}


?>

3) Add rewrite rules in .htaccess.  Notice the $_GET[‘src’] in the script above; we need to hand this variable to the script.  In this example, anything in my gallery folder, or with the filename starting with wmark- will be passed to the script:

RewriteRule ^(.*)wp-content/uploads/wmark-(.*) $1path/to/watermark.php?src=wp-content/uploads/wmark-$2
RewriteRule ^(.*)wp-content/uploads/image-gallery/main_gallery/(.*) $1path/to/watermark.php?src=wp-content/uploads/image-gallery/main_gallery/$2

# If you want to exclude a particular image (excludeme.jpg), add this ABOVE the other rules.  The [L] tells it to ignore other rules
RewriteRule ^wp-content/uploads/excludeme\.jpg$ - [L]

The script will only handle jpg/gif/png file types; if it’s possible that your rewrite rules will pass any other file types to the script, make sure to use (.*\.(gif|jpg|jpeg|png)) if it’s possible instead of just the basic (.*) in your rewrite rules.

WordPress Google Calendar Events plugin notes

I’ve just installed this awesome plugin onto a WP blog: http://www.rhanney.co.uk/plugins/google-calendar-events/

Had two issues I want to note:

1) Use the XML feed from Google, not the iCAL feed, or it will give errors

2) The timezone wasn’t working properly using default settings (it uses WP’s time); I realised this problem actually lies with WP – by default WP was using UTC+0 for my timezone (London), which is wrong, as now we’re on DST, which is UTC+1, changed it to London rather than the UTC offset and it’s working fine.
(Suprised I haven’t noticed this before, although I can’t actually think of any WP sites I’ve made where the post time is displayed – except this one at the moment, as I haven’t modified it at all yet – so it’s never been a problem.)

Drupal 7 custom template files per content type

Very useful – got this from here where you can also find the one for D6.

As the comment suggests, if you have a content type called ‘blog’, you can create a template file to apply to nodes with this content type only called page–blog.tpl.php.

<div class="codeblock"><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php
</span><span style="color: #007700;">function </span><span style="color: #0000bb;">themeName_preprocess_page</span><span style="color: #007700;">(&amp;</span><span style="color: #0000bb;">$vars</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$hook</span><span style="color: #007700;">) {
if (isset(</span><span style="color: #0000bb;">$vars</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'node'</span><span style="color: #007700;">])) {
</span><span style="color: #ff8000;">// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
</span><span style="color: #0000bb;">$vars</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'theme_hook_suggestions'</span><span style="color: #007700;">][] = </span><span style="color: #dd0000;">'page__'</span><span style="color: #007700;">. </span><span style="color: #0000bb;">str_replace</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'_'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'--'</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$vars</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'node'</span><span style="color: #007700;">]-&gt;</span><span style="color: #0000bb;">type</span><span style="color: #007700;">);
}
}
</span><span style="color: #0000bb;">?&gt;