实用工具-OpenERP基础篇-新手入门-数据导入-产品图片
-
OpenERP基础篇-新手入门-数据导入-产品图片
使用场景:
OpenERP导入产品时,目前只能导入产品的基础信息,但有些时候,我们希望也能批量导入产品图片数据.
申明:
使用有风险,操作需谨慎。
操作前先备份数据!!!
by xuxudodo
[openforge.cn]
2012-06
一. 准备工作
1. 环境准备
此工具需要LAMP环境, 即需要运行php, 且php.ini需要将以下这行;去掉. 这样就能连接postgres 数据库并操作了.
extension=php_pgsql.dll
2. 产品数据准备
假设你产品已经导入, 产品都有型号,接下来需要将产品图片名称都修改为对应的型号.
例子: 产品茶杯的型号为 10001,则图片名称需要修改为 10001.jpg.
把所有准备好的产品图片按照上述要求放到img目录中
二.导入数据
假设你已经完成上述准备工作,下载到了程序如下:
运行之后,会自动生成缩略图,并自动更新OpenERP相应帐套中产品的图片数据.
product_img_import.php 代码如下:
<?php
/
OpenERP产品图片数据
by xuxudodo [OpenForge.cn] 2012.06
****/
error_reporting(E_ERROR);
ini_set("arg_seperator.output", "&");
ini_set("magic_quotes_runtime", 0);
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
//帐套数据库连接
$conn_string = "host=192.168.1.1 port=5432 dbname=demo user=openerp password=postgres";
$conn = pg_connect($conn_string);
//系统路径
$filepath="E:/wwwroot/openerp/";
//图片目录名
$filepathimg="img";
//获取所有图片文件
$filelist = get_filetree($filepath.$filepathimg);
foreach($filelist as $value){
//echo "文件名 :\t\t".$value . "\n\r<br>";
if(strpos($value,"_thumb")>0) continue;
//图片类型
$filetype = strtolower(end(explode('.', $value)));
//echo $filetype . "\n\r<br>";
//图片文件名称(包含后缀)
$image_file = $value; //basename($value);
//图片文件名称(不包含后缀)
$image_file_name = basename($value,'.'.$filetype);
//echo $filename . "\n\r<br>";
//缩略图名称
$image_file_small = $filepath.$filepathimg.'/'.$image_file_name.'_thumb.'.$filetype;
list($image_width,$image_height,$image_type,$image_attr) = getimagesize($image_file);
$thumbwidth = min(120,$image_width);
$thumbheight = min(100,$image_width);
$maxw = 200;
$maxh = 200;
$result = makethumb($image_file, $image_file_small, $thumbwidth, $thumbheight, $maxw, $maxh);
if($image_width != $image_height)
{
$iw = $image_width;
$ih = $image_height;
if($maxw > 200 && $maxh > 200 && ($iw > $maxw || $ih > $maxh))
{
list($iw, $ih) = getimagesize($image_file);
}
$src_x = $src_y = 0;
$src_w = $src_h = min($iw, $ih);
if($iw > $ih)
{
$src_x = round(($iw - $ih) / 2);
}
else
{
$src_y = round(($ih - $iw) / 2);
}
$result = makethumb($image_file, $image_file_small, $thumbwidth, $thumbheight, 0, 0, $src_x, $src_y, $src_w, $src_h);
}
$thumbwidth2 = min(420,$image_width);
$thumbheight2 = min(300,$image_width);
$maxw2 = 500;
$maxh2 = 500;
//对原图继续进行减肥
if($image_width> $maxw2 || $image_height > $maxw2)
{
$result = makethumb($image_file, $image_file, $thumbwidth2, $thumbheight2, $maxw2, $maxh2);
}
if (!$result && !is_file($image_file_small))
{
@copy($image_file,$image_file_small);
}
$base64_image_file = base64_encode(file_get_contents($image_file));
//echo '原图转换:<br>'.$base64_image_file.'<br><br>';
$base64_image_file_small = base64_encode(file_get_contents($image_file_small));
//echo '缩略图转换:<br>'.$base64_image_file_small.'<br><br>';
//echo '数据库更新:<br>';
echo '-- 图片:'.basename($value).'<br>';
echo "update product_product set product_image='".$base64_image_file."' <br> where default_code='".$image_file_name."';<br>";
echo "update product_product set product_image_small='".$base64_image_file_small."' <br> where default_code='".$image_file_name."';<br><br>";
//数据导入
$data = array('product_image'=>$base64_image_file, 'product_image_small'=>$base64_image_file_small);
$condition = array('default_code'=>$image_file_name);
$res = pg_update($conn, 'product_product', $data, $condition);
if ($res) {
echo '-- 图片:'.basename($image_file)." 导入成功 $res\n<br><br>";
}
else {
echo '-- 图片:'.basename($image_file)." 警告:导入失败!\n<br><br>";
}
}
echo '-- 图片数据导入完成!\n<br><br>";
function get_filetree($path){
$tree = array();
foreach(glob($path . '/') as $single){
if(is_dir($single)){
$tree = array_merge($tree,get_filetree($single));
} else {
$tree[] = $single;
}
}
return $tree;
}
function makethumb($srcfile,$dstfile,$thumbwidth,$thumbheight,$maxthumbwidth=0,$maxthumbheight=0,$src_x=0,$src_y=0,$src_w=0,$src_h=0) {
if (!is_file($srcfile)) {
return '';
}
$tow = (int) $thumbwidth;
$toh = (int) $thumbheight;
if($tow < 30) {
$tow = 30;
}
if($toh < 30) {
$toh = 30;
}
$make_max = 0;
$maxtow = (int) $maxthumbwidth;
$maxtoh = (int) $maxthumbheight;
if($maxtow >= 300 && $maxtoh >= 300)
{
$make_max = 1;
}
$im = '';
if(false != ($data = getimagesize($srcfile))) {
if($data[2] == 1) {
$make_max = 0;
if(function_exists("imagecreatefromgif")) {
$im = imagecreatefromgif($srcfile);
}
} elseif($data[2] == 2) {
if(function_exists("imagecreatefromjpeg")) {
$im = imagecreatefromjpeg($srcfile);
}
} elseif($data[2] == 3) {
if(function_exists("imagecreatefrompng")) {
$im = imagecreatefrompng($srcfile);
}
}
}
if(!$im) return '';
$srcw = ($src_w ? $src_w : imagesx($im));
$srch = ($src_h ? $src_h : imagesy($im));
$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh)
{
$ftow = $tow;
$ftoh = round($ftow($srch/$srcw),2);
}
else
{
$ftoh = $toh;
$ftow = round($ftoh*($srcw/$srch),2);
}
if($make_max)
{
$maxtowh = $maxtow/$maxtoh;
if($maxtowh <= $srcwh)
{
$fmaxtow = $maxtow;
$fmaxtoh = round($fmaxtow*($srch/$srcw),2);
}
else
{
$fmaxtoh = $maxtoh;
$fmaxtow = round($fmaxtoh*($srcw/$srch),2);
}
if($srcw <= $maxtow && $srch <= $maxtoh)
{
$make_max = 0; }
}
$maxni = '';
if($srcw >= $tow || $srch >= $toh) {
if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && ($ni = imagecreatetruecolor($ftow, $ftoh))) {
imagecopyresampled($ni, $im, 0, 0, $src_x, $src_y, $ftow, $ftoh, $srcw, $srch);
if($make_max && ($maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh))) {
imagecopyresampled($maxni, $im, 0, 0, $src_x, $src_y, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && ($ni = imagecreate($ftow, $ftoh))) {
imagecopyresized($ni, $im, 0, 0, $src_x, $src_y, $ftow, $ftoh, $srcw, $srch);
if($make_max && ($maxni = imagecreate($fmaxtow, $fmaxtoh))) {
imagecopyresized($maxni, $im, 0, 0, $src_x, $src_y, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} else {
return '';
}
if(function_exists('imagejpeg')) {
imagejpeg($ni, $dstfile, 100);
if($make_max && $maxni) {
imagejpeg($maxni, $srcfile, 100);
}
} elseif(function_exists('imagepng')) {
imagepng($ni, $dstfile);
if($make_max && $maxni) {
imagepng($maxni, $srcfile);
}
}
imagedestroy($ni);
if($make_max && $maxni) {
imagedestroy($maxni);
}
}
imagedestroy($im);
if(!is_file($dstfile)) {
return '';
} else {
return $dstfile;
}
}
?>
代码一般,请各位高人不必纠结. 如果感觉似乎哪里看过,不用犹豫,一些函数确实不是我原创的.