Simply Breath Teching…

Resources for PHP Programmers and Small Business Operators

WP Ecommerce Scale Image instead of Cropping

Ok i recently had the problem where i wanted to define a max size for the images in my shopping cart. I didn’t want to set a static size for my entire carts images.

Eg I wanted it to be max of 300px height or width but not 300px wide and 300px high.

So if the image is landscape it would be 300px wide and whatever it works it out to be height (eg 200px, anything lower than 300px). If it was portrait then i want the largest dimension to be the height (at 300px with the above example).

Here are the things i did to achieve this.

Please note – you will need knowledge of PHP to achieve this.

First you need to add this section of code to the file misc.functions.php – located in WP Ecommerce folder/wpsc-includes/
Add this code to the switch($scaling_method) { statement (make sure it’s above the last option
case 'scale':
if (empty($width) || empty($height))
{
if (!empty($width))
$maxSize = $width;
else if (!empty($height))
$maxSize = $height;
else
{
//error
}
}else
{
if ($width > $height)
$maxSize = $width;
else
$maxSize = $height;
}

list($temp_w, $temp_h) = calculate_image_width_and_height($source_w, $source_h, $maxSize, $allowEnlarge=true);
$width = $temp_w;
$height = $temp_h;

break;

Replace

// select our scaling method
$scaling_method = 'cropping';

with

// select our scaling method
$scaling_method = 'scale';

You’ll also have to change theme.functions.php in the same directory as misc.functions.php
You’ll need to remove the css that the file creates.
Namely the Single View Styling
And the last 3 Default View Styling css statements.

The final change is part of your theme – i’m using the default theme, i’m not sure if you’ll have to change it for other themes or not.

Default theme – change single_product.php put a table around the following divs (below the div class=’textcol’ div)
<table width='100%' class='nocolour'>
<tr>
<td valign='top'>
<div>
Image Stuff is here
</div>
</td>
<td valign='top'>
<div>
...
</div>
</td>
</tr>
</table>

I think some css changes need to be done to get this to work (namely removing the postition:relative and some of the floats and positioning for imagecol and producttext)

Once that is all done – you need to go the Presentation area of the Settings Area of WP E-commerce. Go to the Single Product Image Size part and fill in only one of the text boxes, leave the other blank.
That should make it work for your images. I’ll be doing this for another client soon so if the process is different i’ll let you know

Bookmark and Share

Creating a catch all sub domain system

Ok recently i needed a catch all system for a website i was creating. All the tutorials i went through needed me to change the httpd.conf file. Pretty hard to do when on shared hosting. But found the solution to it using CPanel functions.

Instead of adding *.domain.com to the httpd.conf file

ServerAlias www.domain.com domain.com *.domain.com

Answer below

You can create a subdomain using the cpanel function with * as the subdomain title. You can set it’s fileroot as public_html to redirect to the root folder, or set up as something else.

if you want an example of htaccess that would send the subdomain to the script -

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) $1?subDomain=%2 [L]

</IfModule>

Bookmark and Share

could not complete the request because the layer is not a valid text layer – Photoshop error

Hi All

I recently had this error message come up whilst trying to open a file that i had created. I was opening it on the same computer. Restarting Photoshop was able to solve the problem. Hopefully this helps. I’ve read in other forums that you can normally open it in another photoshop program on a different computer and resave it. But didn’t need to do that for mine

Bookmark and Share

SWFUpload gets stuck at uploading…

Recently I came across a problem while installing SWFUpload on my latest site. I had the problem before but couldn’t remember what the solution was.

When I uploaded a file with SWFUpload on my mac (OSX 10.5) it would get stuck at the uploading… status

This was cause by my upload_url file not returning any text at all. Make sure when you set up the system that for every error case that some sort of output is displayed. And also on success.

In PHP I simply output the error message then die;

Bookmark and Share

Can’t get image width or height using javascript in IE 7 and 8

Came across this one whilst working with an TinyMCE plugin.

I had some additional css on the image i was trying to get the height and width of – display:none;

That’s the problem. Just use visibility:hidden; in your CSS and all will be fine in the world.

Oh just in case you are using the incorrect javascript as well.

alert(document.getElementById(‘imageId’).width);

Enjoy!

Bookmark and Share

Forms won’t submit in IE when pressing enter

Hi All

I’ve come across this issue yet again. Realised I’d forgotten to post it the first time.

Instance 1 – Form has 1 text field, when pressing enter the form doesn’t submit correctly, either won’t submit, or won’t send submit button value through to php.

Solution -

if ($noOfTextFields == 1)
{
//fix IE bug where if there is only 1 text field you can't press enter to submit form (it doesn't send submit button name value pair)
$output .= "<input name='" . time() . "' style='display: none;' type='text' / >";
}

That should fix that instance

Instance 2 – Only seems to be happening in IE8 – When a form is either being hidden (using CSS display or visibility tags) or being creating dynamically when you press enter nothing will happen

Solution – add the following code to your form tag – Special thanks to Chris from chrisbegg.com for providing me with an example he used to solve the ajax problem

onkeypress=" var keycode; if (window.event) keycode = window.event.keyCode; else if (e) keycode = e.which; else return true; if (keycode == 13) {this.submit()}else {return true;}"

Make sure that it’s all on one line you know how javascript dislikes multiple lines.

Good luck to you all. If you find any other occurrences of this problem that aren’t covered here let me know

Bookmark and Share

Image Caching – Specifically for Firefox

Ok so it’s Friday and it’s 2:45pm. So close to the weekend and i just spent an hour or so figuring out how to get my image caching to work on my thumbnails. Against all odds i have managed to get firefox to cache images properly – well maybe how i want it is a better term

So let me explain my thumbnail creator – Thumbercules, as i have named it (in reference to Gumbercules, a line said my Zoidberg on Futurama (Gumbercules, in reference to someone having the flexibility of Gumbi and the strength of Hercules)), dynamically created thumbnails at a certain height and width specified in the Url that i call eg http://www.domain.com/webimages/x/756/y/160/src/slideshows/images/4a288b1905e1b.jpg. It will check my thumb folder for an already created copy. If it finds one it will return the image from the folder. If not it creates the file then returns it.

What was happening with my previous caching method was that Firefox would download the image, if i pressed reload it would use the cached version it had. But if i pressed reload again it would download it again and start the process over again. It wasn’t recreating the image, only clearing the cache.

So numerous tests were performed until i was able to come up with the following:

function returnImageFile($fileName, $imageType)
{
$expires = 10800;
header("Expires: " . gmdate("D, d M Y H:i:s", strtotime( "+$expires seconds")) . " GMT");
header("Cache-Control: public, max-age=$expires, pre-check=$expires");
header("Pragma: cache", true);
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// if the browser has a cached version of this image, send 304
header("Last-Modified: " . gmdate('D, d M Y H:i:s', filemtime($fileName)).' GMT');
header("HTTP/1.1 304 Not Modified");
die;
}else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($fileName)))
{
// option 2, if you have a file to base your mod date off:
// send the last mod time of the file back
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($fileName)).' GMT',
true, 304);
header("HTTP/1.1 304 Not Modified");
die;
}else
{
header("Last-Modified: " . gmdate('D, d M Y H:i:s', filemtime($fileName)).' GMT');
}
switch ($imageType)
{
case '1':
case 'gif':
header("Content-type: image/gif");
ob_start();
readfile($fileName);
$ImageData = ob_get_contents();
$ImageDataLength = ob_get_length();
ob_end_clean();
header("Content-Length: ".$ImageDataLength);
echo $ImageData;
break;
case '2':
case 'jpg':
case 'jpeg':
header("Content-type: image/jpeg");
ob_start();
readfile($fileName);
$ImageData = ob_get_contents();
$ImageDataLength = ob_get_length();
ob_end_clean();
header("Content-Length: ".$ImageDataLength);
echo $ImageData;
break;
case '3':
case 'png':
header("Content-type: image/png");
ob_start();
readfile($fileName);
$ImageData = ob_get_contents();
$ImageDataLength = ob_get_length();
ob_end_clean();
header("Content-Length: ".$ImageDataLength);
echo $ImageData;
break;
}
die;

This script returns the file with the size value as well as the correct headers to cache the file. If you don’t want to cache the image then you should change the approach slightly, i can’t see many times where you would require thumbnails to not be cached. But for other dynamically created images refer to my other post about creating non cached images. Firefox Caches Images when it Shouldn’t

If anyone has any points to make please do make them, i haven’t had a chance to test this well on IE or Safari. As they don’t have an easy to use plugin like firebug, which tells you when something is being returned from cache or not.

As always good luck with your programming, hopefully this saves someone some of the hassle, or at least saves someone more time then it took me to write this

Bookmark and Share

‘Length’ is null or not an object error in FCKEditor

So i’m back after a lengthy absence in posting fixes. No that’s not because i haven’t had any frustrating bugs that i’ve worked long and hard to solve. Simply i’ve been too busy. Came across this problem in IE where it throws a length is null or not an object error.

After lengthy google searching and tinkering I found the problem to be an extra comma at the end of one of my custom toolbar sets in the fckconfig.js file

/editor/fckconfig.js is the default file path.

Hope this helps anyone in trouble.

Cheers

Bookmark and Share

file_exists doesn’t work for me

Ok i’ve run across this problem a few times in my programming life, where file_exists always returns false. I don’t think i’ve ever properly fixed it. Generally I make some crappy work around, which ends up causing more harm than good.

So here’s a small checklist to solve the problem.

  1. Check the file path you are actually sending the function. eg file_exists(BASE_DIR . “/images/image1.jpg”);
    • I always like to do a print_r on whatever is in between the brackets just to make sure i’m not completely inept.
    • For this example i have too many ‘/’ that could be causing me dilemmas.
  2. Check that the folders/files you are accessing don’t have any safe mode restrictions.
  3. If those 2 solutions didn’t work and you have ensured you have passed the correct file path then put a call to the function “clearstatcache()” before the file_exists function.

If you still have problems even after that, then send me a comment and I’ll help you debug it.

Happy Coding

Bookmark and Share

FCKEditor shows current website instead of editor area

FCKEditor can sometime display a website in it’s iframe instead of the editor panel. This is especially common when using .htaccess rewrite engine.

A friend of mine encountered this issue for a second time, we both always seem to forget the solution, so he suggested I write a blog about it.

When using FCKEditor and our cms we use htaccess redirects, but because they cascade through the sub folders FCKEditor also follows these Redirects.

To make FCKEditor work properly simple add…

RewriteEngine off

…to the .htaccess file in the editor directory.  This will keep FCKEditor using it default settings and not trying to use your parent folder redirects.

Enjoy!

Bookmark and Share