gpt4 book ai didi

php - 如何更改jpg质量导出?

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:42 26 4
gpt4 key购买 nike

在下面的代码中,将 jpg 存储在 db 中,我需要知道如何先将 jpg 更改为 40% 质量然后保存?我试过 imagejpg ,但它保存空文件:

function exportGraphics($table_name)
{
$odbc_query = "SELECT * FROM " . $table_name;
mkdir("TI/" . $table_name);

$data = odbc_exec($this->odbc_id, $odbc_query);
odbc_longreadlen($data, 10485760); // 10MB = 10485760
while (odbc_fetch_row($data)) {
$row = odbc_fetch_array($data);
if ($row['GRD_ID'] != "") {
$file_name_jpg = "TI/" . $table_name . "/" . $row['GRD_ID'] . ".jpg";
$file = fopen($file_name_jpg, "w");
fputs($file, $row['GRD_GRAPHIC']);
fclose($file);
set_time_limit(3600);
unset($row);
}
}
print "Ýêñïîðò êàðòèíîê èç òàáëèöû " . $table_name . " çàâåðøåí!";
}

Warning: imagecreatefromstring() [function.imagecreatefromstring]: gd warning: one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully in X:\denwer\www\denwer\tecdoc3.php on line 103

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Passed data is not in 'WBMP' format in X:\denwer\www\denwer\tecdoc3.php on line 103

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Couldn't create GD Image Stream out of Data in X:\denwer\www\denwer\tecdoc3.php on line 103

Warning: imagecreatefromstring() [function.imagecreatefromstring]: gd warning: one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully in X:\denwer\www\denwer\tecdoc3.php on line 103

原代码:

 function exportGraphics($table_name) {
$odbc_query = "SELECT * FROM " . $table_name;
mkdir("TI/" . $table_name);

$data = odbc_exec($this->odbc_id, $odbc_query);
odbc_longreadlen($data, 10485760); //10MB = 10485760
while(odbc_fetch_row($data))
{
$row = odbc_fetch_array($data);
if($row['GRD_ID'] != "") {
$file_name_jp2 = "TI/" . $table_name . "/" . $row['GRD_ID'] . ".jp2";
$file = fopen ($file_name_jp2, "w");
fputs($file, $row['GRD_GRAPHIC']);
fclose($file);
set_time_limit(0);
unset($row);
}
}
print "Ýêñïîðò êàðòèíîê èç òàáëèöû " . $table_name . " çàâåðøåí!";
}

最佳答案

试试这个:

function exportGraphics($table_name) {
$odbc_query = "SELECT * FROM " . $table_name;
mkdir("TI/" . $table_name);

$data = odbc_exec($this->odbc_id, $odbc_query);
odbc_longreadlen($data, 10485760); //10MB = 10485760
while(odbc_fetch_row($data))
{
$row = odbc_fetch_array($data);
if($row['GRD_ID'] != "") {
$file_name_jpg = "TI/" . $table_name . "/" . $row['GRD_ID'] . ".jpg";

// create GD graphic from string, call imagejpeg to save new image
$im = imagecreatefromstring($row['GRD_GRAPHIC']);
imagejpeg($im, $file_name_jpg, 40);

set_time_limit(3600);
unset($row);
}
}
print "Ýêñïîðò êàðòèíîê èç òàáëèöû " . $table_name . " çàâåðøåí!";
}

或者这正是您尝试过的?

如果由于某种原因不起作用,您可以尝试:

$im = imagecreatefromstring($row['GRD_GRAPHIC']);
ob_start();
imagejpeg($im, null, 40);
$imgData = ob_get_contents();
ob_end_clean();

$file = fopen ($file_name_jpg, "w+b");
fputs($file, $imgData);
fclose($file);

编辑:GD 不工作的原因是因为它不支持 JPEG-2000 文件格式

可能的解决方案:在服务器上安装 imagemagick 并尝试这样的代码:

$file_name_jpg = "TI/" . $table_name . "/" . $row['GRD_ID'] . ".jp2";
$file_out_jpg = str_replace('.jp2', '.jpg', $file_name_jpg);

file_put_contents($file_name_jpg, $row['GRD_GRAPHIC']);

// execute imagemagick convert to change to jpeg with quality 40
exec("/usr/bin/convert $file_name_jpg -quality 40 -format jpg $file_out_jpg");
unlink($file_name_jpg); // get rid of temp jp2 file

关于php - 如何更改jpg质量导出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11678713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com