Upload & resizing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Upload & resizing

Post by therat »

I am using the following script to upload 2 files, one a zip the other an image file. The upload works perfectly but, i am now trying to resize the image uploaded. The image does get resized but the new file is created in the root directory instead of the uploads directory, also the orginal file is also being uploaded which is not needed. How can I change this so the the original file is deleted and the new resized image is created in the uploads directory.

Code: Select all

<?php
  $upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/"; 
    // Get the upload file array 
    // and upload them to a destination 
    // by looping through that array 

	// An array of allowed file types 
    $file_types = array(   
     'image/pjpeg' => 'jpg',
     'image/jpeg' => 'jpg',
     'image/x-png' => 'png',
	 'application/x-zip-compressed' => 'zip'
    );

    // Get the variables from previous form 
    $up_files = $_FILES['filename']; 

    // set the while counter 
    $counter = 0; 
	
	// Start the file upload to server 
    while($counter <= count($up_files))  { 
         
        if($up_files['size'][$counter] > 0) { 
             
            // Read the mime type of current file 
            $filetype = $up_files['type'][$counter]; 
            $filename = $up_files['name'][$counter]; 
            $tempname = $up_files['tmp_name'][$counter]; 
             
            if (!array_key_exists($filetype, $file_types))  { 
                 print "<font color='#990000'>File <b>$filename</b> of type <b>$filetype</b> is not valid for upload!</font><br />"; 
            } else { 
                 
                $upload_file = $upload_dir . $filename; 

                // THIS UPLOADS THE FILE!!!! 
                move_uploaded_file($tempname, $upload_file);

                // Print the uploaded file 
//                print "<font color='#009900'>File <b>$filename</b> uploaded... 
//                new location: <b>$upload_file</b></font><br /><br />";
//				print_r($images);
            } 
        } 
        $counter++; 
    } 

$thumb = $up_files['name'][0];
//echo "<br>Uploaded Filename = $thumb<br>";
//echo "<br>New Path = $upload_dir$thumb";


// Set a few variables 
$image = $upload_dir.$thumb; 
$newimage = "new22.jpg"; 
$image_quality = 80; 
$addborder = 1; 
$max_height = 225; 
$max_width = 300; 

// Main code 
$src_img = ImageCreateFromJpeg($image); 
$orig_x = ImageSX($src_img); 
$orig_y = ImageSY($src_img); 

$new_y = $max_height; 
$new_x = $orig_x/($orig_y/$max_height);   

if ($new_x > $max_width) { 
    $new_x = $max_width; 
    $new_y = $orig_y/($orig_x/$max_width); 
} 

$dst_img = ImageCreateTrueColor($new_x,$new_y); 
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y); 

if ($addborder == 1) { 
    // Add border 
    $black = ImageColorAllocate($dst_img, 0, 0, 0); 
    ImageSetThickness($dst_img, 1); 
    ImageLine($dst_img, 0, 0, $new_x, 0, $black); 
    ImageLine($dst_img, 0, 0, 0, $new_y, $black); 
    ImageLine($dst_img, $new_x-1, 0, $new_x-1, $new_y, $black); 
    ImageLine($dst_img, 0, $new_y-1, $new_x, $new_y-1, $black); 
} 
     
ImageJpeg($dst_img, $newimage, $image_quality); 
ImageDestroy($src_img); 
ImageDestroy($dst_img); 

?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Use [php_man]copy[/php_man], e.g.

Code: Select all

<?php
copy($image,"../$location/$imagename");
?>
To delete the original image, use [php_man]unlink[/php_man].
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Works prefectly thanks. I now have another small problem. I need to rename the image instead of calling it "new22.jpg" all the time. I have changed

Code: Select all

$newimage = "new22.jpg";
to

Code: Select all

$newimage = substr(sha1(rand(10, time())), 0,  . '.' . $file_types[$filetype];
It does the rename correctly but changes the extension of the file to zip/rar instead of jpg. What needs to change to get the thumb to use the correct extension.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

That should be code problem, rather than a PHP related issue. Could you post the (relevant) code?
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Its the same code as in the first post apart from the line renaming the file. That is the only part I have altered.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Is the value of $filetype entirely identical with a key in your array $file_types?

var_dump those variables to double-check the values.
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

The problem appears to happen when I upload files with different extensions. If I upload t jpg's then everythign is fine but, if I upload a jpg and a zip/rar the thumb takes the zip extension, but the image still shows on the page.
I am trying to upload 2 files, the first has to be a jpg/png/gif, which has to be resized if to big, and the second has to be a zip/rar archive file. The script I have does work but has its problems. Is there an easier/better way of acheiving what I need. Thansk for the help.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

It's because the renaming part is outside of the loop. So the loop executes and this leaves $newimage equal to the last filetype uploaded. If you want to upload multiple files, the renaming should be inside the upload loop.
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

I've now got the renaming working correctly. I have added this after the upload line

Code: Select all

$new_file = substr(sha1(rand(10, time())), 0,  . '.' . $file_types[$filetype]; 
	rename($upload_file, $upload_dir . $new_file);
The image file is uploaded last so I can pass the $new_file variable straight to the resize script. The second line of the resize part is

Code: Select all

$newimage = "new22.jpg";
How do I change this to keep the orginal filename, the one passed by $new_file. I tried changing it to

Code: Select all

$newimage = $new_file;
but it did not work. Thanks for all the info.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Inside of the loop you have the line:

Code: Select all

<?php
$filename = $up_files['name'][$counter];
?>
That is the original filename. You will only be able to use inside of the loop unless you do something, like store all of the filenames in an array.
Post Reply