Current location: Hot Scripts Forums » Programming Languages » PHP » php file upload image resizing


php file upload image resizing

Reply
  #1 (permalink)  
Old 03-06-10, 10:27 AM
ben_johnson1991 ben_johnson1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Posts: 11
Thanks: 4
Thanked 0 Times in 0 Posts
php file upload image resizing

Hey,
I am trying to resize an image before it uploads to my server.
I have never tried this before, but i'm giving it a shot.

my current script is basically
PHP Code:

$photo1 =      $photo1 $_FILES['photo1']['tmp_name'];


Then this to validate

if (!$_FILES['photo1']['tmp_name']) { 
        
                        
$photo1Error '* You Must Submit Atleast <strong>One Image</strong> <br />';
                        
$errorStat1 '1';
                        
$photoStat1 '0';

        } else {
            
            list(
$width,$height)=getimagesize($photo1);

                    
$newwidth=268;
                    
$newheight=($height/$width)*$newwidth;
                    
$tmp=imagecreatetruecolor($newwidth,$newheight);
                                    
                    
imagecopyresampled($tmp,$photo1,0,0,0,0,$newwidth,$newheight,
                     
$width,$height);
                    
                    
$photo1 $_FILES['file']['tmp_name'];
            

            
$maxfilesize 524288// 524288 bytes equals 0.5MB
            
if($_FILES['photo1']['size'] > $maxfilesize ) { 

                        
$photo1Error '* <strong>Image1</strong> was too large (0.5MB max) <br />';
                        
$errorStat1 '1';
                        
$photoStat1 '0';
                        
unlink($_FILES['photo1']['tmp_name']); 

            } else if (!
preg_match("/\.(gif|jpg|jpeg|png)$/i"$_FILES['photo1']['name'] ) ) {

                        
$photo1Error '* <strong>Image1</strong> was not one of the accepted formats (.jpg/.jpeg/.png/.gif) <br />';
                        
$errorStat '1';
                        
$photoStat1 '0';
                        
unlink($_FILES['photo1']['tmp_name']); 
            }
            
$photoStat1 '1';
        } 


and 
finally 

     
if($photoStat1 == 1){
        
$newname "image01.jpg";
        
$place_file move_uploaded_file$_FILES['photo1']['tmp_name'], "classifieds/$id/".$newname);
     } 
to upload.

When I submited this, i recieved te following error
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/bensboxc/public_html/volkslove/regclassified.php on line 143

line 143 is '''' $width,$height);''''



any suggestions where im going wrong, thanks!

Last edited by wirehopper; 03-06-10 at 11:12 AM. Reason: PHP tags
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 03-06-10, 11:20 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
This uses ImageMagick (http://www.imagemagick.org) on the command line to do the resize.

HTML Code:
<form enctype="multipart/form-data" method="post">
<br />
<label>Image file<input type="file" name="image" id="image" /></label>
<input type="submit" value="Go" />
<br /><br />
</form>
PHP Code:

if (isset($_FILES['image']))

{
$v=$_FILES['image'];
if (
$v['tmp_name']!=='')
{
        
$parse=pathinfo($v['name']);
        if (isset(
$parse['extension']))
        {
                
$tmp_image='tmp/image.gif';
                `
rm $tmp_image`;
                
system ('convert '.escapeshellarg($v['tmp_name']).' -thumbnail 200 '.escapeshellarg($tmp_image));
        }
        else
        {
                echo 
'No extension';
                exit();
        }

Linux - not Windows.
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 03-07-10, 11:37 AM
ben_johnson1991 ben_johnson1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Posts: 11
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by wirehopper View Post
This uses ImageMagick (http://www.imagemagick.org) on the command line to do the resize.

HTML Code:
<form enctype="multipart/form-data" method="post">
<br />
<label>Image file<input type="file" name="image" id="image" /></label>
<input type="submit" value="Go" />
<br /><br />
</form>
PHP Code:

if (isset($_FILES['image']))

{
$v=$_FILES['image'];
if (
$v['tmp_name']!=='')
{
        
$parse=pathinfo($v['name']);
        if (isset(
$parse['extension']))
        {
                
$tmp_image='tmp/image.gif';
                `
rm $tmp_image`;
                
system ('convert '.escapeshellarg($v['tmp_name']).' -thumbnail 200 '.escapeshellarg($tmp_image));
        }
        else
        {
                echo 
'No extension';
                exit();
        }

Linux - not Windows.

Could you explain this a bit more, just so I can understand the functions that I need as I can't quite work out where it is the image dimensions are being changed, and how i would implement this into my current script
Cheers for getting back to me though!
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 03-07-10, 01:11 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

system ('convert '.escapeshellarg($v['tmp_name']).' -thumbnail 200 '.escapeshellarg($tmp_image)); 

convert is the command for ImageMagick

The first filename is the input file. In this case, it's the file, as uploaded to the server and present in the server's tmp directory.

-thumbnail 200 means resize the image as a thumbnail, 200px wide, preserve the attribute ratio

Second filename is the destination file.

The example may be more useful to you for the upload process of getting the file input and interpreting it. I never use PHP's image processing libraries, I think the command line is easier.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
error, file, php, resize, upload


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
div css theighost CSS 11 09-14-08 03:30 AM
random banner code with image and flash file sujata_ghosh JavaScript 0 06-30-08 03:43 AM
PHP image upload script in IE7 jac2703 PHP 2 02-21-07 06:38 PM
create php file on the fly and delete when session ends recedo PHP 0 01-06-06 11:28 PM
ASP upload prob minority ASP 1 06-27-05 09:35 AM


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