andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


Upload 2 Crop And Save Cropped

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Crop Canvas
View previous topic :: View next topic  
Author Message
EssEt
Newbie


Joined: 21 Dec 2004
Posts: 12

PostPosted: Tue Dec 21, 2004 2:00 pm    Post subject: Upload 2 Crop And Save Cropped Reply with quote

Hi there... I know that everyone say "omg i love your script" so i just have to say it to

Your script is really classy - Nice work man!

I code a lot to and i love classes in php, just divine!

Now i want to your wonderful script and tweak it a bit so you at first see a upload form where you can upload a pic ... size doesnt matter... and the pic is uploaded to the server as a temporary file. when the upload is finished you will get to the crop interface and when you have selected the area and press cropp the cropped file will be saved as a another file in a another directory.... is that possible ?

Best regards
Thanks anyway and godspeed // EssEt
Back to top
View user's profile Send private message Send e-mail
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Wed Dec 22, 2004 8:13 am    Post subject: Reply with quote

Hi there,

Welcome to the board and many thanks for your kind comments! Smile

What you want is totally possible. The class has been built around the fact that it's not a one-stop script and if you put it on your server it'll do the work of an entire application. Instead, it's built around the concept that you might want to integrate a cropping function into your own application. With that said, you'll have to build the script to upload and save a cropped image. This, however, is pretty simply!

First you need to upload the image, then you need to crop it and then save it. How you do this is up to you; one file, separate files for each action, etc., etc. But here's a little push in the right direction...

config.php

PHP:
<?php

    define
('BASE_DIR',   dirname(__FILE__) . '/');
    
define('UPLOAD_DIR'BASE_DIR 'uploaded/');
    
define('CROP_DIR',   BASE_DIR 'cropped/');

?>


upload.php

PHP:
<?php

    
require_once('config.php');

    
ob_start();
    if (!isset(
$_FILES) || empty($_FILES)) {
        echo 
'<form action="" method="post" enctype="multipart/form-data">';
        echo 
'Select your image type to upload<br />';
        echo 
'<input type="file" name="image"> <input type="submit" value="upload"><br />';
        echo 
'</form>';
    } else {
        
$filename str_replace(' ''_'preg_replace('/\s+/'' 'preg_replace('/[^a-z0-9-_ ]/i'''$_FILES['image']['name'])));
        if (
move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR $filename)) {
            
header("Location: crop.php?file=$filename");
        } else {
            echo 
'There was an error uploading that file.';
            
print_r($_FILES);
        }
    }
    
ob_end_flush();

?>


crop.php

PHP:
<?php <?php

    
require_once('config.php');
    require_once(
'class.cropinterface.php');
    
$ci =& new cropInterface();
    
    if (isset(
$_GET['file']) && isset($_GET['sx']))
    {
        
$ci->loadImage($_GET['file']);
        
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
        
$ci->saveImage(CROP_DIR $_GET['file']);
        
header('Content-type: image/jpeg');
        
$ci->showImage('jpg'90);
        exit;
    }

?>

<html>

<head>
    <style type="text/css">
        body, td, p
        {
            font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
            font-size: 10px;
        }
        
        #submit
        {
            font-size: 10px;
            font-family: "MS Sans Serif", Geneva, sans-serif;
            height: 23px;
            background-color: #D4D0C8;
            border: 0px;
            padding: 3px,5px,3px,5px;
            width: 100%;
        }
    </style>
</head>
<body>

    <?php

        $ci
->loadInterface(UPLOAD_DIR $_GET['file']);
        
$ci->loadJavaScript();

    
?>

</body>
</html> ?>


I don't promise that the above will work right off the bat, and you may need to make some tweaks. But it should guide you in the right direction.

You may also need to apply the 'DOCUMENT_ROOT' fix shown in this thread:

http://php.amnuts.com/forums/viewtopic.php?t=129

Hope that helps!

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
EssEt
Newbie


Joined: 21 Dec 2004
Posts: 12

PostPosted: Thu Dec 23, 2004 4:25 pm    Post subject: Thnx... but having probs Reply with quote

Hi amnuts i am soooooo greatfull for your support!
Love how you explain... step by step... man your awsome!

I am trying to integrate the script you gave me to my upload code... im running a template class in all the glory... something is twisted i think... check it out...

change.php

Code:

   function ChangeUserPic()
   {
      if (defined('USER_RANK'))
      {
         if (!empty($_POST['avatar_gallery']))
         {
            $sql = "UPDATE ".MYSQL_PREFIX."_users SET u_pic='images/avatars/".$_POST['avatar_gallery']."', u_join = '".$_POST['join']."' WHERE u_id='".USER_ID."'";
            mysql_query($sql, CONNECT);
            return $this->EditUserPic();
         }
         else
         {
            if (!empty($_FILES['avtar_upload'][name]))
            {

               if (strstr($_FILES['avtar_upload'][type],"image"))
               {   
                  $size = $_FILES['avtar_upload'][size] * 0.001024;
                  /* converts the file size form bytes to kilobytes */
                  /* Get max filesize */
                  $sql = "SELECT s_value FROM ".MYSQL_PREFIX."_site WHERE s_name='avtarmaxsize'";
                  $query = mysql_query($sql, CONNECT);
                  $row = mysql_fetch_array($query);
                           
                  if ($size < $row[s_value])
                  {
                     $ext = substr($_FILES['avtar_upload']['name'],strpos($_FILES['avtar_upload']['name'],'.'));
                     $url = "images/users/". USER_ID . $ext;
                     copy($_FILES['avtar_upload']['tmp_name'], $url);
                     $sql = "UPDATE ".MYSQL_PREFIX."_users SET u_pic = '".$url."', u_join = '".$_POST['join']."' WHERE u_id = '".USER_ID."'";
                     mysql_query($sql, CONNECT);
                     return $this->EditUserPic();
                  }
                  else
                  {
                     define("LIST_ERROR", LANG_IMAGE_LARGE);
                     return $this->GetNewPage(TEMP_ERROR_LIST);
                  }      
               }
               else
               {
                  define("LIST_ERROR", LANG_IMAGE_WRONG_TYPE);
                  return $this->GetNewPage(TEMP_ERROR_LIST);
               }
            }
            else
            {
               $sql = "UPDATE ".MYSQL_PREFIX."_users SET u_pic = '".$_POST['avtar_url']."', u_join = '".$_POST['join']."' WHERE u_id = '".USER_ID."'";
               mysql_query($sql, CONNECT);
               return $this->EditUserPic();
            }
         }
      }
   }



upload.tpl

Code:

<table width="98%" align="center" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD width="100%">


<form action="preferences.php?page=changepic" method="post">

<table align="center" border="0" width="98%">
   <tr>
      <td align="center"><b>AVATAR CENTRALEN</b><hr></td>
   </tr>
</table>

<table align="center" border="0" width="98%">
   <tr>
      <td align="center"><a href="{USER_PHOTO}" target="Resource Window"><img src="{USER_PHOTO}" border="0"></a><hr></td>
   </tr>
</table>

<table align="center" border="0" width="98%">
   <tr>
      <td align="center"><b>V?LJ AVATAR</b><br><br><select name="avatar_gallery">{AVATAR_LIST}</select><br><br><input type="submit" value="Uppdatera" name="submit"></td>
   </tr>
</table>
</form>


<form action="preferences.php?page=changepic" method="post" enctype="multipart/form-data">
<table align="center" border="0" width="98%">
   <tr>
      <td align="center"><hr><b>LADDA UPP NY AVATAR</b><br><br><input type="file" size="30" name="avtar_upload"></td>
   </tr>
</table>

<table align="center" border="0" width="98%">
   <tr>
      <td align="center"><hr><input type="submit" name="submit" value="Uppdatera">
      <input class="inputform" type='hidden' name='join' value="{X_JOIN}" size='25' maxlength='20'>
      </td>
   </tr>
</table>

</form>


</TD>
</TR>
</table>



Theme.php

Code:

class Theme
{
   var $row;
   
   function variable_buffer($string, $var="")
   {

      // only call variables if asked for

      if (!defined($string[1]))
      {
         switch($string[1])
         {
            case 'USER_PHOTO':
               define("USER_PHOTO", $this->GetUserPic(USER_ID));
            break;
            case 'CHATROOM_LOG':
               define("CHATROOM_LOG", $this->GetChatLog());
            break;
         }
      }
   
      if (strstr($string[1], "row"))
      {   
         $v = '$this->'.$string[1];
         eval("\$var=$v;");
      }
      else
      {
         $var = @constant($string[1]);
      }
        return $var;
   }

   function GetNewPage($temp)
   {
      $tpl = file_get_contents(STYLE_DIR."/".$temp);
      $tpl = preg_replace_callback("#\{([a-z0-9\-_\[\]]*?)\}#is", array($this, 'variable_buffer'), $tpl);
      return $tpl;
   }

   function GetTheme()
   {         
      $sql = "SELECT s_dir FROM ".MYSQL_PREFIX."_style WHERE s_used = '1'";
      $query = mysql_query($sql, CONNECT);

      if ($row = mysql_fetch_array($query))
      {
         define ("STYLE_NAME", "$row[0]");
         define ("STYLE_DIR", "theme/$row[0]");
      }
      else
      {
         define ("STYLE_NAME", "default");
         define ("STYLE_DIR", "theme/default");
      }

      require_once("./".STYLE_DIR."/master.php");
      return;
   }


Sorry for the long codes... this is my original... i am trying to nest your code in to mine... but my integration becomes screwed... dont know why!

im am going to copy my version of the integration soon ... im checking for more errors now...

Merry Christmas and a Happy New Year amnuts! and thanks 4 being there 4 your fans...

Laughing
Back to top
View user's profile Send private message Send e-mail
albastorm
Newbie


Joined: 09 Nov 2004
Posts: 6

PostPosted: Sun Mar 06, 2005 5:13 pm    Post subject: Upload problem! Reply with quote

Hi!

I tried your upload-code, but when I upload photos (example: mypic.jpg) something is wrong because when pics have been uploaded the name changes to mypicjpg and I can?t open and crop image.
How can I set the dot (.) between mypic(.) jpg?

Sorry about this "stupid question" but I?m a php-newbie Confused
Back to top
View user's profile Send private message
albastorm
Newbie


Joined: 09 Nov 2004
Posts: 6

PostPosted: Mon Mar 07, 2005 1:03 pm    Post subject: Reply with quote

Hi again, I solved one problem, now upload works fine (files are... OK image.jpg)

But when images have been uploaded I can?t see it....when i want to CROP it, or save it.

Any idea...what?s wrong?

PHP:
<?php
    
require_once('config.php'); 

    
ob_start(); 
    if (!isset(
$_FILES) || empty($_FILES)) { 
        echo 
'<form action="" method="post" enctype="multipart/form-data">'
        echo 
'Select your image type to upload<br />'
        echo 
'<input type="file" name="image"> <input type="submit" value="upload"><br />'
        echo 
'</form>'
    } else { 
        
$filename $_FILES['image']['name']; 
        if (
move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR $filename)) { 
            
header("Location: crop.php?file=$filename"); 
        } else { 
            echo 
'There was an error uploading that file.'
            
print_r($_FILES); 
        } 
    } 
    
ob_end_flush(); 

?>
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Mon Mar 07, 2005 2:13 pm    Post subject: Reply with quote

Hi there,

Well done for getting the upload sorted out! As for your next problem, there's not a lot I can tell from that code. Unless you're getting a 'cannot resend header' error and it's not loading up crop.php, then I'd need to know what you're trying to do in the crop.php file. If your problem IS the headers, then either move the config file call to after the ob_start(), or make sure the config file has no white space outside of the php tags.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
albastorm
Newbie


Joined: 09 Nov 2004
Posts: 6

PostPosted: Mon Mar 07, 2005 5:08 pm    Post subject: Upload and crop Reply with quote

Back to top
View user's profile Send private message
albastorm
Newbie


Joined: 09 Nov 2004
Posts: 6

PostPosted: Tue May 31, 2005 4:39 am    Post subject: Help Reply with quote

Hi, I?ll try to fix this again.

It seems that?s wrong path somewhere, but I don?t know where Sad

You can see what?s happen on this link: http://217.198.148.34/php-crop/upload3.php
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue May 31, 2005 8:23 am    Post subject: Reply with quote

I think this is because of the path you're using in the loadInterface call. The image src ends up being something like:

src="D:\albastorm\php-crop/uploaded/acm1.jpg"

but what you really want it to be is something like:

src="/php-crop/uploaded/acm1.jpg"

So make sure you apply this fix:

http://php.amnuts.com/forums/viewtopic.php?t=129#577

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
johanlundin88
Newbie


Joined: 09 Jul 2006
Posts: 2

PostPosted: Mon Jul 10, 2006 3:20 pm    Post subject: Reply with quote

Iíve used the files in this topic for getting the upload function to work, but the cropped images still arenít saved at my server.

This is the link that appears in the address field when Iím cropping the image:
http://www.homepage.se/crop/crop/crop.php?file=/customers/homepage.se/homepage.se/httpd.www/crop/crop/uploaded/hund.JPG&sx=2&sy=170&ex=82&ey=277

Here are my files:

config.php I don't think I get what I should define in this file.
Code:

<?php

    define('BASE_DIR',   dirname(__FILE__) . '/'); 
    define('UPLOAD_DIR', BASE_DIR . 'uploaded/'); 
    define('CROP_DIR',   BASE_DIR . 'cropped/'); 

?>


upload.php
Code:

<?php
    require_once('config.php'); 

    ob_start(); 
    if (!isset($_FILES) || empty($_FILES)) { 
        echo '<form action="" method="post" enctype="multipart/form-data">'; 
        echo 'Select your image type to upload<br />'; 
        echo '<input type="file" name="image"> <input type="submit" value="upload"><br />'; 
        echo '</form>'; 
    } else { 
        $filename = $_FILES['image']['name']; 
        if (move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR . $filename)) { 
            header("Location: crop.php?file=$filename"); 
        } else { 
            echo 'There was an error uploading that file.'; 
            print_r($_FILES); 
        } 
    } 
    ob_end_flush(); 

?>


crop.php
Code:

<?php

    require_once('config.php'); 
    require_once('class.cropinterface.php'); 
    $ci =& new cropInterface(); 
     
    if (isset($_GET['file']) && isset($_GET['sx'])) 
    { 
        $ci->loadImage($_GET['file']); 
        $ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']); 
        $ci->saveImage(CROP_DIR . $_GET['file']); 
        header('Content-type: image/jpeg'); 
        $ci->showImage('jpg', 90); 
        exit; 
    } 

?>
<html> 

<head> 
    <style type="text/css"> 
        body, td, p 
        { 
            font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
            font-size: 10px; 
        } 
         
        #submit 
        { 
            font-size: 10px; 
            font-family: "MS Sans Serif", Geneva, sans-serif; 
            height: 23px; 
            background-color: #D4D0C8; 
            border: 0px; 
            padding: 3px,5px,3px,5px; 
            width: 100%; 
        } 
    </style> 
</head> 
<body> 

<?php

        $ci->loadInterface(UPLOAD_DIR . $_GET['file']); 
        $ci->loadJavaScript(); 
?>

</body> 
</html>?>
Back to top
View user's profile Send private message
johanlundin88
Newbie


Joined: 09 Jul 2006
Posts: 2

PostPosted: Mon Jul 17, 2006 9:55 am    Post subject: Reply with quote

Should I define something more then the names of the two folders in the config.php file?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Crop Canvas All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group