PHP Image Resizer Class

class imageResizer {
var $newImage;

function getSquareImage($size, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_sq’. $size .’.jpg’;

//if that image does not exist already (we haven’t resized it before):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $size || $height > $size) {
if ($width > $height) $ratio = $size / $width;
else $ratio = $size / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

function getImageDimensions($sizeX, $sizeY, $image) {
//getting the path and resetting to the new path of the image
$oldPath = explode(‘/’, $image);
$t = sizeof($oldPath) – 1;
$i = 0;
while ($i < $t) {
$newPath = $newPath . $oldPath[$i] . ‘/’;
$i++;
}
$filename = str_replace(‘.gif’, ”, end($oldPath));
$filename = str_replace(‘.jpg’, ”, $filename);
$filename = str_replace(‘.png’, ”, $filename);
$filename = str_replace(‘.JPG’, ”, $filename);
$filename = str_replace(‘.jpeg’, ”, $filename);
$newPath = $newPath . ‘cache/’ . $filename . ‘_’. $sizeX .’x’. $sizeY .’.jpg’;

//if that image does not exist already (we haven’t resized it befoe):
if(!is_file($newPath)) {
list($width, $height) = getimagesize($image);

//check if the image has the right propotions or do:
if ($width > $sizeX || $height > $sizeY) {
if ($width > $height) $ratio = $sizeX / $width;
else $ratio = $sizeY / $height;

$new_width = round($width * $ratio);
$new_height = round($height * $ratio);

$newImg = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($image);
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newImg, $newPath);

$this -> newImage = $newPath;
}
//If the original image is the correct size:
else $this -> newImage = $image;
}
//set this orignal imaget the image returned:
else  $this -> newImage =  $newPath;
return $this -> newImage;
}

}

PHP Image Downloader Class

a simple PHP class with just one method to download images from a remote server. checks for supported types (gif, jpg, png) and will throw an exception if the file is not supported.

usage:

  1. create instance if ImageDownloader
  2. call function downloadImageFrom with parameters: url, destination in local file-system, image name, image type
explanation of download image:
  1. check if the image type is supported
  2. get width and height of original image
  3. create a new image on local machine with width and height
  4. check the filetype and load correct image from imagecreatefrom~ function, parsing the image remote url
  5. resample the image
  6. save the image as the correct filetype using img~ function, parsing the image in memory and the new filename

code:

class ImageDownloader {
var $supported = array(“png”,”jpg”,”gif”);
function downloadImageFrom($url, $to, $fn, $img_type) {
if (in_array($img_type, $this -> supported)) {
list($width, $height) = getimagesize($url);
$newImg = imagecreatetruecolor($width, $height);
$imageTmp = ”;
if ($img_type == ‘png’) {
$imageTmp = imagecreatefrompng($url);
}
elseif ($img_type == ‘jpg’) {
$imageTmp = imagecreatefromjpeg($url);
}
elseif ($img_type == ‘gif’) {
$imageTmp = imagecreatefromgif($url);
}
if ($imageTmp != ”) {
imagecopyresampled($newImg, $imageTmp, 0, 0, 0, 0, $width, $height, $width, $height);
$newPath = $to . $fn . ‘.’ . $img_type;
$this -> imgLoc = $newPath;
if ($img_type == ‘jpg’) {
imagejpeg($newImg, $newPath);
}
elseif ($img_type == ‘gif’) {
imagegif($newImg, $newPath);
}
elseif ($img_type == ‘png’) {
imagepng($newImg, $newPath);
}
}
}
else {
throw new Exception(‘Not supported file-type’);
}
}

Feng Office – Team Collaboration & Project Management software

Feng Office is the successor of a software called OpenGoo and probably one of the best pieces of free, open source software that I have come accross. It’s really on par with MediaWiki and has become a real asset in the development of my site.

It combines a powerful project management and tracking system with a collaboration hub.

Using workspaces, you can conveniently manage multiple projects at the same time and handle resources accross the different projects. User rights prevent people from accessing locations that they are not meant to see.

Through a bit of customisation, you can set it up to not only track changes to all your online files, but also see who is working on which file at the moment. While someone is working on a file, and be this a PHP file or a letter to a supplier, it is marked as ‘checked out’. This prevents two people modifying the file at the same time, thus eliminating one parties work…

It also has features for Notes, email, contacts and a calendar. It’s a truly amazing piece of software and deserves the support fo the community…

PHP function to exchange HTML characters

This is of course if htmlentities does not satisfy you

Ready for you to copy:

function html_strReplaceSymbols( $str) {
$str1 = str_replace( ‘&’, ‘&#38;’, $str);
$str1 = str_replace( ‘ ‘, ‘&#32;’, $str1);
$str1 = str_replace( ‘!’, ‘&#33;’, $str1);
$str1 = str_replace( ‘”‘, ‘&#34;’, $str1);
$str1 = str_replace( ‘$’, ‘&#36;’, $str1);
$str1 = str_replace( ‘%’, ‘&#37;’, $str1);
$str1 = str_replace( “‘”, ‘&#39;’, $str1);
$str1 = str_replace( ‘(‘, ‘&#40;’, $str1);
$str1 = str_replace( ‘)’, ‘&#41;’, $str1);
$str1 = str_replace( ‘*’, ‘&#42;’, $str1);
$str1 = str_replace( ‘+’, ‘&#43;’, $str1);
$str1 = str_replace( ‘,’, ‘&#44;’, $str1);
$str1 = str_replace( ‘-‘, ‘&#45;’, $str1);
$str1 = str_replace( ‘.’, ‘&#46;’, $str1);
$str1 = str_replace( ‘/’, ‘&#47;’, $str1);
$str1 = str_replace( ‘:’, ‘&#58;’, $str1);
$str1 = str_replace( ‘<', '&#60;', $str1); $str1 = str_replace( '=', '&#61;', $str1); $str1 = str_replace( '>‘, ‘&#62;’, $str1);
$str1 = str_replace( ‘?’, ‘&#63;’, $str1);
$str1 = str_replace( ‘[‘, ‘&#91;’, $str1);
$str1 = str_replace( ‘\’, ‘&#92;’, $str1);
$str1 = str_replace( ‘]’, ‘&#93;’, $str1);
$str1 = str_replace( ‘^’, ‘&#94;’, $str1);
$str1 = str_replace( ‘_’, ‘&#95;’, $str1);
$str1 = str_replace( ‘`’, ‘&#96;’, $str1);
$str1 = str_replace( ‘{‘, ‘&#123;’, $str1);
$str1 = str_replace( ‘|’, ‘&#124;’, $str1);
$str1 = str_replace( ‘}’, ‘&#125’, $str1);
$str1 = str_replace( ‘~’, ‘&#126’, $str1);
return $str1;
}