3

在以下代码中,将 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 . " çàâåðøåí!";
}

警告:imagecreatefromstring() [function.imagecreatefromstring]:gd 警告:内存分配乘法的一个参数为负数或零,在 X:\denwer\www\denwer\tecdoc3.php 第 103 行中优雅地失败操作

警告:imagecreatefromstring() [function.imagecreatefromstring]:在第 103 行的 X:\denwer\www\denwer\tecdoc3.php 中传递的数据不是“WBMP”格式

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

警告:imagecreatefromstring() [function.imagecreatefromstring]:gd 警告:内存分配乘法的一个参数为负数或零,在 X:\denwer\www\denwer\tecdoc3.php 第 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 . " çàâåðøåí!";
 }
4

1 回答 1

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
于 2012-07-26T22:09:29.673 回答