在下面的代码中,将数据库存储在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 . " çàâåðøåí!";
}
警告:imagecreatefromstring()[function.imagecreatefromstring]:gd 警告:内存分配乘法的一个参数是 否定或零,失败的操作优雅地进入 第103行的X:\ denwer \ www \ denwer \ tecdoc3.php
警告:imagecreatefromstring()[function.imagecreatefromstring]: 传递的数据不是'WBMP'格式 第103行的X:\ denwer \ www \ denwer \ tecdoc3.php
警告:imagecreatefromstring()[function.imagecreatefromstring]: 无法在数据中创建GD图像流 第103行的X:\ denwer \ www \ denwer \ tecdoc3.php
警告:imagecreatefromstring()[function.imagecreatefromstring]:gd 警告:内存分配乘法的一个参数是 否定或零,失败的操作优雅地进入 第103行的X:\ denwer \ www \ denwer \ tecdoc3.php
原始代码:
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 . " çàâåðøåí!";
}
答案 0 :(得分:2)
试试这个:
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