By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

Change this to a function?

100+
P: 133
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
Share this Question
Share on Google+
3 Replies


nathj
Expert 100+
P: 938
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

100+
P: 133
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
Expert 100+
P: 938
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

Post your reply

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