[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;
}
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?