php 将 imagejpeg() 与 file_put_contents() 结合使用
php using imagejpeg() with file_put_contents()
我正在使用 file_put_contents()
将图像存储在目录中。但是,图像尺寸太大。我想将图像大小从 mb
压缩到 kb
以改进我的 Web 应用程序。但是,我不确定是否可以将 imagejpeg()
函数与 file_put_contents()
一起使用。特此通知,我正在使用 Croppie.js
并且图像正在通过 AJAX 传输到单独的文件进行处理。
PHP
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$image = base64_decode($image_array_2[1]);
$imageName = $user['id'][0] . "_" . time() . '.jpg';
$dir = "../images/users/".$user['id'][0]."/"."avatar/";
$imageDirectory = $dir.$imageName;
如果我上传 1 mb
尺寸的图片,它会变成 6-7 mb
尺寸。不是减少它,而是将大小乘以 6-7 倍。我希望它减少到 50-100 kb
以下。有什么办法可以压缩这里的大小吗?
我自己找到了解决方案。我在这里发帖供将来寻找它的人使用。如果您使用 Croppie.js 并且您的文件太大,您需要做的第一件事就是将图像格式设置为 format: "jpeg"
。与 PNG 文件相比,JPEG 本身使用压缩技术,因此无需执行任何操作即可生成较小的文件。现在,如果您想要更好的质量,请设置 size: "original"
和 quality: 1
。基本上你的 Croppie 设置应该是这样的:
resize.croppie('result', {
type: "canvas",
size: "original",
format: "jpeg",
quality: 1
})
现在,使用此设置,文件大小应该小于 PNG,但仍然足够大以指示网络应用程序的危险信号。因此,解决方案是在上传过程中使用 PHP 压缩图像。现在我不得不面对挑战。我在互联网上找到的所有示例都是使用上传图像时通常使用的 $_FILES
方法给出的。但是,正如您在我上面的问题中看到的那样,PHP 编码不同。因此,我必须找到一种方法来解决这个问题,因为网络上没有给出考虑使用 Croppie.js 上传的相关示例。因此,我继续使用试错法自行解决问题,并能够复制通常用于图像压缩的 $_FILES
方法的解决方案。我为它编写了自己的函数。但是,下面的函数只包含 JPEG 图像类型的解决方案。如果您想要任何其他格式,请随时使用我的函数作为示例并进行相应的修改。
function compressImage($image, $imageDirectory, $quality) {
return imagejpeg(imagecreatefromstring($image), $imageDirectory, $quality);
}
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$image = base64_decode($image_array_2[1]);
$imageName = $user['id'][0] . "_" . time() . '.jpg';
if(!file_exists("../images/users/".$user['id'][0]."/"."avatar/")) {
$dir = mkdir("../images/users/".$user['id'][0]."/"."avatar/", 0777, true);
}else{
$dir = "../images/users/".$user['id'][0]."/"."avatar/";
}
$imageDirectory = $dir.$imageName;
compressImage($image, $imageDirectory, 50);
尽情享受吧!
我正在使用 file_put_contents()
将图像存储在目录中。但是,图像尺寸太大。我想将图像大小从 mb
压缩到 kb
以改进我的 Web 应用程序。但是,我不确定是否可以将 imagejpeg()
函数与 file_put_contents()
一起使用。特此通知,我正在使用 Croppie.js
并且图像正在通过 AJAX 传输到单独的文件进行处理。
PHP
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$image = base64_decode($image_array_2[1]);
$imageName = $user['id'][0] . "_" . time() . '.jpg';
$dir = "../images/users/".$user['id'][0]."/"."avatar/";
$imageDirectory = $dir.$imageName;
如果我上传 1 mb
尺寸的图片,它会变成 6-7 mb
尺寸。不是减少它,而是将大小乘以 6-7 倍。我希望它减少到 50-100 kb
以下。有什么办法可以压缩这里的大小吗?
我自己找到了解决方案。我在这里发帖供将来寻找它的人使用。如果您使用 Croppie.js 并且您的文件太大,您需要做的第一件事就是将图像格式设置为 format: "jpeg"
。与 PNG 文件相比,JPEG 本身使用压缩技术,因此无需执行任何操作即可生成较小的文件。现在,如果您想要更好的质量,请设置 size: "original"
和 quality: 1
。基本上你的 Croppie 设置应该是这样的:
resize.croppie('result', {
type: "canvas",
size: "original",
format: "jpeg",
quality: 1
})
现在,使用此设置,文件大小应该小于 PNG,但仍然足够大以指示网络应用程序的危险信号。因此,解决方案是在上传过程中使用 PHP 压缩图像。现在我不得不面对挑战。我在互联网上找到的所有示例都是使用上传图像时通常使用的 $_FILES
方法给出的。但是,正如您在我上面的问题中看到的那样,PHP 编码不同。因此,我必须找到一种方法来解决这个问题,因为网络上没有给出考虑使用 Croppie.js 上传的相关示例。因此,我继续使用试错法自行解决问题,并能够复制通常用于图像压缩的 $_FILES
方法的解决方案。我为它编写了自己的函数。但是,下面的函数只包含 JPEG 图像类型的解决方案。如果您想要任何其他格式,请随时使用我的函数作为示例并进行相应的修改。
function compressImage($image, $imageDirectory, $quality) {
return imagejpeg(imagecreatefromstring($image), $imageDirectory, $quality);
}
$image_array_1 = explode(";", $image);
$image_array_2 = explode(",", $image_array_1[1]);
$image = base64_decode($image_array_2[1]);
$imageName = $user['id'][0] . "_" . time() . '.jpg';
if(!file_exists("../images/users/".$user['id'][0]."/"."avatar/")) {
$dir = mkdir("../images/users/".$user['id'][0]."/"."avatar/", 0777, true);
}else{
$dir = "../images/users/".$user['id'][0]."/"."avatar/";
}
$imageDirectory = $dir.$imageName;
compressImage($image, $imageDirectory, 50);
尽情享受吧!