如果太大,压缩上传的图像

时间:2013-04-07 10:01:22

标签: php compression

所以我试图调整我的图像上传器,这样如果图像超过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;
    }

}

2 个答案:

答案 0 :(得分:1)

您必须重复使用filesize。可能需要先调用clearstatcache

filesize($_FILES['new_image']['tmp_name'])

答案 1 :(得分:0)

我也在努力压缩大图像。我使用以下代码。

function image_quality()
{
    $name = ''; $type = ''; $size = ''; $error = '';
    function compress_image($source_url, $destination_url, $quality)
    {
        $info = getimagesize($source_url);

            if ($info['mime'] == 'image/jpeg')
                    $image = imagecreatefromjpeg($source_url);

            elseif ($info['mime'] == 'image/gif')
                    $image = imagecreatefromgif($source_url);

            elseif ($info['mime'] == 'image/png')
                    $image = imagecreatefrompng($source_url);

            imagejpeg($image, $destination_url, $quality);
        return $destination_url;
    }

    $dir   = 'images_high';
    $files = scandir($dir);
    foreach($files as $image)
    {   
        $size=filesize("images_high/$image");
        if ($size <=50000)
                rename("images_high/$image","user_images/$image");

        $url = "user_images/$image";
        $filename = compress_image("images_high/$image", $url, 20);
        $buffer = file_get_contents($url);

        unlink("images_high/$image");
    }
}

我希望它会对你有所帮助。