Avatar

havent gotten this to work yet, cant figure out why, any time i upload something, it keeps telling me the file type is wrong even though its not... plus its a bit long...
basicly what this script does it
- upload a photo
- resize it to a width of 1020 and create a thumnbnail with a width of 150
-i wanted the actuall names of the files to stay the same, instead of some weird numbers and letters, so it checks if a photo with that name has already been uploaded, changing the name
-uploads both images to their directories, and adds theyre name and location to my database...

i used this tutorial to make it
http://www.phpit.net/article/image-manipulation-php-gd-part1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
// check to see if image has been uploaded
if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
}    else {
    $uploaded_image = $_FILES['image']['tmp_name'];
}    


//function to view any type of image
function open_image ($file) {
        // Get extension
        $extension = strrchr($file, '.');
        $extension = strtolower($extension);

        switch($extension) {
                case '.jpg':
                 $im = @imagecreatefromjpeg($file);
                        $image_type = ".jpg";
                        break;
                case '.jpeg':
                        $im = @imagecreatefromjpeg($file);
                        $image_type = ".jpeg";
                        break;
                case '.gif':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".gif";
                        break;                        
                case '.png':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".png";
                        break;

                default:
                        $im = false;
                        break;
        }
        return $im;
    }
        
        

        
        
// Load image
$image = open_image($uploaded_image);
if ($image === false) { die ('that file type is not allowed'); } // die if theres no image


// get original image width and height
$width = imagesx($image);
$height = imagesy($image);


// resized image width
$resize_width = 1020;
$resize_height = $height * ($resize_width/$width);

// thumbnail image width
$thumb_width = 150;
$thumb_height = $height * ($thumb_width/$width);

// Resample resized image
$image_resized = imagecreatetruecolor($resize_width, $resize_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height);

// Resample thumbnail image
$image_resized = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($image_thumbnail, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


///////////////////////////////// check image name and rename it ///////////////////////////////

  $uploaded_img_name = $_FILES['image']['name'];
  
  $result_check = mysql_query("SELECT photo_file_name FROM album WHERE photo_file_name='$uploaded_img_name'") or die(mysql_error());

while($check_data = mysql_fetch_array( $result_check )) 
{        
    $check_name= $check_data['user_id']; 
}

if (empty($check_name)) {
    $uploaded_img_name = $uploaded_img_name;
} else {
    $uploaded_img_name = str_replace($image_type, "", $uploaded_img_name);
    $uploaded_img_name = $uploaded_img_name ."1". $image_type;
    
}
  
  
/////////////////////////////////////// insert to directory ///////////////////////////////

  $filedir = '/upload/user_album/'; // the directory for the original image
  $thumbdir = '/upload/user_album/thumb/'; // the directory for the thumbnail image
  $prod_img = $filedir.$uploaded_img_name;
  $prod_img_thumb = $thumbdir.$uploaded_img_name;
         



    if( !empty($HTTP_POST_FILES['song_image']['tmp_name']) )
        {
        $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';

        if ( @$ini_val('open_basedir') != '' )
        {
            if ( @phpversion() < '4.0.3' )
            {
            message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file<br /><br />Please contact your server admin', '', __LINE__, __FILE__);
            }

            $move_file = 'move_uploaded_file';
        }
        else
        {
            $move_file = 'copy';
        }
}

        
        // moved resized image into directory
        $move_file($uploaded_image, $prod_img);
        @chmod($prod_img, 0777);
        
        // moved thumb image into directory
        $move_file($uploaded_image, $prod_img_thumb);
        @chmod($prod_img_thumb, 0777);
        
        }
        
/////////////////////////////////////// insert to database///////////////////////////////
        
        mysql_query("INSERT INTO album ('photo_file_name') VALUES('$uploaded_img_name') ") or die(mysql_error());

Refactorings

No refactoring yet !

A2c8fecfd1fb707dd0a8f292ade77e1e

typefreak

October 26, 2007, October 26, 2007 13:21, permalink

No rating. Login to rate!

The wrong filetype is easy to understand: You're using $_FILES['image']['tmp_name'] to check the extension, while you should use $_FILES['image']['name'] For that purpose.
http://nl2.php.net/manual/nl/features.file-upload.php

A2c8fecfd1fb707dd0a8f292ade77e1e

typefreak

October 26, 2007, October 26, 2007 13:40, permalink

No rating. Login to rate!

A bit of rewriting:

Comment behinde lines is telling what i've changed

Why are you using the database? Is there anything else using it? Just storing the name is useless, you can also use 'file_exists' or simmilar functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// check to see if image has been uploaded

if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
}    else {
    $uploaded_image = $_FILES['image']['tmp_name'];
    $uploaded_image_name = $_FILES['image']['name']; // Added name var
}    


//function to view any type of image
function open_image ($file, $name) { // Request another paramater, containing original filename
        // Get extension
        $extension = strtolower(strrchr($name, '.')); // 2 lines to 1, using the new parameter

        switch($extension) {
                case '.jpg':
                case '.jpeg': // .jpg and .jpeg can be treated the same
                        $im = @imagecreatefromjpeg($file);
                        $image_type = ".jpeg";
                        break;
                case '.gif':
                        $im = @imagecreatefromgif($file);
                        $image_type = ".gif";
                        break;                        
                case '.png':
                        $im = @imagecreatefrompng($file); // Create from png instead of gif (whe have png, don't we?)
                        $image_type = ".png";
                        break;
                default:
                        $im = false;
                        break;
        }
        return $im;
    }
        
        

        
        
// Load image
$image = open_image($uploaded_image, $uploaded_image_name); // Also pass second parameter
if ($image === false) { die ('that file type is not allowed'); } // die if theres no image


// get original image width and height
$width = imagesx($image);
$height = imagesy($image);

// resized image width
$resize_width = 1020;
$resize_height = intval($height * ($resize_width/$width)); // Make sure $resize_height is an int

// thumbnail image width
$thumb_width = 150;
$thumb_height = intval($height * ($thumb_width/$width)); // Make sure $thumb_height is an int

// Resample resized image
$image_resized = imagecreatetruecolor($resize_width, $resize_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height);

// Resample thumbnail image
$image_resized = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($image_thumbnail, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

///////////////////////////////// check image name and rename it ///////////////////////////////
$db_safe_name = mysql_real_escape_string($uploaded_image_name); // Better safe then sorry
  $result_check = mysql_query("SELECT photo_file_name FROM album WHERE photo_file_name='{$db_safe_name}'") or die(mysql_error()); // You sure you want to show that error?

$number = mysql_num_rows($result_check); // Why use a while if you can see if there are results directly?
if ( $number > 0 ) { // Perhaps checking again after renaming, the new name might already by in use.
    $ext = explode($uploaded_image_name);
    $ext = array_pop($ext);
    $uploaded_image_name = str_replace("." . $ext, "", $uploaded_image_name);
    $uploaded_image_name = $uploaded_image_name . "1.". $ext;
}
  
  
/////////////////////////////////////// insert to directory ///////////////////////////////

  $filedir = '/upload/user_album/'; // the directory for the original image
  $thumbdir = '/upload/user_album/thumb/'; // the directory for the thumbnail image
  $prod_img = $filedir.$uploaded_image_name;
  $prod_img_thumb = $thumbdir.$uploaded_image_name;
         



    if( !empty($HTTP_POST_FILES['song_image']['tmp_name']) )
        {
        $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';

        if ( @$ini_val('open_basedir') != '' )
        {
            if ( @phpversion() < '4.0.3' )
            {
            message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file<br /><br />Please contact your server admin', '', __LINE__, __FILE__);
            }

            $move_file = 'move_uploaded_file';
        }
        else
        {
            $move_file = 'copy';
        }
}

        
        // moved resized image into directory
        $move_file($uploaded_image, $prod_img);
        @chmod($prod_img, 0777);
        
        // moved thumb image into directory
        $move_file($uploaded_image, $prod_img_thumb);
        @chmod($prod_img_thumb, 0777);
        
        }
        
/////////////////////////////////////// insert to database///////////////////////////////
        $db_safe_name = mysql_real_escape_string($uploaded_image_name);
        mysql_query("INSERT INTO album ('photo_file_name') VALUES('$db_safe_name') ") or die(mysql_error());
Avatar

blank714.myopenid.com

October 29, 2007, October 29, 2007 18:43, permalink

No rating. Login to rate!

hey, sorry it took me so long to respond, and thanks a lot for fixing it

im using a database cuz im going to be adding a lot more information, not just the file name (user_id, time it was added, category, etc)

but yeah, thanks a lot, ill test it out when i get homes

4a953ddd872a2ee99a62c92721a2104a

website reviews

March 8, 2008, March 08, 2008 20:25, permalink

No rating. Login to rate!

Thanks guys, i needed some script like this for my website...

3abddbe168654655172375bdfb8700c7

Gugl Gastronomiebedarf

July 8, 2008, July 08, 2008 11:59, permalink

No rating. Login to rate!

Thanks, nice script!

049f8e02ca9c3b46d81be7e14a32a396

Gasthof Grabmayer

August 20, 2008, August 20, 2008 13:37, permalink

No rating. Login to rate!

Thanks for this script.

Your refactoring





Format Copy from initial code

or Cancel