File: //home/awaldron/public_html/archive/ressyn/include/functions/f_upload_resize.php
<?php
/*##################################################################################
| |
| upload script by http://www.reducedcomplexity.com |
| if you use the script please send an email to scripts@reducedcomplexity.com |
| found a bug? please send me an email so i can fix it. |
| |
| use at your own risk! reducedcomplexity.com gives absolutely no warranties. |
| copyright http://www.reducedcomplexity.com | Rudi Bieller |
| |
*/##################################################################################
// turn on public mode / high security ? 1 means public use, 0 means use in admin-areas only.
// for your security: never ever set 1 to 0 when using the script on a public site!!!
// when using mode 0 all files have to be secured (.htacess) so that no-one except you or any other
// admin has access to them.
$pubMode = 1;
// extensions allowed for upload.
$badExt = array("jpg");
// set your password for uploading files; set to $passwordUpl = ""; if no password is needed
// -> if no password is needed / is set, no field for entering a password will be displayed :)
$passwordUpl = "";
// set your password for administration (delete/rename in showuploadFiles.php)
$passwordDel = "";
// max. filesize to be uploaded (value in bytes: 10000 means 10kb, 100000 means 100kb, etc.)
$maxSize = 2097152;
// number of upload fields to be displayed
$uplfields = 1;
// show newname field? 0-no | 1-yes
$show_newname = 0;
// show overwrite field? 0-no | 1-yes
$show_overwrite = 0;
// show rename field? 0-no | 1-yes
$show_rename = 0;
// show autorename field? 0-no | 1-yes
$show_autorename = 0;
// show quit field? 0-no | 1-yes
$show_quit = 0;
// send email to admin when a file was uploaded? 0-no , 1-yes
$sendmail_upl = 0;
// admin email adress(es) - seperate email adresses with a comma, eg. $mailaddress = "you@yourdomain.com, you@anotherdomain.com";
$mailaddress = "admin@5thace.com";
// language to be displayed // available is "en" for english or "de" for deutsch.
$language = "en";
// set the absolute path where you run rc_uploader.
// please keep the trailing slash.
$baseFolder = "/home/awaldron/public_html/5thace/gui/upload/";
// message output: file will be overwritten
$msgOver = "file will be overwritten...";
// message output: file will be renamed
$msgRen = "File Already Exists, Renaming to";
// // message output: file exists, no overwrite.
$msgQuit = "file already exists on server and you chose not to overwrite it. upload aborted.";
// message output: "rename" was checked but no new filename was given
$errMsgOver = "chose a new name for your file first, then submit. - mind the correct extension.";
// message output: renaming is not possible because a file with that name already exists
$errMsgRenex = "new filename is invalid. a file with the same name already exists.";
// message output: general error message in case of a failed upload.
$errMsgCpfail = "copy failed. try again.";
// message output: file size exceeds size limit
$errMsgToobig = error("Upload not successful. File exceeds maximum filesize of $maxSize bytes.");
// message nothing was uploaded
$errMsgNoup = error("Upload not successful. Nothing was uploaded.");
// message output: upload was successful
$msgSuccess = "";
// message output: no local file selected for an upload
$msgPunk = error("Upload not successful. Nothing was uploaded.");
// message output: wrong password was given
$msgPwd = "password is not correct. please try again. - remember: passwords are case-sensitive.";
// message if no file was chosen for delete but del-button was pressed anyway
$msgNofchosen = "please chose a file to be deleted.";
// file-browser: success message when file was deleted successfully
$msgDelsuccess = "delete was successful";
// message for invalid characters
$preg_err_say0 = error("Upload not successful. It is not possible to create a file with one of the following character(s): " "); // edit
$preg_err_say1 = "".<br>please go back and try again."; // edit
// message for invalid extensions
$msgInvExt = error("Upload not successful. The chosen filetype is not permitted for upload.");
// text to be displayed inside the <form></form>
$txt_file4up = "search file to be uploaded!";
$txt_newname = "give file new name? - don't forget the extension!";
$txt_fileexists = "if selected file and/or new filename exists:";
$txt_overwrite = "overwrite?";
$txt_rename = "rename?";
$txt_quit = "quit?";
$txt_autorename = "rename automatically (filename_000.extension)?";
$txt_pass = "password:";
$txt_go = "upload!";
// #############################################################################################
// do not edit below!!!!!!!!!!!!! ##############################################################
// #############################################################################################
define ("PUB_MODE", $pubMode);
define ("PWDUP", $passwordUpl);
define ("PWDADM", $passwordDel);
define ("MAXFSIZE", $maxSize);
define ("UPLFIELDS", $uplfields);
define ("SENDMAILUP", $sendmail_upl);
define ("BASEFOLDER", $baseFolder);
define ("SHOW_NEW", $show_newname);
define ("SHOW_OVER", $show_overwrite);
define ("SHOW_REN", $show_rename);
define ("SHOW_QUIT", $show_quit);
define ("SHOW_AUTORENAME", $show_autorename);
define ("MSG_OVER", $msgOver);
define ("MSG_REN", $msgRen);
define ("MSG_QUIT", $msgQuit);
define ("ERR_MSG_OVER", $errMsgOver);
define ("ERR_MSG_RENEX", $errMsgRenex);
define ("ERR_MSG_CPFAIL", $errMsgCpfail);
define ("ERR_MSG_TOOBIG", $errMsgToobig);
define ("ERR_MSG_NOUP", $errMsgNoup);
define ("MSG_SUCCESS", $msgSuccess);
define ("MSG_PUNK", $msgPunk);
define ("MSG_PWD", $msgPwd);
define ("MSG_NOFCHOSEN", $msgNofchosen);
define ("MSG_DELSUCCESS", $msgDelsuccess);
define ("MSG_INVCHAR1", $preg_err_say0);
define ("MSG_INVCHAR2", $preg_err_say1);
define ("MSG_INVEXT", $msgInvExt);
define ("TXT_FILE4UP", $txt_file4up);
define ("TXT_NEWNAME", $txt_newname);
define ("TXT_FILEEXISTS", $txt_fileexists);
define ("TXT_OVERWRITE", $txt_overwrite);
define ("TXT_RENAME", $txt_rename);
define ("TXT_QUIT", $txt_quit);
define ("TXT_AUTORENAME", $txt_autorename);
define ("TXT_PASS", $txt_pass);
define ("TXT_GO", $txt_go);
function upload($RCFILES) {
global $badExt;
global $mailaddress;
$oimg = $_POST["gennewname0"];
$fn = str_replace(" ","_",$RCFILES['file2upl']['name'][0]);
for($i=0;$i<count($RCFILES["file2upl"]["tmp_name"]);$i++) {
$RCFILES['file2upl']['name'][$i] = str_replace(" ","_",$RCFILES['file2upl']['name'][$i]);
$destination = BASEFOLDER . $RCFILES['file2upl']['name'][$i]; // do NOT edit this line!
if (count($RCFILES["file2upl"]["tmp_name"])>1) $j = $i + 1;
else $j = "";
//if (!empty($RCFILES["file2upl"]["name"][$i])) //echo $j . " " . $RCFILES["file2upl"]["name"][$i] . ":<br>\n";
//else //echo $j . " --<br>\n";
if ( (isset($_POST["pwd"]) && $_POST["pwd"] == PWDUP) || PWDUP == "") { // password is ok
$test = opendir(BASEFOLDER);
if ($test === FALSE) { // upload-folder does not exist yet
mkdir (BASEFOLDER, 0777);
}
if ($RCFILES['file2upl']['tmp_name'][$i] == "none") { // nothing was uploaded
//echo "<P>Filesize_1 : " . $RCFILES['file2upl']['size'][$i] . " / " . MAXFSIZE;
//echo ERR_MSG_NOUP;
$ok = "no";
} elseif ( (!checkExt($RCFILES['file2upl']['name'][$i])) && (PUB_MODE == 1) ) { // check the extensions
//echo MSG_INVEXT;
$ok = "no";
} elseif ($RCFILES['file2upl']['size'][$i] > MAXFSIZE) { // filesize is too big
//echo "<P>Filesize_2 : " . $RCFILES['file2upl']['size'][$i] . " / " . MAXFSIZE;
//echo ERR_MSG_TOOBIG;
$ok = "no";
} else { // file size is ok
//echo "<P>Is Uploaded File? " . is_uploaded_file($RCFILES['file2upl']['tmp_name'][$i]);
//echo "<br>File Exists? " . file_exists($RCFILES['file2upl']['tmp_name'][$i]);
if (is_uploaded_file($RCFILES['file2upl']['tmp_name'][$i])) {
if (file_exists($destination) && empty($_POST["gennewname".$i])) { // file already exists, no new name was set
switch($_POST["fileexists".$i]) {
case "overwrite":
$target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
//echo MSG_OVER;
break;
case "autorename":
$autoname = autoRename($RCFILES['file2upl']['name'][$i]);
$target = BASEFOLDER . $autoname;
$fn = $autoname;
//echo "<b>".MSG_REN." '".$autoname."'</b><br>\n";
break;
case "rename":
if(empty($_POST["newname".$i])) {
//echo ERR_MSG_OVER;
$ok = "no";
}
if (file_exists(BASEFOLDER . $_POST["newname".$i]) && (!isset($ok))) {
//echo ERR_MSG_RENEX;
$ok = "no";
}
if (!checkName($_POST["newname".$i])) {
$ok = "no";
}
if ( (!checkExt($_POST["newname".$i])) && (PUB_MODE == 1) ) { // check the extensions
//echo MSG_INVEXT;
$ok = "no";
}
$target = BASEFOLDER . $_POST["newname".$i];
if (!isset($ok)) //echo MSG_REN;
break;
case "quit":
$target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
//echo MSG_QUIT;
$ok = "no";
break;
default:
$target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
//echo MSG_QUIT;
$ok = "no";
}
} elseif (isset($_POST["gennewname".$i]) && !empty($_POST["gennewname".$i])) { // file should get a new name
if (empty($_POST["gennewname".$i])) { // is a new name given? usually yes.. ;)
//echo ERR_MSG_OVER;
$ok = "no";
}
if (!checkName($_POST["gennewname".$i])) { // check invalid characters in name
$ok = "no";
}
if ( (!checkExt($_POST["gennewname".$i])) && (PUB_MODE == 1) ) { // check the extensions
//echo MSG_INVEXT;
$ok = "no";
}
if (file_exists(BASEFOLDER . $_POST["gennewname".$i]) && (!isset($ok))) { // a file with the new name already exists
if (!isset($_POST["fileexists".$i]) || $_POST["fileexists".$i] == "quit") {
$target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
//echo ERR_MSG_RENEX;
$ok = "no";
} else {
###############################
switch($_POST["fileexists".$i]) {
case "overwrite":
$target = BASEFOLDER . $_POST["gennewname".$i];
//echo MSG_OVER;
break;
case "autorename":
$autoname = autoRename($RCFILES['file2upl']['name'][$i]);
$target = BASEFOLDER . $autoname;
$fn = $autoname;
//echo "<b>".MSG_REN." '".$autoname."'</b><br>\n";
break;
case "rename":
if(empty($_POST["newname".$i])) {
//echo ERR_MSG_OVER;
$ok = "no";
}
if (file_exists(BASEFOLDER . $_POST["newname".$i]) && (!isset($ok))) {
//echo ERR_MSG_RENEX;
$ok = "no";
}
if (!checkName($_POST["newname".$i])) {
$ok = "no";
}
if ( (!checkExt($_POST["newname".$i])) && (PUB_MODE == 1) ) {
//echo MSG_INVEXT;
$ok = "no";
}
$target = BASEFOLDER . $_POST["newname".$i];
if (!isset($ok)) //echo MSG_REN;
break;
default:
$target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
//echo MSG_QUIT;
$ok = "no";
}
###############################
}
} else { // new name is ok
$target = BASEFOLDER . $_POST["gennewname".$i];
}
} else { // file does not exist yet
if (!checkName($RCFILES['file2upl']['name'][$i])) $ok = "no";
else $target = BASEFOLDER . $RCFILES['file2upl']['name'][$i];
}
if (!isset($ok) || $ok != "no") {
$fn = $target;
move_uploaded_file($RCFILES['file2upl']['tmp_name'][$i], $target) or die ($errMsgCpfail);
@chmod($target, 0755); // in case 600 is set after move_uploaded_file
//echo "<P>Filesize_Punk : " . $RCFILES['file2upl']['size'][$i] . " / " . MAXFSIZE;
//echo "<P>Filename_Punk : " . $RCFILES['file2upl']['name'][$i];
//echo "<P>Proposed Name : " . $_POST["gennewname0"];
//echo "<P>FileNewName_Punk : " . $RCFILES['file2upl']['tmp_name'][$i];
//echo MSG_SUCCESS;
if (SENDMAILUP == 1) {
$ip = $_SERVER["REMOTE_ADDR"];
$mailsubject = "reducedcomplexity uploader: new file uploaded!";
$mailbody = "changes in your upload-folder:\n";
$mailbody .= "$target\n\n";
$mailbody .= "date of upload: ".date("Y-m-d, H:i:s")."\n";
$mailbody .= "ip of uploader: $ip\n\n";
$mailbody .= "reducedcomplexity uploader\n";
$mailbody .= "http://www.reducedcomplexity.com/scripts\n";
@mail($mailaddress, $mailsubject, $mailbody);
}
}
} else { // !file_is_uploaded
//echo "<P>Filesize_Punk : " . $RCFILES['file2upl']['size'][$i] . " / " . MAXFSIZE;
//echo "<P>Filename_Punk : " . $RCFILES['file2upl']['name'][$i];
//echo "<P>Proposed Name : " . $_POST["gennewname0"];
//echo "<P>FileNewName_Punk : " . $RCFILES['file2upl']['tmp_name'][$i];
//echo "<P>" . MSG_PUNK;
}
}
} elseif (isset($_POST["pwd"]) && $_POST["pwd"] != PWDUP) { // wrong password
//echo MSG_PWD;
} else { // no password set at all / impossible ;)
//echo MSG_PWD;
}
//print "<br>\n";
//unset ($ok);
}// end for
if ($ok == "no") return FALSE;
else return $fn;
}// end function
////////////////////////////////////////////////////////////
// FILES ASSOCIATED WITH UPLOAD FUNCITON ///////////////////
////////////////////////////////////////////////////////////
function checkName($names) {
// '\' => '\',
$slashtest = explode("/", $names);
if (sizeof($slashtest) > 1) $preg_err[] = "/ ";
$backslashtest = explode("\\", $names);
if (sizeof($backslashtest) > 1) $preg_err[] = "\ ";
$invalidArr = array(
' ' => 'blanks',
"'" => "'",
'\"' => """,
'&' => '&',
',' => ',',
';' => ';',
'`' => '`',
'<' => '<',
'>' => '>',
':' => ':',
'|' => '|',
'�' => '�',
'^' => '^',
'=' => '=',
'$' => '$',
'*' => '*',
'?' => '?',
'+' => '+',
'(' => '(',
')' => ')',
'%' => '%',
'#' => '#'
);
foreach($invalidArr as $badChar=>$eigenschaft) {
if (strstr($names, $badChar)) $preg_err[] = "$eigenschaft ";
}
if (!isset($preg_err)) return true;
else {
//echo MSG_INVCHAR1;
foreach ($preg_err as $single_err) {
//echo stripslashes($single_err);
}
//echo MSG_INVCHAR2;
return false;
}
}
// ---------------------------------------------------------------------------------------
// check for malicious extensions when high security is on -------------------------------
// ---------------------------------------------------------------------------------------
function checkExt($fname) {
global $badExt;
$path = pathinfo($fname);
foreach($badExt as $singleExt) {
$singleExt = strtolower($singleExt);
if (strtolower(@$path["extension"]) == $singleExt) $ok[] = "no";
if (@$path["extension"] == "") $ok[] = "no";
}
if (isset($ok) && sizeof($ok) > 0) {
return true;
} else {
return false;
}
}
// ---------------------------------------------------------------------------------------
// show readable filesize ----------------------------------------------------------------
// ---------------------------------------------------------------------------------------
function niceFilesize($f_size) {
if ($f_size >= 1073741824) {
$show_filesize = number_format(($f_size / 1073741824),2) . " gb";
} elseif ($f_size >= 1048576) {
$show_filesize = number_format(($f_size / 1048576),2) . " mb";
} elseif ($f_size >= 1024) {
$show_filesize = number_format(($f_size / 1024),2) . " kb";
} elseif ($f_size >= 0) {
$show_filesize = $f_size . " bytes";
} else {
$show_filesize = "0 bytes";
}
return $show_filesize;
}
// ---------------------------------------------------------------------------------------
// delete one or more file(s) in the upload folder ---------------------------------------
// ---------------------------------------------------------------------------------------
function delFiles($file) {
if (isset($file)) {
if (!isset($_POST["pwd"]) || $_POST["pwd"] != PWDADM) {
//echo MSG_PWD;
$ok = "No";
} else {
foreach($file as $delme) {
$todel = BASEFOLDER . $delme;
unlink($todel);
}
//echo MSG_DELSUCCESS;
}
} else {
//echo MSG_NOFCHOSEN; // no checkbox was selected when clicking delete button
}
}
// ---------------------------------------------------------------------------------------
// rename one or more file(s) in the upload folder ---------------------------------------
// ---------------------------------------------------------------------------------------
function renFiles($alt, $neu) {
$old = BASEFOLDER . $alt;
$new = BASEFOLDER . $neu;
if (!isset($_POST["pwd"]) || $_POST["pwd"] != PWDADM) {
//echo MSG_PWD;
$ok = "No";
} else {
// check new name....
if (!checkName($neu)) {
$ok = "no";
}
if (PUB_MODE == 1) {
if (!checkExt($neu)) {
//echo MSG_INVEXT;
$ok = "no";
}
}
if (file_exists($new)) {
//echo ERR_MSG_RENEX;
$ok ="no";
}
if (!isset($ok)) {
if (rename ($old, $new)) echo "rename ok.";
else echo "error: rename failed.";
}
}
}
// ---------------------------------------------------------------------------------------
// autorename a file in case filename already exists -------------------------------------
// ---------------------------------------------------------------------------------------
function autoRename($filename) {
$path = pathinfo($filename);
$filename_full = $path["basename"];
$filename_extension = @$path["extension"];
if (isset($filename_extension) && !empty($filename_extension)) {
$filename_extension = "." . $filename_extension;
$position = strpos($filename_full, $filename_extension); // get the position of ".extension"
$filename_noExt = substr($filename, 0, $position); // extract the filename without extension
} else {
$filename_noExt = $filename_full;
}
// dann den neuen namen erstellen
$n = 0;
$copy = "";
while(file_exists(BASEFOLDER . $filename_noExt . $copy . $filename_extension)) {
if ($n<=9) $n = "00" . $n;
if ($n<=99 && $n>=10) $n = "0" . $n;
if ($n<=999 && $n>=100) $n = "" . $n;
$copy = "_" . $n;
$n++;
}
// return new filename
return $new_filename = $filename_noExt . $copy . $filename_extension;
}
// ---------------------------------------------------------------------------------------
// function to show the html between <form> and </form> ----------------------------------
// ---------------------------------------------------------------------------------------
function showForm() {
// do not edit this ---------------------------------------------------------------------
print "<form action=\"".$_SERVER["PHP_SELF"]."\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"".MAXFSIZE."\">\n";
// --------------------------------------------------------------------------------------
// displays the upload fields
// you may edit this carefully, but keep in mind:
// -> edit the html only!
// -> do not change any name attribute of an input tag!
// -> if you want to add a class oder style attribute use the preserved expression
// -----------------------------------------------------------------------------------------
for ($i=0; $i<UPLFIELDS; $i++)
{
//echo TXT_FILE4UP."<br>\n";
//echo "<input type=\"file\" name=\"file2upl[]\" size=\"36\"><br>\n";
//echo "<br>\n";
if (SHOW_NEW == 1)
{
//echo TXT_NEWNAME."<br>\n";
//echo "<input type=\"text\" name=\"gennewname$i\" size=\"36\"><br><br>\n";
}
if (SHOW_OVER == 1 || SHOW_REN == 1 || SHOW_QUIT == 1 || SHOW_AUTORENAME == 1)
{
//echo TXT_FILEEXISTS."<br>\n";
//echo "<table border=\"0\">\n";
if (SHOW_OVER == 1)
{
//echo "<tr>\n";
//echo "<td><input type=\"radio\" name=\"fileexists$i\" value=\"overwrite\"></td>\n";
//echo "<td>".TXT_OVERWRITE."</td>\n";
//echo "</tr>\n";
}
if (SHOW_REN == 1)
{
//echo "<tr>\n";
//echo "<td><input type=\"radio\" name=\"fileexists$i\" value=\"rename\"></td>\n";
//echo "<td>".TXT_NEWNAME."<br><input type=\"text\" name=\"newname$i\" size=\"36\"></td>\n";
//echo "</tr>\n";
}
if (SHOW_AUTORENAME == 1)
{
//echo "<tr>\n";
//echo "<td><input type=\"radio\" name=\"fileexists$i\" value=\"autorename\"></td>\n";
//echo "<td>".TXT_AUTORENAME."</td>\n";
//echo "</tr>\n";
}
if (SHOW_QUIT == 1)
{
//echo "<tr>\n";
//echo "<td><input type=\"radio\" name=\"fileexists$i\" value=\"quit\" checked></td>\n";
//echo "<td>".TXT_QUIT."</td>\n";
//echo "</tr>\n";
}
//echo "</table>\n";
}
//echo "<p><br></p>\n";
}
// end
if (PWDUP != "")
{
//echo "<br>\n";
//echo TXT_PASS."<br>\n";
//echo "<input type=\"password\" name=\"pwd\" class=\"form\"><br><br>\n";
//echo "<input type=\"submit\" name=\"upload\" value=\"".TXT_GO."\">\n";
//echo "<br><br>\n";
//echo "<small style=\"font-size:08pt;\">upload script © <a href=\"http://www.reducedcomplexity.com\" target=\"_blank\">reducedcomplexity.com</a></small>\n";
//echo "</form>\n";
}
else
{
//echo "<br>\n";
//echo "<input type=\"submit\" name=\"upload\" value=\"".TXT_GO."\">\n";
//echo "<br><br>\n";
//echo "<small style=\"font-size:08pt;\">upload script © <a href=\"http://www.reducedcomplexity.com\" target=\"_blank\">reducedcomplexity.com</a></small>\n";
//echo "</form>\n";
}
} // end of function
/************************************************************************************************
Purpose: class to handle image resize; can output to file or directly to browser
Author: Yuriy Horobey, yuriy@horobey.com
Property: Horobey Freelance & Telecommuting
URL: http://horobey.com
Date: 12.04.2003
************************************************************************************************/
$ERR["UNABLE_TO_OUTPUT"] = "Unable to output: ";
$ERR["FILE_DOESNOT_EXSIT"] = "This file does not exist: ";
$ERR["FUNCTION_DOESNOT_EXIST"] = "This function does not exist: ";
$ERR["GD2_NOT_CREATED"] = "GD2 is installed, function ImageCreateTruecolor() exists, but image is not created";
$ERR["IMG_NOT_CREATED"] = "Image is not created ImageCreate(). {GD2 suppor is OFF}";
$ERR["GD2_UNAVALABLE"] = "You specified to use GD2, but not all GD2 functions are present.";
$ERR["GD2_NOT_RESIZED"] = "GD2 is installed, function ImageCopyResampled() exists, but image is not resized";
$ERR["IMG_NOT_RESIZED"] = "Image was not resized. {GD2 suppor is OFF}";
$ERR["UNKNOWN_OUTPUT_FORMAT"] = "This image format cannot bu output: ";
$ERR["NO_IMAGE_FOR_OUTPUT"] = "Image you are trying to output does not exist. ";
$ERR["IMG_NOT_SUPPORTED"] = "Can not create image. Sorry, this image type is not supported yet.";
//this class works with image
class img_resize {
var $image_original;
var $file_original;
var $image_original_width;
var $image_original_height;
var $image_original_type_code;
var $image_original_type_abbr;
var $image_original_html_sizes;
var $image_resized;
var $file_resized;
var $image_resized_width;
var $image_resized_height;
var $image_resized_type_code;
var $image_resized_type_abbr;
var $image_resized_html_sizes;
//some settings
var $jpeg_quality;
var $use_gd2;
function img_resize($file_original){
//constructor of the class
//it takes given file and creates image out of it
global $ERR;
$this->clear(); // clear all.
if(file_exists($file_original)) {
$this->file_original = $file_original;
$this->image_original = $this->imagecreatefromfile($file_original);
if(!$this->image_original){
$this->error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original");
return false;
}
} else {
$this->error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
}
}
function clear() {
// clear all the class member varaibles
$this->image_original = 0;
$this->file_original = "";
$this->image_original_width = 0;
$this->image_original_height = 0;
$this->image_original_type_code = 0;
$this->image_original_type_abbr = "";
$this->image_original_html_sizes= "";
$this->image_resized = 0;
$this->file_resized = "";
$this->image_resized_width = 0;
$this->image_resized_height = 0;
$this->image_resized_type_code = -1;
$this->image_resized_type_abbr = "";
$this->image_resized_html_sizes = "";
$this->set_parameters();
}
function set_parameters($jpeg_quality="85", $use_gd2=true) {
$this->jpeg_quality=$jpeg_quality;
$this->use_gd2=$use_gd2;
}
function error($msg){
//error messages and debug info:
// here you can implement your own error handling
//echo("<hr color='red'><font color='red'><b>$msg</b></font><br> file=<b>".__FILE__."</b><hr color='red'>");
}
function imagecreatefromfile($img_file){
global $ERR;
$img=0;
$img_sz = getimagesize( $img_file ); ## returns array with some properties like dimensions and type;
####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
switch( $img_sz[2] ){
case 1:
$img = $this->_imagecheckandcreate("ImageCreateFromGif", $img_file);
$img_type = "GIF";
break;
case 2:
$img = $this->_imagecheckandcreate("ImageCreateFromJpeg", $img_file);
$img_type = "JPG";
break;
case 3:
$img = $this->_imagecheckandcreate("ImageCreateFromPng", $img_file);
$img_type = "PNG";
break;
// would be nice if this function will be finally supported
case 4:
$img = $this->_imagecheckandcreate("ImageCreateFromSwf", $img_file);
$img_type = "SWF";
break;
default:
$img = 0;
$img_type = "UNKNOWN";
$this->error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
break;
}//case
if($img){
$this->image_original_width=$img_sz[0];
$this->image_original_height=$img_sz[1];
$this->image_original_type_code=$img_sz[2];
$this->image_original_type_abbr=$img_type;
$this->image_original_html_sizes=$img_sz[3];
}else {
$this->clear();
}
return $img;
}
function _imagecheckandcreate($function, $img_file) {
//inner function used from imagecreatefromfile().
//Checks if the function exists and returns
//created image or false
global $ERR;
if(function_exists($function)) {
$img = $function($img_file);
}else{
$img = false;
$this->error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
}
return $img;
}
function resize($desired_width, $desired_height, $mode="-"){
//this is core function--it resizes created image
//if any of parameters == "*" then no resizing on this parameter
//>> mode = "+" then image is resized to cover the region specified by desired_width, _height
//>> mode = "-" then image is resized to fit into the region specified by desired_width, _height
// width-to-height ratio is all the time the same
//>>mode=0 then image will be exactly resized to $desired_width _height.
//geometrical distortion can occur in this case.
// say u have picture 400x300 and there is circle on the picture
//now u resized in mode=0 to 800x300 -- circle shape will be distorted and will look like ellipse.
//GD2 provides much better quality but is not everywhere installed
global $ERR;
if($desired_width == "*" && $desired_height == "*"){
$this->image_resized = $this->image_original;
Return true;
}
switch($mode) {
case "-":
case '+':
//multipliers
if($desired_width != "*") $mult_x = $desired_width / $this->image_original_width;
if($desired_height != "*") $mult_y = $desired_height / $this->image_original_height;
$ratio = $this->image_original_width / $this->image_original_height;
if($desired_width == "*"){
$new_height = $desired_height;
$new_width = $ratio * $desired_height;
}elseif($desired_height == "*"){
$new_height = $desired_width / $ratio;
$new_width = $desired_width;
}else{
if($mode=="-"){
if( $this->image_original_height * $mult_x < $desired_height ){
//image must be smaller than given $desired_ region
//test which multiplier gives us best result
//$mult_x does the job
$new_width = $desired_width;
$new_height = $this->image_original_height * $mult_x;
}else{
//$mult_y does the job
$new_width = $this->image_original_width * $mult_y;
$new_height = $desired_height;
}
}else{
//mode == "+"
// cover the region
//image must be bigger than given $desired_ region
//test which multiplier gives us best result
if( $this->image_original_height * $mult_x > $desired_height ){
//$mult_x does the job
$new_width = $desired_width;
$new_height = $this->image_original_height * $mult_x;
}else{
//$mult_y does the job
$new_width = $this->image_original_width * $mult_y;
$new_height = $desired_height;
}
}
}
break;
case '0':
//fit the region exactly.
if($desired_width == "*") $desired_width = $this->image_original_width;
if($desired_height == "*") $desired_height = $this->image_original_height;
$new_width = $desired_width;
$new_height = $desired_height;
break;
default:
$this->error($ERR["UNKNOWN_RESIZE_MODE"]." $mode");
break;
}
// OK here we have $new_width _height
//create destination image checking for GD2 functions:
if( $this->use_gd2 ){
if( function_exists("imagecreatetruecolor")){
$this->image_resized = imagecreatetruecolor($new_width, $new_height) or $this->error($ERR["GD2_NOT_CREATED"]);
}else {
$this->error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
}
} else {
$this->image_resized = imagecreate($new_width, $new_height) or $this->error($ERR["IMG_NOT_CREATED"]);
}
//Resize
if( $this->use_gd2 ){
if( function_exists("imagecopyresampled")){
$res = imagecopyresampled($this->image_resized,
$this->image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this->image_original_width, $this->image_original_height // src sizes
) or $this->error($ERR["GD2_NOT_RESIZED"]);
}else {
$this->error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
}
} else {
$res = imagecopyresized($this->image_resized,
$this->image_original,
0, 0, //dest coord
0, 0, //source coord
$new_width, $new_height, //dest sizes
$this->image_original_width, $this->image_original_height // src sizes
) or $this->error($ERR["IMG_NOT_RESIZED"]);
}
}
function output_original($destination_file, $image_type="JPG") {
//outputs original image
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
return _output_image($destination_file, $image_type, $this->image_original);
}
function output_resized($destination_file, $image_type="JPG") {
//if destination file is empty image will be output to browser
// right now $image_type can be JPG or PNG
$res = $this->_output_image($destination_file, $image_type, $this->image_resized);
if(trim($destination_file)){
$sz=getimagesize($destination_file);
$this->file_resized = $destination_file;
$this->image_resized_width = $sz[0];
$this->image_resized_height = $sz[1];
$this->image_resized_type_code=$sz[2];
$this->image_resized_html_sizes=$sz[3];
//only jpeg and png are really supported, but I'd like to think of future
switch($this->image_resized_html_sizes){
case 0:
$this->image_resized_type_abbr = "GIF";
break;
case 1:
$this->image_resized_type_abbr = "JPG";
break;
case 2:
$this->image_resized_type_abbr = "PNG";
break;
case 3:
$this->image_resized_type_abbr = "SWF";
break;
default:
$this->image_resized_type_abbr = "UNKNOWN";
break;
}
}
return $res;
}
function _output_image($destination_file, $image_type, $image){
//if destination file is empty image will be output to browser
// right now $image_type can be JPEG or PNG
global $ERR;
$destination_file = trim($destination_file);
$res = false;
if($image){
switch($image_type) {
case 'JPEG':
case 'JPG':
$res = ImageJpeg($image, $destination_file, $this->jpeg_quality);
break;
case 'PNG':
$res = Imagepng($image, $destination_file);
break;
default:
$this->error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type");
break;
}
}else{
$this->error($ERR["NO_IMAGE_FOR_OUTPUT"]);
}
if(!$res) $this->error($ERR["UNABLE_TO_OUTPUT"]." $destination_file");
return $res;
}
}
?>