我一直在尝试在我的codeigniter API中使用imagemagic。问题是下面的代码可以与我的jpg图像压缩正常工作。当我上传2MB大小的“ .jpg”图像时,它成功地将其大小调整为服务器上的完美80kb文件,但是当我使用“ .png”扩展名时,该大小减小了,但幅度仍然很小,但仍然为我提供了300kb的图像大小,我观察到png格式的“质量”无效。我尝试将质量分别设置为100和50,但对于“ .png”仍然使用大图像尺寸。]
而且,如果我删除“ width”和“ height”配置参数,而不仅仅是“ quality”不会影响图像文件,它将上传非常大的图像...没有基于“ quality”参数的图像文件大小压缩。
任何人都可以在我犯错或遗漏的地方提供帮助吗?
我的API:
public function multi_image_upload()
{
$aa = array();
if (!empty($_FILES['images'])) {
foreach ($_FILES['images']['name'] as $key => $image) {
$extFile = $_FILES['images']['name'][$key];
$ext = pathinfo($extFile, PATHINFO_EXTENSION);
$nm = date('ymdhis') . '-bingo' . rand(11111, 99999);
$filnm = $nm . ".png";
$file = APPPATH . '../upld/biz/' . $filnm;
$newname = $filnm;
$source_image = $file;
if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
$this->fun->imageupload("upld/biz/", $newname, $source_image);
if (file_exists($file)) {
unlink($file);
}
}
}
$res = $this->responce(false, $aa);
} else {
$res = $this->responce(true, "Please select image");
}
echo $res;
}
图像上传模型:
public function imageupload($url, $newname, $source_image)
{
$path = APPPATH . '../' . $url;
$imgsz = array(256,700);
foreach ($imgsz as $imxs) {
$sizefolder = $imxs . "x" . $imxs . "/";
if (!file_exists($path . $sizefolder)) {
mkdir($path . $sizefolder, 0777);
}
$size = getimagesize($source_image);
$filekb = filesize($source_image); // $config['image_library'] = 'gd2';
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['overwrite'] = TRUE;
$config['source_image'] = $source_image;
$config['new_image'] = $path . $sizefolder . $newname;
$config['quality'] = 100;
if ($size[1] > $size[0]) {
$config['height'] = $imxs;
} else {
$config['width'] = $imxs;
}
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
JPEG是有损压缩,这意味着您可以通过设置质量设置来限制输出图像的大小,从而导致较低质量的图像以及较小的文件大小。PNG是无损压缩,因此您不能限制质量-它始终保持不变。但是您可以限制压缩-Imagemagick为缩小PNG文件所做的努力-但与e一样。 G。 ZIP压缩的输出大小在很大程度上取决于数据类型-大多数图像的压缩效果都不佳。
有关限制PNG文件大小的更多选项,另请参见Compress a PNG image with ImageMagick。>>