PHP:Basics
<?php
// Enable Debug
ini_set('display_errors', 'On');
error_reporting(E_ALL ^ E_NOTICE);
// Turn off all error reporting
// error_reporting(0);
// Report simple running errors
// error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
// error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
// error_reporting(E_ALL);
// Report all PHP errors
// error_reporting(-1);
// Same as error_reporting(E_ALL);
// ini_set('error_reporting', E_ALL);
ob_start();
session_start();
//database settings
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', 'dbpass');
define('DBNAME', 'dbname');
$db = new PDO('mysql:host='.DBHOST.';port=8889;dbname='.DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//set timezone
date_default_timezone_set('Australia/Adelaide');
//load_classes_as_needed
function __autoload($class){
$classpath = 'classes/class.'.$class.'.php';
if (file_exists($classpath)) {
require_once $classpath;
}
$classpath = '../classes/class.'.$class.'.php';
if (file_exists($classpath)) {
require_once $classpath;
}
$classpath = '../../classes/class.'.$class.'php';
if (file_exists($classpath)) {
require_once $classpath;
}
}
?>
<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/
class ImageHelper {
private $typeToCreatorFunction = array (
IMAGETYPE_JPEG => 'imagecreatefromjpeg',
IMAGETYPE_GIF => 'imagecreatefromgif',
IMAGETYPE_PNG => 'imagecreatefrompng'
);
private $typeToOutputFunction = array (
IMAGETYPE_JPEG => 'imagejpeg',
IMAGETYPE_GIF => 'imagegif',
IMAGETYPE_PNG => 'imagepng'
);
private $image;
private $image_mime;
public function load ($filename) {
if ($this->image != null) {
imagedestroy($this->image);
}
$image_info = getimagesize($filename);
$image_type = $image_info[2];
/* if ($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif ($this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif ($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
} */
$this->image = $this->typeToCreatorFunction[$image_type]($filename);
$this->image_mime = $image_info['mime'];
}
public function save ($filename, $image_type = IMAGETYPE_JPEG, $file_mod = null, $jpegQuality = 100) {
switch ($image_type) {
case IMAGETYPE_JPEG:
// imagejpeg($this->image, $filename, $jpegQuality);
$this->typeToOutputFunction[$image_type]($this->image, $filename, $jpegQuality);
break;
case IMAGETYPE_GIF:
$this->typeToOutputFunction[$image_type]($this->image, $filename);
break;
case IMAGETYPE_PNG:
$this->typeToOutputFunction[$image_type]($this->image, $filename);
break;
}
if ($file_mod != null) {
chmod($filename, $file_mod);
}
}
public function output ($image_type = IMAGETYPE_JPEG, $jpegQuality = 100) {
header('Content-Type: '.$this->image_mime);
// header('Content-Length: '.filesize($file));
if ($image_type != IMAGETYPE_JPEG) {
$this->typeToOutputFunction[$image_type]($this->image);
} else {
$this->typeToOutputFunction[$image_type]($this->image, null, $jpegQuality);
}
}
public function resize ($width, $height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
public function resizeWidth ($width, $syncHeight = false) {
if ($syncHeight) {
$this->resize($width, $width / $this->getWidth() * $this->getHeight());
} else {
$this->resize($width, $this->getHeight());
}
}
public function resizeHeight ($height, $syncWidth = false) {
if ($syncWidth) {
$this->resize($height / $this->getHeight() * $this->getWidth() , $height);
} else {
$this->resize($this->getWidth(), $height);
}
}
public function scale ($percentage) {
$this->resize($this->getWidth() * $percentage, $this->getHeight() * $percentage);
}
private function getWidth () {
return imagesx($this->image);
}
private function getHeight () {
return imagesy($this->image);
}
}
?>
<?php
require('includes/config.php');
$image = $_GET['image'];
$isValidWidth = is_numeric($width);
$isValidHeight = is_numeric($height);
$imageHelper = new ImageHelper();
if (file_exists($image)) {
$imageHelper->load($image);
if ($isValidWidth && $isValidHeight) {
$imageHelper->resize($width, $height);
} elseif ($isValidWidth) {
$imageHelper->resizeWidth($width, TRUE);
} elseif ($isValidHeight) {
$imageHelper->resizeHeight($height, TRUE);
}
$imageHelper->output(IMAGETYPE_JPEG);
}
?>
<?php
require('includes/config.php');
$image = $_GET['image'];
$isValidWidth = is_numeric($width);
$isValidHeight = is_numeric($height);
$imageHelper = new ImageHelper();
if (file_exists($image)) {
$imageHelper->load($image);
if ($isValidWidth && $isValidHeight) {
$imageHelper->resize($width, $height);
} elseif ($isValidWidth) {
$imageHelper->resizeWidth($width, TRUE);
} elseif ($isValidHeight) {
$imageHelper->resizeHeight($height, TRUE);
}
$imageHelper->output(IMAGETYPE_JPEG);
}
?>