Php 如何使用imagejpeg()确保图像大小小于100kb

Php 如何使用imagejpeg()确保图像大小小于100kb,php,Php,我正在上传产品图片,希望确保所有内容都在100kb以下,所以我引用并写了这篇文章,问题是在某些情况下大小在100kb以上,如何选择一个值,使大小始终在100kb以下 function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg(

我正在上传产品图片,希望确保所有内容都在100kb以下,所以我引用并写了这篇文章,问题是在某些情况下大小在100kb以上,如何选择一个值,使大小始终在100kb以下

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

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

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

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

    imagejpeg($image, $_SERVER['DOCUMENT_ROOT'].'/image processing/'.$destination, $quality);

    
}

$source_img = 'product.jpg';
$destination_img = 'destination.jpg';

compress($source_img, $destination_img, 70);
1MB==1048576字节

如果您想将文件上载限制在<1MB以下!!然后,由于$\u FILES数组将以字节为单位输出,因此您可以执行以下操作。
如果($_文件['name']['size']>1048576){
//您无法上载此文件
}
或者,如果要从浏览器级别对其进行限制,可以将属性添加到窗体中,如下所示:

formmethod=“POST”enctype=“多部分/表单数据”>
当然,任何人都可以轻易地改变第二种选择,而且 不应该使用


最好对图像大小进行限制。例如,不允许上传超过2M或更多的图像。你可以很容易地做到这一点

压缩图像的一种方法是改变图像的宽度和质量

例如,下面的函数可以为您执行此操作

     function create_thumbnail($file, $pathToSave = '', $w, $quality = 95, $crop = FALSE)
    {
        list($width, $height) = getimagesize($file);
        $h = $height * ($w / $width);
        $new_height = $h;
        $r = $width / $height;
        if ($crop) {
            if ($width > $height) {
                $width = ceil($width - ($width * abs($r - $w / $h)));
            } else {
                $height = ceil($height - ($height * abs($r - $w / $h)));
            }
            $newwidth = $w;
            $newheight = $h;
        } else {
            if ($w / $h > $r) {
                $newwidth = $h * $r;
                $newheight = $h;
            } else {
                $newheight = $w / $r;
                $newwidth = $w;
            }
        }
        $what = getimagesize($file);
        switch (strtolower($what['mime'])) {
            case 'image/png':
                $src = imagecreatefrompng($file);
                break;
            case 'image/jpeg':
                $src = imagecreatefromjpeg($file);
                break;
            case 'image/gif':
                $src = imagecreatefromgif($file);
                break;
            default:
        }
        if ($new_height != '') {
            $newheight = $new_height;
        }
        $dst = imagecreatetruecolor($newwidth, $newheight);//the new image
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($dst, $pathToSave, $quality);
        return $dst;
    }

例如,w是设置为500的新宽度。(使用500px宽度创建新图像)

尺寸和质量应该是重要因素,不是吗?文件大小是它的一个函数。图像的尺寸和质量都相同,但文件大小不同吗?还是有些图像比其他图像大?然后将它们调整为相同的大小。@deceze图像的尺寸不同,但大多数是正方形*600@deceze有没有什么方法可以预测图像的大小,因为当图像以纵向模式拍摄时,即使系数20也不起作用我不想限制我想压缩大小创建压缩图像()用于压缩JPEG、PNG和GIF图像的函数。该函数采用3个参数–1。资料来源2。目的地3。文件质量调用imagecreatefromjpeg($source)、imagecreatefromgif($source)和imagecreatefrompng($source),根据$info['mime']值创建新图像。执行imagejpeg()方法将图像存储到目标。这里,第三个参数“质量”是可选的。它的取值范围为0–100,默认值为75.compressImage($\u FILES['imagefile']['tmp\u name'],$location,60);感谢您和@deceze告诉我文件维度非常重要,我做了类似的事情,但使用了
imagescale()
而不是
imagecopyresampled()
并在白色虚拟方形图像上复制了这张新图像,现在一切看起来都很好。
form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
    <input type="file" name="pictures" />
    <input type="submit" value="upload" />
</form>
     function create_thumbnail($file, $pathToSave = '', $w, $quality = 95, $crop = FALSE)
    {
        list($width, $height) = getimagesize($file);
        $h = $height * ($w / $width);
        $new_height = $h;
        $r = $width / $height;
        if ($crop) {
            if ($width > $height) {
                $width = ceil($width - ($width * abs($r - $w / $h)));
            } else {
                $height = ceil($height - ($height * abs($r - $w / $h)));
            }
            $newwidth = $w;
            $newheight = $h;
        } else {
            if ($w / $h > $r) {
                $newwidth = $h * $r;
                $newheight = $h;
            } else {
                $newheight = $w / $r;
                $newwidth = $w;
            }
        }
        $what = getimagesize($file);
        switch (strtolower($what['mime'])) {
            case 'image/png':
                $src = imagecreatefrompng($file);
                break;
            case 'image/jpeg':
                $src = imagecreatefromjpeg($file);
                break;
            case 'image/gif':
                $src = imagecreatefromgif($file);
                break;
            default:
        }
        if ($new_height != '') {
            $newheight = $new_height;
        }
        $dst = imagecreatetruecolor($newwidth, $newheight);//the new image
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($dst, $pathToSave, $quality);
        return $dst;
    }