How can I use this code for just the "avatar" upload?
For avatar upload I now use this solution by thame^, but the picture quality is much better with Koos' solution.
I'd like to use Koos' code in profile.php for the avatar upload, but with the regular avatar naming method and regular img/avatars folder - although it would be a nice improvement if the originals were saved as well, so maybe add an img/avatars/thumb folder?
This was thame^'s code:
if (is_uploaded_file($uploaded_file['tmp_name']))
{
$allowed_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
if (!in_array($uploaded_file['type'], $allowed_types))
message($lang_profile['Bad type']);
// Determine type
$extensions = null;
if ($uploaded_file['type'] == 'image/gif')
$extensions = array('.gif', '.jpg', '.png');
else if ($uploaded_file['type'] == 'image/jpeg' || $uploaded_file['type'] == 'image/pjpeg')
$extensions = array('.jpg', '.gif', '.png');
else
$extensions = array('.png', '.gif', '.jpg');
// Move the file to the avatar directory. We do this before checking the width/height to circumvent open_basedir restrictions.
if (!@move_uploaded_file($uploaded_file['tmp_name'], $pun_config['o_avatars_dir'].'/'.$id.'.tmp'))
message($lang_profile['Move failed'].' <a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>.');
// Now check the width/height
list($width, $height, $type,) = getimagesize($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
if (empty($width) || empty($height) || $width > $pun_config['o_avatars_width'] || $height > $pun_config['o_avatars_height'])
{
// Attempt to resize if GD is installed with support for the uploaded image type, as well as JPG for the output
$check_type = str_replace(array(1, 2, 3), array('IMG_GIF', 'IMG_JPG', 'IMG_PNG'), $type);
if (extension_loaded('gd') && imagetypes() & constant($check_type) && imagetypes() & IMG_JPG)
{
// Load the image for processing
if ($type == 1) $src_img = @imagecreatefromgif($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
elseif ($type == 2) $src_img = @imagecreatefromjpeg($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
elseif ($type == 3) $src_img = @imagecreatefrompng($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
if ($src_img)
{
// Figure out new image dimensions based on the maximum width
$new_w = $pun_config['o_avatars_width'];
$ratio = $height * $new_w;
$new_h = $ratio / $width;
// Do the new dimensions, based on the maximum width, fit the maximum height? If not, recalculate
if ($new_h > $pun_config['o_avatars_height'])
{
$new_h = $pun_config['o_avatars_height'];
$ratio = $width * $new_h;
$new_w = $ratio / $height;
}
// Resize the image
$new_img = imagecreatetruecolor($new_w, $new_h);
imagecopyresized($new_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
// Delete the old image and write the newly resized one
@unlink($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
imagejpeg($new_img,$pun_config['o_avatars_dir'].'/'.$id.'.tmp');
// Set the extension to JPG, since that's what the resized image is now
$extensions[0] = '.jpg';
}
// Something went wrong while attempting to load the image for processing
else
{
@unlink($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
message('An unexpected error occured while attempting to resize the image.');
}
}
// No GD installed or image type not supported; can't resize
else
{
@unlink($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
message($lang_profile['Too wide or high'].' '.$pun_config['o_avatars_width'].'x'.$pun_config['o_avatars_height'].' '.$lang_profile['pixels'].'.');
}
}
else if ($type == 1 && $uploaded_file['type'] != 'image/gif') // Prevent dodgy uploads
{
@unlink($pun_config['o_avatars_dir'].'/'.$id.'.tmp');
message($lang_profile['Bad type']);
}
// Make sure the file isn't too big
if (filesize($pun_config['o_avatars_dir'].'/'.$id.'.tmp') > $pun_config['o_avatars_size'])
message($lang_profile['Too large'].' '.$pun_config['o_avatars_size'].' '.$lang_profile['bytes'].'.');
// Delete any old avatars and put the new one in place
@unlink($pun_config['o_avatars_dir'].'/'.$id.$extensions[0]);
@unlink($pun_config['o_avatars_dir'].'/'.$id.$extensions[1]);
@unlink($pun_config['o_avatars_dir'].'/'.$id.$extensions[2]);
@rename($pun_config['o_avatars_dir'].'/'.$id.'.tmp', $pun_config['o_avatars_dir'].'/'.$id.$extensions[0]);
@chmod($pun_config['o_avatars_dir'].'/'.$id.$extensions[0], 0644);
}
else
message($lang_profile['Unknown failure']);
// Enable use_avatar (seems sane since the user just uploaded an avatar)
$db->query('UPDATE '.$db->prefix.'users SET use_avatar=1 WHERE id='.$id) or error('Unable to update avatar state', __FILE__, __LINE__, $db->error());
redirect('profile.php?section=personality&id='.$id, $lang_profile['Avatar upload redirect']);
}
$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Profile'];
$required_fields = array('req_file' => $lang_profile['File']);
$focus_element = array('upload_avatar', 'req_file');
require PUN_ROOT.'header.php';
?>
<div class="blockform">
<h2><span><?php echo $lang_profile['Upload avatar'] ?></span></h2>
<div class="box">
<form id="upload_avatar" method="post" enctype="multipart/form-data" action="profile.php?action=upload_avatar2&id=<?php echo $id ?>" onsubmit="return process_form(this)">
<div class="inform">
<fieldset>
<legend><?php echo $lang_profile['Upload avatar legend'] ?></legend>
<div class="infldset">
<input type="hidden" name="form_sent" value="1" />
<!--<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $pun_config['o_avatars_size'] ?>" />-->
It does what I need, but the resized pictures are really ugly. Koos' code produces a much nicer result. How can I improve thame^'s code with bits from Koos?
I suspect the problem is in the resize method. thame^ uses $ratio, Koos uses $zoom. How can I use the $zoom method in thame^'s code? I'm not a PHP-coder, so this is hard for me to figure out.