Below I present sample code sheet in PHP and JavaScript.
Sample PHP5 class used to upload images from website to database.
class procms_picture {
private $img; // Image data
private $width; // Output image width
private $height; // Output image height
private $prefs; // reference variable to preferences
private $preserve; // bool, determines whether to preserve aspect ratio or not
private $background = array ( 255, 255, 255 ); // background color for resized image
public function __construct ( $width = null, $height = null ) {
// Preferences containing default values should be loaded first
if ( !isset ( $GLOBALS['prefs'] ) )
die ( "procms_picture::__construct(): class procms_preferences must be initialized first!" );
else
$this->prefs =& $GLOBALS['prefs'];
// Check if picture size was specified on construction. Otherwise, use default values from preferences
if ( $width ) $this->width = $width; else $this->width = $this->prefs->default_thumbnail_x;
if ( $height ) $this->height = $height; else $this->height = $this->prefs->default_thumbnail_y;
// Get information from preferences about preserving apect ratio on resizing
$this->preserve = $this->prefs->thumbnail_preserve_side;
}
private function getFileType ( $filename ) {
// Get EXIF data from image and determine filetype
$exif = getimagesize ( $filename );
switch ( $exif [2] ) {
case IMAGETYPE_GIF:
$img = imagecreatefromgif ( $filename );
if ( !$img ) return false; else return $img;
break;
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg ( $filename );
if ( !$img ) return false; else return $img;
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng ( $filename );
if ( !$img ) return false; else return $img;
break;
default:
// If unknown or unsuported filetype, return false
return false;
}
}
private function resize() {
// Get original image size
$old_height = imagesy ( $this->img );
$old_width = imagesx ( $this->img );
// Check if image should be resized
if ( ( $this->width < $old_width ) || ( $this->height < $old_height ) ) {
// Check picture orientation (landscape or portrait)
if ( $old_height > $old_width ) {
// Portrait
$y = $this->height;
$x = round ( ($old_width/$old_height)*$y );
} else {
// Landscape
$x = $this->width;
$y = round ( ($old_height/$old_width)*$x );
}
} else {
// If original image is smaller than requested thumbnail, exit
return true;
}
// Create resized image
$imgnew = imagecreatetruecolor($x, $y);
// Resize picture
if ( !imagecopyresampled ($imgnew, $this->img, 0, 0, 0, 0, $x, $y, $old_width, $old_height) ) return false;
$this->img = $imgnew;
// Destroy unused variable
imagedestroy ( $imgnew );
return true;
}
public function loadFromFile ( $filename ) {
$this->img = $this->getFileType ( $filename );
if ( !$this->img ) return ERROR_UNKNOWN_FILE_FORMAT;
if ( $this->preserve == 'none' ) return true;
if ( !$this->resize() ) return ERROR_CANT_RESIZE_PICUTRE;
}
public function returnBinary ( $format ) {
// return binary string in order to construct SQL query
ob_start();
switch ( $format ) {
case 'png':
imagepng ( $this->img );
break;
case 'gif':
imagegif ( $this->img );
break;
case 'jpg':
imagejpeg ( $this->img );
break;
default:
// Because function is to return part of SQL query, if specified format was incorrect returning NULL string will avoid future errors.
return "NULL";
}
$data = ob_get_contents();
ob_end_clean();
$data = "0x" . bin2hex ( $data );
return $data;
}
}
Sample JavaScript functions maintaining cross-browsering of my CMS.
/******************************************************************************/ /* JavaScript Sample Code * File encoding: UTF-8 * Script compatibility: Gecko 1.8 */ /******************************************************************************/ // GLOBAL VARIABLES DEFINITIONS var httpRequest = null; // FUNCTION DETECTING MSIE BROWSER function isIE(){ if(-1 != navigator.userAgent.indexOf("MSIE")) { return true; } else { return false; } } // CROSS-BROWSER FUNCTIONS USED TO DETECT VARIOUS OBJECTS' PROPERTIES function screenWidth() { if( typeof( window.innerWidth ) == 'number' ) { // Mozilla compliant return window.innerWidth; } else { if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' return document.documentElement.clientWidth; } else { if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible return document.body.clientWidth; } } } } function screenHeight() { if( typeof( window.innerWidth ) == 'number' ) { // Mozilla compliant return window.innerHeight; } else { if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' return document.documentElement.clientHeight; } else { if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible return document.body.clientHeight; } else { // Other browsers? return 0; } } } } function getTextContent(elm) { return ( elm.textContent || elm.innerText || elm.text ); } function getVerticalOffset() { if ( window.pageYOffset ) { // Mozilla compliant return window.pageYOffset; } else if ( document.body.scrollTop ) { // MSIE 6+ compliant return document.body.scrollTop; } else if ( document.documentElement.scrollTop ) { // MSIE <6 return document.documentElement.scrollTop; } else { // Other browsers? return 0; } } function createNewElement(sTag){ // IE6 supports only uppercase tags to create DOM elements if (isIE()) { obj = document.createElement(sTag.toUpperCase()); } // Other browsers are not as buggy else { obj = document.createElement(sTag); } return obj; }