Current location: Hot Scripts Forums » Programming Languages » PHP » Save file after image upload and resize


Save file after image upload and resize

Reply
  #1 (permalink)  
Old 08-15-08, 05:01 PM
acctman acctman is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Save file after image upload and resize

I'm using this script below to display the users image after uploading, it also will resize the image too. Everything works fine, but can't figure out how to save the resized image to the /temp/ folder. the steps are 1. upload 2. resize image if needed 3. display image to the user 4. save displayed/resized image to /temp/ folder.

PHP Code:

<?php



    
// Get the session Id passed from Upload. We have to do this to work-around the Flash Player Cookie Bug
    
if (isset($_POST["PHPSESSID"])) {
        
session_id($_POST["PHPSESSID"]);
    } else if (isset(
$_GET["PHPSESSID"])) {
        
session_id($_GET["PHPSESSID"]);
    }

    
session_start();

    
// Check the upload
    
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
        
header("HTTP/1.1 500 Internal Server Error");
        echo 
"invalid upload";
        exit(
0);
    }

    
// Get the image and create a thumbnail
    
$img = @imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
    if (!
$img) {
        
header("HTTP/1.1 500 Internal Server Error");
        echo 
"could not create image handle";
        exit(
0);
    }

    
$width imageSX($img);
    
$height imageSY($img);

    
// Check if image is too small
    
if ($width 160 || $height 120 ) {
        
header("HTTP/1.1 200 Internal Server Error");
        echo 
"Invalid width or height. Your image is way to small.";
        exit(
0);
    }
    
    
// Resize if image is bigger than 640x480        
     
if ($width 640 || $height >= 480 ) {

    
// Build the thumbnail
    
$target_width 640;
    
$target_height 800;
    
$target_ratio $target_width $target_height;

    
$img_ratio $width $height;

    if (
$target_ratio $img_ratio) {
        
$new_height $target_height;
        
$new_width $img_ratio $target_height;
    } else {
        
$new_height $target_width $img_ratio;
        
$new_width $target_width;
    }

    if (
$new_height $target_height) {
        
$new_height $target_height;
    }
    if (
$new_width $target_width) {
        
$new_height $target_width;
    }

    
$new_img ImageCreateTrueColor($new_width$new_height);

    if (!@
imagecopyresampled($new_img$img, ($target_width-$new_width)/0, ($target_height-$new_height)/000$new_width$new_height$width$height)) {
        
header("HTTP/1.0 500 Internal Server Error");
        echo 
"Could not resize image";
        exit(
0);
    }

if (!isset(
$_SESSION["file_info"])) {
        
$_SESSION["file_info"] = array();
    }

    
// Use a output buffering to load the image into a variable
    
ob_start();
    
imagejpeg($new_img);
    
$imagevariable ob_get_contents();
    
ob_end_clean();

    
$file_id md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);

    
$_SESSION["file_info"][$file_id] = $imagevariable;

    echo 
$file_id;    // Return the file id to the script

    // Resize if image is bigger than 640x480
    
} elseif ($width <= 640 and $width >= 160 and $height <= 480 and $height >= 120) {

        if (!isset(
$_SESSION["file_info"])) {
        
$_SESSION["file_info"] = array();
    }

    
// Use a output buffering to load the image into a variable
    
ob_start();
    
imagejpeg($img);
    
$imagevariable ob_get_contents();
    
ob_end_clean();

    
$file_id md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);

    
$_SESSION["file_info"][$file_id] = $imagevariable;

    echo 
$file_id;    // Return the file id to the script


}    
        
    
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-16-08, 07:41 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
The code below uses ImageMagick on the command line to manipulate the image. Change the thumbnail operation to suit your needs.

http://imagemagick.org/

PHP Code:

define ('TMP','/www/tmp/');

define ('TMPURL','http://domain.com/tmp/');

process_image('new_image_colors',$sFileName);

function 
process_image($sImageInput,&$sFileName)
{
        if (isset(
$_FILES[$sImageInput]))
        {
                
$v=$_FILES[$sImageInput];
                if (
$v['tmp_name']!=='')
                {
                        
$parse=pathinfo($v['name']);
                        if (isset(
$parse['extension']))
                        {
                                
$sFileName=$parse['basename'];
                                
system ('convert '.escapeshellarg($v['tmp_name']).' -thumbnail 100 '.TMP.$sImageInput.'.gif');
                                echo 
'<img src="'.TMPURL.$sImageInput.'.gif" /><br />';
                                
$rm='rm '.escapeshellarg($v['tmp_name']); `$rm`;                             
                        }
                }
        }

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-16-08, 11:22 AM
acctman acctman is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
I need to use the existing coding that I have above, I'm pretty sure its just a move/copy or imagejpg function that will save it.

Is anyone able to read the coding and understand whats going on in order to figure out how to save the image?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-17-08, 11:14 AM
algorithmetics algorithmetics is offline
Newbie Coder
 
Join Date: Aug 2008
Location: Pittsburgh, PA
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
imagejpeg() takes more than one argument. If you supply a path as the second argument, it will save the image as a file at the path you specified.

http://us.php.net/imagejpeg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload file resize xavier039 PHP 1 10-20-06 06:03 AM
multiple image upload and resize - pls help ashishsehgal PHP 2 07-21-05 04:12 AM
image resize and upload ascanio PHP 0 06-25-05 07:37 PM
will pay. php script needed. upload, resize, & email jamjammo Script Requests 4 02-29-04 09:30 PM
Upload rename, resize & database insert therat PHP 3 01-16-04 03:42 PM


All times are GMT -5. The time now is 01:58 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.