/** * 图片处理函数 * * @access public * @param integer $page * @param integer $page_size * @param integer $type * @param boolen $thumb 是否生成缩略图 * @param boolen $watermark 是否生成水印图 * @param boolen $change true 生成新图,删除旧图 false 用新图覆盖旧图 * @param boolen $silent 是否执行能忽略错误 * * @return void */ function process_image($page = 1, $page_size = 100, $type = 0, $thumb = true, $watermark = true, $change = false, $silent = true) { if ($type == 0) { $sql = "SELECT g.goods_id, g.original_img, g.goods_img, g.goods_thumb FROM " . $GLOBALS['ecs']->table('goods') . " AS g WHERE g.original_img > ''" . $GLOBALS['goods_where']; $res = $GLOBALS['db']->SelectLimit($sql, $page_size, ($page - 1) * $page_size); while ($row = $GLOBALS['db']->fetchRow($res)) { $goods_thumb = ''; $image = ''; /* 水印 */ if ($watermark) { /* 获取加水印图片的目录 */ if (empty($row['goods_img'])) { $dir = dirname(ROOT_PATH . $row['original_img']) . '/'; } else { $dir = dirname(ROOT_PATH . $row['goods_img']) . '/'; } $image = $GLOBALS['image']->make_thumb(ROOT_PATH . $row['original_img'], $GLOBALS['_CFG']['image_width'], $GLOBALS['_CFG']['image_height'], $dir); //先生成缩略图 if (!$image) { //出错返回 $msg = sprintf($GLOBALS['_LANG']['error_pos'], $row['goods_id']) . "\n" . $GLOBALS['image']->error_msg(); if ($silent) { $GLOBALS['err_msg'][] = $msg; continue; } else { make_json_error($msg); } } $image = $GLOBALS['image']->add_watermark(ROOT_PATH . $image, '', $GLOBALS['_CFG']['watermark'], $GLOBALS['_CFG']['watermark_place'], $GLOBALS['_CFG']['watermark_alpha']); if (!$image) { //出错返回 $msg = sprintf($GLOBALS['_LANG']['error_pos'], $row['goods_id']) . "\n" . $GLOBALS['image']->error_msg(); if ($silent) { $GLOBALS['err_msg'][] = $msg; continue; } else { make_json_error($msg); } } /* 重新格式化图片名称 */ $image = reformat_image_name('goods', $row['goods_id'], $image, 'goods'); if ($change || empty($row['goods_img'])) { /* 要生成新链接的处理过程 */ if ($image != $row['goods_img']) { $sql = "UPDATE " . $GLOBALS['ecs']->table('goods') . " SET goods_img = '{$image}' WHERE goods_id = '" . $row['goods_id'] . "'"; $GLOBALS['db']->query($sql); /* 防止原图被删除 */ if ($row['goods_img'] != $row['original_img']) { @unlink(ROOT_PATH . $row['goods_img']); } } } else { replace_image($image, $row['goods_img'], $row['goods_id'], $silent); } } /* 缩略图 */ if ($thumb) { if (empty($row['goods_thumb'])) { $dir = dirname(ROOT_PATH . $row['original_img']) . '/'; } else { $dir = dirname(ROOT_PATH . $row['goods_thumb']) . '/'; } $goods_thumb = $GLOBALS['image']->make_thumb(ROOT_PATH . $row['original_img'], $GLOBALS['_CFG']['thumb_width'], $GLOBALS['_CFG']['thumb_height'], $dir); /* 出错处理 */ if (!$goods_thumb) { $msg = sprintf($GLOBALS['_LANG']['error_pos'], $row['goods_id']) . "\n" . $GLOBALS['image']->error_msg(); if ($silent) { $GLOBALS['err_msg'][] = $msg; continue; } else { make_json_error($msg); } } /* 重新格式化图片名称 */ $goods_thumb = reformat_image_name('goods_thumb', $row['goods_id'], $goods_thumb, 'thumb'); if ($change || empty($row['goods_thumb'])) { if ($row['goods_thumb'] != $goods_thumb) { $sql = "UPDATE " . $GLOBALS['ecs']->table('goods') . " SET goods_thumb = '{$goods_thumb}' WHERE goods_id = '" . $row['goods_id'] . "'"; $GLOBALS['db']->query($sql); /* 防止原图被删除 */ if ($row['goods_thumb'] != $row['original_img']) { @unlink(ROOT_PATH . $row['goods_thumb']); } } } else { replace_image($goods_thumb, $row['goods_thumb'], $row['goods_id'], $silent); } } } } else { /* 遍历商品相册 */ $sql = "SELECT album.goods_id, album.img_id, album.img_url, album.thumb_url, album.img_original FROM " . $GLOBALS['ecs']->table('goods_gallery') . " AS album " . $GLOBALS['album_where']; $res = $GLOBALS['db']->SelectLimit($sql, $page_size, ($page - 1) * $page_size); while ($row = $GLOBALS['db']->fetchRow($res)) { $thumb_url = ''; $image = ''; /* 水印 */ if ($watermark && file_exists(ROOT_PATH . $row['img_original'])) { if (empty($row['img_url'])) { $dir = dirname(ROOT_PATH . $row['img_original']) . '/'; } else { $dir = dirname(ROOT_PATH . $row['img_url']) . '/'; } $file_name = cls_image::unique_name($dir); $file_name .= cls_image::get_filetype(empty($row['img_url']) ? $row['img_original'] : $row['img_url']); copy(ROOT_PATH . $row['img_original'], $dir . $file_name); $image = $GLOBALS['image']->add_watermark($dir . $file_name, '', $GLOBALS['_CFG']['watermark'], $GLOBALS['_CFG']['watermark_place'], $GLOBALS['_CFG']['watermark_alpha']); if (!$image) { @unlink($dir . $file_name); $msg = sprintf($GLOBALS['_LANG']['error_pos'], $row['goods_id']) . "\n" . $GLOBALS['image']->error_msg(); if ($silent) { $GLOBALS['err_msg'][] = $msg; continue; } else { make_json_error($msg); } } /* 重新格式化图片名称 */ $image = reformat_image_name('gallery', $row['goods_id'], $image, 'goods'); if ($change || empty($row['img_url']) || $row['img_original'] == $row['img_url']) { if ($image != $row['img_url']) { $sql = "UPDATE " . $GLOBALS['ecs']->table('goods_gallery') . " SET img_url='{$image}' WHERE img_id='{$row['img_id']}'"; $GLOBALS['db']->query($sql); if ($row['img_original'] != $row['img_url']) { @unlink(ROOT_PATH . $row['img_url']); } } } else { replace_image($image, $row['img_url'], $row['goods_id'], $silent); } } /* 缩略图 */ if ($thumb) { if (empty($row['thumb_url'])) { $dir = dirname(ROOT_PATH . $row['img_original']) . '/'; } else { $dir = dirname(ROOT_PATH . $row['thumb_url']) . '/'; } $thumb_url = $GLOBALS['image']->make_thumb(ROOT_PATH . $row['img_original'], $GLOBALS['_CFG']['thumb_width'], $GLOBALS['_CFG']['thumb_height'], $dir); if (!$thumb_url) { $msg = sprintf($GLOBALS['_LANG']['error_pos'], $row['goods_id']) . "\n" . $GLOBALS['image']->error_msg(); if ($silent) { $GLOBALS['err_msg'][] = $msg; continue; } else { make_json_error($msg); } } /* 重新格式化图片名称 */ $thumb_url = reformat_image_name('gallery_thumb', $row['goods_id'], $thumb_url, 'thumb'); if ($change || empty($row['thumb_url'])) { if ($thumb_url != $row['thumb_url']) { $sql = "UPDATE " . $GLOBALS['ecs']->table('goods_gallery') . " SET thumb_url='{$thumb_url}' WHERE img_id='{$row['img_id']}'"; $GLOBALS['db']->query($sql); @unlink(ROOT_PATH . $row['thumb_url']); } } else { replace_image($thumb_url, $row['thumb_url'], $row['goods_id'], $silent); } } } } }