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 and crop an image

 
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
filt
Newbie


Joined: 19 Sep 2004
Posts: 1

PostPosted: Sun Sep 19, 2004 7:23 pm    Post subject: Upload and crop an image Reply with quote

Hi, i have a "Rate my image" website.

I have a php form where users upload a picture of themself, and get it rated. Too bad my script doesnt allow croping and resizing.

I suck at php, so i wonder if anyone has an example of how to make this come true:

Upload a picture
Selecting an area, with the size A*B
Seting A to 640 and B to 480
Saving the image.


Any practical examples?

Best regards,
Fabian Mossberg
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 Sep 20, 2004 3:24 am    Post subject: Reply with quote

Hi, and welcome to the board!

Hrere is a script I'm playing aorudn with to do something very similar. I'm not going to tidy it up for this example, but just give it to you as-is. Where I use a singleton class to create instances of my objects, you'd just use the '$obj =& new Class()' way of doing things. Also, ignore the colourize call - it's just something I was playing with.

Don't expect this to work out-of-the-box for you!

PHP:
<?php <html>
<
head>
    <
style type="text/css">
        
bodytdp
        
{
            
font-familyTahomaVerdanaArialHelveticasans-serif;
            
font-size10px;
        }
        
        
#submit
        
{
            
font-size10px;
            
font-family"MS Sans Serif"Genevasans-serif;
            
height23px;
            
background-color#D4D0C8;
            
border0px;
            
padding3px,5px,3px,5px;
            
width100%;
        }
    </
style>
</
head>
<
body>
<?
php

    define
('BASE_DIR',     $_SERVER['DOCUMENT_ROOT'] . '/');
    
define('CORE_DIR',     BASE_DIR 'colours/wcmimg/');
    
define('CLASS_DIR',    CORE_DIR);
    
define('IMAGE_DIR',    CORE_DIR);
    
define('IMAGE_WIDTH',  250);
    
define('IMAGE_THUMBNAIL_SQUARE'80);
    
define('COLOUR''#426173');

    require_once(
CLASS_DIR 'class_singleton.php');
    require_once(
CLASS_DIR 'class.image.php');

    if (!isset(
$_GET['sx'])) {
        if (!isset(
$_FILES) || empty($_FILES)) {
            echo 
'<form action="" method="post" enctype="multipart/form-data">';
            echo 
'Select your image type to upload (jpg files only)<br />';
            echo 
'<input type="file" name="image"> <input type="submit" value="upload"><br />';
            echo 
'</form>';
        } else {
            
$fileHandle /*uniqid('img_') .*/ 'original.jpg';
            if (
move_uploaded_file($_FILES['image']['tmp_name'], IMAGE_DIR $fileHandle)) { 
                
$ds =& Singleton::getInstance('dropShadow'true);
                
$ds->loadImage(IMAGE_DIR $fileHandle);
                
$sizes getimagesize(IMAGE_DIR $fileHandle);
                if (
$sizes[0] > IMAGE_WIDTH) {
                    
$ds->resizeToSize(IMAGE_WIDTH0);
                    
$ds->saveFinal(IMAGE_DIR 'resized.png''png'100);
                } else {
                    
$ds->saveOrig(IMAGE_DIR 'resized.png''png'100);
                }
                
chmod(IMAGE_DIR 'resized.png'0777);
                
$sizes getimagesize(IMAGE_DIR 'resized.png');
                
$cropsize = (min($sizes[0], $sizes[1])) / 2;
                
$ci =& Singleton::getInstance('cropInterface');
                
$ci->setCropMinSize(IMAGE_THUMBNAIL_SQUAREIMAGE_THUMBNAIL_SQUARE);
                
$ci->setCropDefaultSize(max($cropsizeIMAGE_THUMBNAIL_SQUARE), max($cropsizeIMAGE_THUMBNAIL_SQUARE));
                
$ci->setResizing(true);
                
$ci->fixResizeType(1);
                
$ci->loadInterface(IMAGE_DIR 'resized.png');
                
$ci->loadJavaScript();
            } else { 
                echo 
'There was an error uploading that file.';
                
print_r($_FILES);
            }
        }
    } else {
        
$ci =& Singleton::getInstance('cropInterface');
        
$ci->loadImage($_GET['file']);
        
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
        
$ci->saveImage(IMAGE_DIR 'cropped.png'100);
        if ((
$_GET['ex'] - $_GET['sx']) > IMAGE_THUMBNAIL_SQUARE) {
            
$ds =& Singleton::getInstance('dropShadow');
            
$ds->loadImage(IMAGE_DIR 'cropped.png');
            
$ds->resizeToSize(IMAGE_THUMBNAIL_SQUARE0);
            
$ds->saveFinal(IMAGE_DIR 'cropped.png''png'100);
        }
        
$ci->flushImages();
        
$ci->loadImage(IMAGE_DIR 'cropped.png');
        
$ci->colorize(COLOUR);
        
$ci->saveImage(IMAGE_DIR 'colourised.png'90);
        
        
        echo 
'Original<br /><img src="original.jpg"><br /><br />';
        echo 
'Resized<br /><img src="resized.png"><br /><br />';
        echo 
'Cropped<br /><img src="cropped.png"><br /><br />';
        echo 
'Colourised<br /><img src="colourised.png"><br /><br />';
    }

?>
</body>
</html> ?>


You'll notice that I resize the image to a set size first, and then crop if needs be. But it shows how easy it can be to use an upload form, resize and crop all in one simple file.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
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