473,508 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change this to a function?

133 New Member
Hi,

I want to convert this into a function. How would i go about doing this?

[PHP]$path_thumbs = "images/thumbs";
$path_big = "images/big";

//the new width of the resized image.
$img_thumb_width = 150; // in pixcel

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");


//check if folders are Writable or not
//please CHOMD them 777
if (!is_writeable($path_thumbs)){
die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
}
if (!is_writeable($path_big)){
die ("Error: The directory <b>($path_big)</b> is NOT writable");
}

//if the for has submittedd
if ($_POST){

$file_type = $_FILES['image']['type'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];

//check if you have selected a file.
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't do anything else.
}
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
//print message
}

//upload the big image
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

$imagename = "$rand_name.$file_ext";[/PHP]


Cheers,
Adam
Feb 8 '08 #1
3 1475
nathj
938 Recognized Expert Contributor
Hi,

I would create a class then add the code as a function on that class. Then simply instantiate the class and reference call the function on the object:
[php]
class myObject
{
// property list
var $porperty1 ;
var $property2 ;
public function myObject($pPara1, $pPara2)
{
$this->property1 = $pPara1 ;
$this->property2 = $pPara2 ;
}
function function1()
{
// your code goes here
}

}
[/php]
Then in the calling code:
[php]
<?php
function __autoload($pcClassName)
{
require_once '../lib/' . $pcClassName . '.php' ;
}
session_start() ;
$loObject = new myObject("value1", "value2") ; // the file name used must be myObject in this case

$loObject->function1() ;
?>
[/php]
If the function returns anything then the call should be $lvReturn = $this->fuction1();

I know this is only psuedo code but it should help.

Cheers
nathj

PS a google search on PHP Object, or PHP functions should help too.
Feb 8 '08 #2
adamjblakey
133 New Member
Thank you for your reply, i very much doubt i am at a level thought to write my own class.

Could someone please convert this to a class/function so i can use it?
Feb 8 '08 #3
nathj
938 Recognized Expert Contributor
Thank you for your reply, i very much doubt i am at a level thought to write my own class.
Why so negative about your own ability? Anyone can achieve anything they really set their mind to. Nine months ago I had never developed a web site, and now 'm using PHP classes and building full web systems. You can do this!
Could someone please convert this to a class/function so i can use it?
I've pretty much done this for you already. Take the code sample I gave you, save the class definition to a php file with the same name as the class - this is case sensitive.
Then where you want to use this class load the other lines I gave you.

It's that easy and I've already done more than most - this is a help forum not a have my code written for me forum.

Have a go at it and post back with specific problems when/if you get stuck - I'm happy to help.

Cheers
nathj
Feb 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1844
by: Nick Calladine | last post by:
Is this possible to ... I wish to get the value of a dropdown select but gets is indexable value (dont know if that is the right term) if that is possible (the position it assigned get assigned...
4
20751
by: codemonkey | last post by:
TIA! I would like to know if there is a way to dynamically change the colors of Subform items? I have already set in place Conditional Formatting so each row has an alernating color scheme, but...
4
15485
by: pjac | last post by:
I need some help with some VB language that will change the screen resolution on a monitor when a MS-Access 2000 database is opened from 1024 x 768 to 800 x 600. Any help with this effort would be...
2
2390
by: MLH | last post by:
What's the simplest way to allow a user of an A97 app to change password?
8
8479
by: lauren quantrell | last post by:
Is there a way to force a change in a user's screen resolution using VBA code without having any input from the user? Example: User John Backwards has his screen set to 800 x 600 pixels. Backwards...
1
3496
by: Tony | last post by:
Hi, I have two forms A and B, both opened. In form A, I programmatically change the Date of Birth field of the current record of form B. I noticed that form B automatically displays the new data...
3
4186
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - I have window.status="Moomin"; why doesn't the statusbar change?...
1
6038
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
7
21811
by: moondaddy | last post by:
in asp.net 2.0, i have a link buttonand want to change the forecolor in a mouse over event. how can I do this? -- moondaddy@noemail.noemail
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.