/    Sign up×
Community /Pin to ProfileBookmark

Can someone check this file uploading function for me?

[code=php]/*** Generates thumbnails for posts ***/
function generate_image_thumbnail( $source_image_path, $thumbnail_image_path )
{
list( $source_image_width, $source_image_height, $source_image_type ) = getimagesize( $source_image_path );

switch ( $source_image_type )
{
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif( $source_image_path );
break;

case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg( $source_image_path );
break;

case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng( $source_image_path );
break;
}

if ( $source_gd_image === false )
{
return false;
}

$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;

$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $thumbnail_image_width / $thumbnail_image_height;

if ( $source_image_height > $source_image_width )
{
$src_h = $source_image_width;
$src_w = $source_image_width;
$x = 0;
$y = ( $source_image_height – $source_image_width ) / 2;
}
else
{
$src_h = $source_image_height;
$src_w = $source_image_height;
$y = 0;
$x = ( $source_image_width – $source_image_height ) / 2;
}

$thumbnail_gd_image = imagecreatetruecolor( 80, 80 );

$colourBlack = imagecolorallocate($thumbnail_gd_image, 0, 0, 0);

imagecolortransparent($thumbnail_gd_image, $colourBlack);

imagecopyresampled( $thumbnail_gd_image, $source_gd_image, 0, 0, $x, $y, 80, 80, $src_w, $src_h );

imagejpeg( $thumbnail_gd_image, $thumbnail_image_path, 75 );

imagedestroy( $source_gd_image );

imagedestroy( $thumbnail_gd_image );

return true;
}

//——————————–
// FILE PROCESSING FUNCTION
//——————————–

function process_image_upload( $field )
{
$temp_image_path = $_FILES[ $field ][ ‘tmp_name’ ];
$temp_image_name = $_FILES[ $field ][ ‘name’ ];

$num = 2;
[B]while (1) {
str_replace(‘ ‘,’_’,$temp_image_name);
if (file_exists(“../wp-content/uploads/wp-post-image/” . $temp_image_name)) {
if ($num == 2) {
preg_match(‘/.[a-zA-Z]{3,4}$/’,$temp_image_name,$match);
$extension = $match[0];
}
$temp_image_name = preg_replace(‘/.[a-zA-Z]{3,4}$/’,”,$temp_image_name);
if ($num > 2)
$regex = ‘/’.$num.’$/’;
if ($num > 2)
$temp_image_name = preg_replace($regex,”,$temp_image_name);
$temp_image_name = $temp_image_name.$num;
} else {
if ($num > 2)
$temp_image_name = $temp_image_name.$extension;
break;
}
$num++;
}[/B]

list( , , $temp_image_type ) = getimagesize( $temp_image_path );

if ( $temp_image_type === NULL )
{
return false;
}

switch ( $temp_image_type )
{
case IMAGETYPE_GIF:
break;

case IMAGETYPE_JPEG:
break;

case IMAGETYPE_PNG:
break;

default:
return false;
}

$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;

move_uploaded_file( $temp_image_path, $uploaded_image_path );

$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace( ‘{\.[^\.]+$}’, ‘.jpg’, $temp_image_name );

$result = generate_image_thumbnail( $uploaded_image_path, $thumbnail_image_path );

return $result
? array( $uploaded_image_path, $thumbnail_image_path )
: false;
}[/code]

The bolded part may have flaws. Can someone please check this for me?

Also, how can I delete the image in $uploaded_image_path after this function is called?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@narutodude000authorDec 10.2010 — I found a second flaw. The image filenames aren't unique.
×

Success!

Help @narutodude000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...