Jump to content

Can someone please make this a function..


adamjblakey

Recommended Posts

Hi,

 

Please can someone turn this into a function for me:

 

<?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 (isset($_POST['upForm'])){

       $file_type = $_FILES['imgfile']['type'];
       $file_name = $_FILES['imgfile']['name'];
       $file_size = $_FILES['imgfile']['size'];
       $file_tmp = $_FILES['imgfile']['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
           echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
        }

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


}else{ //if the form hasn't been submitted.

      //print the form
      echo "Sorry";

}


?>

 

I want to be able to go e.g. $bannerimage = resizedimage("$_POST['bannerimage']");

 

So i don't have to put the full lot of code all the time.

 

Cheers in advanced for your help.

 

Adam

Link to comment
Share on other sites

At some point you have to learn how to create functions.

 

This is the basic syntax for creating a function.

 

<?php

function name_of_function($argument1, $argument2, $argument3='has a standard value') {

  //The function code goes here

}

?>

 

function name_of_function(){} declares a function called name_of_function().

($argument1, $argumnet2, $argument3='has a default value') specifies 3 arguments which can be passed to the function. $argument3 has a default value which will be used unless it is passed again when the function is called...

{} holds the actual code which will run when the function is called

 

 

So basically all you need to do is put your code inside function resize_image() { } and determine which variables to use as function arguments.

 

The most basic function of yours might look something like this:

 

<?php

function resize_image($image, $width) {

  //your code goes here

}

?>

 

Have a go at it!

 

Wuhtzu

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.