Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    convert this to a function

    i am really struggling after reading php.net on how to create functions. basically i have a code that creates a smaller image of an image i upload. i need to do 2 images. one thumbnail and an image that can be viewed.

    function resizeImage ($directory, $maxwidth, $maxheight, $name)

    that is about as much as ive understood.

    my code i want to change into a function is this:

    PHP Code:
    <?php

    // $image, $maxHeight, $maxWidth

    $pinfo pathinfo($image);
    $tmb_name $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
    if(!isset(
    $maxWidth)){
        
    $maxWidth 100;
    }
    if(!isset(
    $maxHeight)){
        
    $maxHeight 150;
    }

    switch(
    strtolower(substr($image, -3))) {
    case 
    "jpg" :
        
    $fileType "jpeg";
        
    $imageCreateFunction "imagecreatefromjpeg";
        
    $imageOutputFunction "imagejpeg";
        break;
    case 
    "jpeg" :
        
    $fileType "jpeg";
        
    $imageCreateFunction "imagecreatefromjpeg";
        
    $imageOutputFunction "imagejpeg";
        break;
    case 
    "png" :
        
    $fileType "png";
        
    $imageCreateFunction "imagecreatefrompng";
        
    $imageOutputFunction "imagepng";
        break;
    case 
    "gif" :
        
    $fileType "gif";
        
    $imageCreateFunction "imagecreatefromgif";
        
    $imageOutputFunction "imagegif";
        break;
    }

    $size GetImageSize($image);
    $originalWidth $size[0];
    $originalHeight $size[1];

    $x_ratio $maxWidth $originalWidth;
    $y_ratio $maxHeight $originalHeight;

    // check that the new width and height aren't bigger than the original values.
    // the new values are higher than the original, don't resize or we'll lose quality
    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
        
    $newWidth $originalWidth;
        
    $newHeight $originalHeight;
    } else if ((
    $x_ratio $originalHeight) < $maxHeight) {
        
    $newHeight ceil($x_ratio $originalHeight);
        
    $newWidth $maxWidth;
    } else {
        
    $newWidth ceil($y_ratio $originalWidth);
        
    $newHeight $maxHeight;
    }

    $src $imageCreateFunction($image);
    $dst imagecreatetruecolor($newWidth$newHeight);
    // Resample
    imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);

    // Save image
    $imageOutputFunction($dst$tmb_name);

    ImageDestroy($src);
    ImageDestroy($dst);

    ?>
    the reason i need this in a function is as the moment when i upload an image this script is run and is given a maxwidth and maxheight for a thumbnail i need a way of running this script again so i can create an image the user can view when i upload the file.

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Seriously thou, a minute of thought and i'm sure you could've figured out the 3 "real" variables to this

    PHP Code:
    <?php
    function thumbnail($image$maxWidth=100$maxHeight=150) {    
        
    $pinfo        pathinfo($image);
        
    $tmb_name     $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
        
        switch(
    $pinfo['extension']) :
            case 
    "jpg" :
            case 
    "jpeg" 
                
    $fileType "jpeg";
                
    $imageCreateFunction "imagecreatefromjpeg";
                
    $imageOutputFunction "imagejpeg";
            break;
            case 
    "png" :
                
    $fileType "png";
                
    $imageCreateFunction "imagecreatefrompng";
                
    $imageOutputFunction "imagepng";
            break;
            case 
    "gif" :
                
    $fileType "gif";
                
    $imageCreateFunction "imagecreatefromgif";
                
    $imageOutputFunction "imagegif";
            break;
        endswitch;
        
        list(
    $originalWidth$originalHeight) = getimagesize($image);
        
        
    $x_ratio $maxWidth $originalWidth;
        
    $y_ratio $maxHeight $originalHeight;
        
        
    // check that the new width and height aren't bigger than the original values.
        // the new values are higher than the original, don't resize or we'll lose quality
        
    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
            
    $newWidth $originalWidth;
            
    $newHeight $originalHeight;
        } else if ((
    $x_ratio $originalHeight) < $maxHeight) {
            
    $newHeight ceil($x_ratio $originalHeight);
            
    $newWidth $maxWidth;
        } else {
            
    $newWidth ceil($y_ratio $originalWidth);
            
    $newHeight $maxHeight;
        }
        
        
    $src $imageCreateFunction($image);
        
    $dst imagecreatetruecolor($newWidth$newHeight);

        
    // Resample
        
    imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);
        
        
    // Save image
        
    $imageOutputFunction($dst$tmb_name);
        
        
    imagedestroy($src);
        
    imagedestroy($dst);
        
        return 
    true;
    }
    ?>
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    function thumbnail($image$maxWidth$maxHeight) {    
        
    $pinfo        pathinfo($image);
        
    $tmb_name     $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
        
        switch(
    $pinfo['extension']) :
            case 
    "jpg" :
            case 
    "jpeg" 
                
    $fileType "jpeg";
                
    $imageCreateFunction "imagecreatefromjpeg";
                
    $imageOutputFunction "imagejpeg";
            break;
            case 
    "png" :
                
    $fileType "png";
                
    $imageCreateFunction "imagecreatefrompng";
                
    $imageOutputFunction "imagepng";
            break;
            case 
    "gif" :
                
    $fileType "gif";
                
    $imageCreateFunction "imagecreatefromgif";
                
    $imageOutputFunction "imagegif";
            break;
        endswitch;
        
        list(
    $originalWidth$originalHeight) = getimagesize($image);
        
        
    $x_ratio $maxWidth $originalWidth;
        
    $y_ratio $maxHeight $originalHeight;
        
        
    // check that the new width and height aren't bigger than the original values.
        // the new values are higher than the original, don't resize or we'll lose quality
        
    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
            
    $newWidth $originalWidth;
            
    $newHeight $originalHeight;
        } else if ((
    $x_ratio $originalHeight) < $maxHeight) {
            
    $newHeight ceil($x_ratio $originalHeight);
            
    $newWidth $maxWidth;
        } else {
            
    $newWidth ceil($y_ratio $originalWidth);
            
    $newHeight $maxHeight;
        }
        
        
    $src $imageCreateFunction($image);
        
    $dst imagecreatetruecolor($newWidth$newHeight);

        
    // Resample
        
    imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);
        
        
    // Save image
        
    $imageOutputFunction($dst$tmb_name);
        
        
    imagedestroy($src);
        
    imagedestroy($dst);
        
        return 
    true;
    }
    ?>
    if i was to change the maxwidth to just a variable like in the code now.

    and call this code in my upload form:

    PHP Code:
     <?php
     
     $max_size
    =5*1024*1024;
     
     
    // Check if a file has been uploaded
    if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)
    {
         
    // Make sure the file was sent without errors
         
    if($_FILES['uploaded_file']['error'] == 0)
         {
            
    $target_path "images/";
            
    $target_path $target_path basename$_FILES['uploaded_file']['name']);     
            
            if(!
    file_exists($target_path)){

        if(
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
            {
                
                
    functionyourfunctionname($maxheight,$maxwidth,$directory)
    {
       
    $maxheight=150;
       
    $maxwidth=100;
       
    $directory= (where ever);
    }
    function 
    yourfunctionname($maxheight,$maxwidth,$directory)
    {
       
    $maxheight=550;
       
    $maxwidth=600;
       
    $directory= (where ever);
    }

                
                echo 
    "The file ".  basename($_FILES['uploaded_file']['name']). " has been uploaded";
                                         
                
    $dbLink = new mysqli('localhost''root''''gallery');
                 if(
    mysqli_connect_errno()) {
                die(
    "MySQL connection failed: "mysqli_connect_error());
                                            }             
             
                  
    // Gather all required data
                  
    $name $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
                  
    $mime $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
                  
    $size intval($_FILES['uploaded_file']['size']);
                  
    $image_path $dbLink->real_escape_string($target_path);
                  
    $gallery_type $dbLink->real_escape_string($_POST['gallery_type']);
                 
    $desc $dbLink->real_escape_string($_POST['desc']);
                 
    $image_path $dbLink->real_escape_string($target_path);
                 
    $tmb_name $dbLink->real_escape_string($tmb_name);
                 
                 
    //query to insert the data i had gathered into the database
                 
    $query "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` )
                 VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')"
    ;
                 
                 
    //executes the query
                 
    $dbLink->query($query);
            }
        }
          
        else 
             {
                echo 
    'A file with the same name exists please change the file name and try again';
              }
    }
      
      else
              {
          echo 
    'A file was not sent';
            }
    }

    else
              {
          echo 
    'The file is too large';
            }
            
     
    // Echo a link back to the main page
     
    echo '<p>Click <a href="member-index.php">here</a> to go back</p>'
     
    ?>

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I'm not sure what you're asking/saying. Was my func wrapper not what you were looking for?
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i dont know how to use it. i dont want to specify the maxheight and width in that function though i want to specify it when i call it in the add_file.php after it checks to see if the file doesnt exist.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •