そのため、画像が 35kb を超える場合は圧縮するように画像アップローダーを調整しようとしていますが、うまくいかないようです。常に「ファイル サイズが大きすぎる」というエラーが表示されるので、正しく実行しているとは思わないでください。大きい! 最大は 35kb です。さらに圧縮するか、別の画像を見つけてください。」
これが私のコードです:
function article_top_image($article_id)
{
global $db, $config;
if (isset($_FILES['new_image']) && $_FILES['new_image']['error'] == 0)
{
if (!@fopen($_FILES['new_image']['tmp_name'], 'r'))
{
$this->error_message = "Could not find image, did you select one to upload?";
return false;
}
else
{
// check the dimensions
list($width, $height, $type, $attr) = getimagesize($_FILES['new_image']['tmp_name']);
// check if its too big
if ($_FILES['new_image']['size'] > 35900)
{
// compress it to see if we can make it smaller
imagejpeg($_FILES['new_image']['tmp_name'], $_FILES['new_image']['tmp_name'], 50);
// check again
if ($_FILES['new_image']['size'] > 35900)
{
$this->error_message = 'File size too big! The max is 35kb, try to use some more compression on it, or find another image.';
return false;
}
}
else if ($width > $config['article_image_max_width'] || $height > $config['article_image_max_height'])
{
$this->error_message = 'Too big!';
return false;
}
// this will make sure it is an image file, if it cant get an image size then its not an image
else if (!getimagesize($_FILES['new_image']['tmp_name']))
{
$this->error_message = 'Not an image!';
return false;
}
}
// see if they currently have an avatar set
$db->sqlquery("SELECT `article_top_image` FROM `articles` WHERE `article_id` = ?", array($article_id));
$image = $db->fetch();
// give the image a random file name
$imagename = rand() . 'id' . $article_id . '.jpg';
// the actual image
$source = $_FILES['new_image']['tmp_name'];
// where to upload to
$target = $_SERVER['DOCUMENT_ROOT'] . "/uploads/articles/topimages/" . $imagename;
if (move_uploaded_file($source, $target))
{
// remove old avatar
if ($image['article_top_image'] == 1)
{
unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/articles/topimages/' . $image['article_top_image_filename']);
}
$db->sqlquery("UPDATE `articles` SET `article_top_image` = 1, `article_top_image_filename` = ? WHERE `article_id` = ?", array($imagename, $article_id));
return true;
}
else
{
$this->error_message = 'Could not upload file!';
return false;
}
return true;
}
}