意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

深度分析lightinthebox 添加商品 图片 大图 小图 多图设置及源码

来源:恒创科技 编辑:恒创科技编辑部
2024-01-09 05:29:59


基于MVC的单主入口原理就不在此多讲了,可以认为有一个前端控制器 和很多业务控制器

打开新增产品url——​​http://localhost/webbuildceo_lb/admin/product.php?product_type=1&cPath=0_1_3&action=new_product&x=51&y=8​​


深度分析lightinthebox 添加商品 图片 大图 小图 多图设置及源码

点击新增商品按钮url——​​http://localhost/webbuildceo_lb/admin/product.php?cPath=0_1_3&product_type=1&action=new_product_preview​​

首先利用admin下的前端控制器product.php进行拦截处理.

<?php
require('includes/application_top.php');
require(DIR_WS_MODULES . 'prod_cat_header_code.php');
$action = (isset($_GET['action']) ? $_GET['action'] : '');
// Ultimate SEO URLs v2.100
// If the action will affect the cache entries
if (preg_match("/(insert|update|setflag)/i", $action)) {
include_once(DIR_WS_INCLUDES . 'reset_seo_cache.php');
}

if (zen_not_null($action)) {
switch ($action) {
case 'setflag':
if ( ($_GET['flag'] == '0') || ($_GET['flag'] == '1') ) {
if (isset($_GET['pID'])) {
zen_set_product_status($_GET['pID'], $_GET['flag']);
}
} zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID'] . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')));
break; case 'delete_product_confirm':
$delete_linked = 'true';
if ($_POST['delete_linked'] == 'delete_linked_no') {
$delete_linked = 'false';
} else {
$delete_linked = 'true';
}
$product_type = zen_get_products_type($_POST['products_id']);
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/delete_product_confirm.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/delete_product_confirm.php');
} else {
require(DIR_WS_MODULES . 'delete_product_confirm.php');
}
break;
case 'move_product_confirm':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/move_product_confirm.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/move_product_confirm.php');
} else {
require(DIR_WS_MODULES . 'move_product_confirm.php');
}
break;
case 'insert_product_meta_tags':
case 'update_product_meta_tags':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/update_product_meta_tags.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/update_product_meta_tags.php');
} else {
require(DIR_WS_MODULES . 'update_product_meta_tags.php');
}
break;
case 'insert_product':
case 'update_product':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/update_product.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/update_product.php');
} else {
require(DIR_WS_MODULES . 'update_product.php');
}
break;
case 'copy_to_confirm':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/copy_to_confirm.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/copy_to_confirm.php');
} else {
require(DIR_WS_MODULES . 'copy_to_confirm.php');
}
break;
case 'new_product_preview_meta_tags':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/new_product_preview_meta_tags.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/new_product_preview_meta_tags.php');
} else {
require(DIR_WS_MODULES . 'new_product_preview_meta_tags.php');
}
break;
case 'new_product_preview':
if (file_exists(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/new_product_preview.php')) {
require(DIR_WS_MODULES . $zc_products->get_handler($product_type) . '/new_product_preview.php');
} else {
require(DIR_WS_MODULES . 'new_product_preview.php');
}
break; }
}// check if the catalog image directory exists
if (is_dir(DIR_FS_CATALOG_IMAGES)) {
if (!is_writeable(DIR_FS_CATALOG_IMAGES)) $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
} else {
$messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');
}
?>
配置文件中 define('DIR_WS_MODULES', DIR_WS_INCLUDES . 'modules/');

那么我们顺藤摸瓜——F:\wamp\www\webbuildceo_lb\admin\includes\modules
在此文件夹下找到文件new_product_preview.php

<?php
if (! defined ( 'IS_ADMIN_FLAG' )) {
die ( 'Illegal Access' );
}
$file = 'F:\1.jpg';//$_FILES;
// copy image only if modified
$fileArray = array_keys($_FILES);array_shift($fileArray);
for($i = 0; $i < 20; $i ++) {
if ($_POST ['existimg_' . $i]) {
$existimgArray[] = $_POST ['existimg_' . $i];
}
}require_once(DIR_FS_CATALOG . DIR_WS_FUNCTIONS . 'extra_functions/functions_bmz_io.php');
$watermark = DIR_FS_CATALOG_IMAGES.'watermark.png';
$wateralpha = 65;
if (! isset ( $_GET ['read'] ) || $_GET ['read'] == 'only') {
if (count($_FILES) > 0) {
$data = array();
for($i = count ( $fileArray )-1; $i >=0; $i --) {
$setSubFolder = date('d')<16?'a':'b';
$nameBase = date('Ym').'/'.$setSubFolder.'/'.time().$i; $data['ImgExtension'] = strtolower(substr( $_FILES[$fileArray[$i]]['name'], strrpos($_FILES[$fileArray[$i]]['name'], '.')));
$source_name = $_FILES[$fileArray[$i]]['tmp_name'];

//Normal images
$destination_name = DIR_FS_CATALOG_IMAGES . 's/'.$nameBase . $data['ImgExtension'];
io_makeFileDir($destination_name);

if ( !move_uploaded_file($source_name, $destination_name) ) {
$messageStack->add( TEXT_MSG_NOUPLOAD_LARGE, "error" );
}else{
$messageStack->add( TEXT_MSG_UPLOAD_LARGE, "success" );
}
//die("11");
$data['originalFileName'] = 'vold/'.$nameBase . $data['ImgExtension'];
$data['smallFileName'] = 's/'.$nameBase . $data['ImgExtension'];
$data['mediumFileName'] = 'l/'.$nameBase . $data['ImgExtension'];
//print_r($data['mediumFileName']);
$data['largeFileName'] = 'v/'.$nameBase . $data['ImgExtension'];
//print_r($data['largeFileName']);
$destination_name_original = DIR_FS_CATALOG_IMAGES .$data['originalFileName'];
$destination_name_small = DIR_FS_CATALOG_IMAGES .$data['smallFileName'];
$destination_name_medium = DIR_FS_CATALOG_IMAGES .$data['mediumFileName'];
$destination_name_large = DIR_FS_CATALOG_IMAGES .$data['largeFileName'];
io_makeFileDir($destination_name_medium);
io_makeFileDir($destination_name_large);
io_makeFileDir($destination_name_original);

//copy original image
if (!copy($destination_name, $destination_name_original))
{
$messageStack->add('failed to copy '.$destination_name_original.'...', "error" );
}
else
{
$messageStack->add('Successed to copy '.$destination_name_original.'...', "success" );
} if (!copy($destination_name, $destination_name_medium)) {
$messageStack->add('failed to copy '.$destination_name_medium.'...', "error" );
}else{
$messageStack->add('Successed to copy '.$destination_name_medium.'...', "success" );
if ($data['ImgExtension'] == ".jpg" || $data['ImgExtension'] == ".jpeg")
$im = @imagecreatefromjpeg ($destination_name_medium );
if ($data['ImgExtension'] == ".gif")
$im = @imagecreatefromgif ($destination_name_medium );
var_dump($im);
if ($data['ImgExtension'] == ".png")
$im = @imagecreatefrompng ($destination_name_medium );
list($width_orig, $height_orig) = getimagesize($destination_name_medium );
$width = PRODUCT_MEDIUM_IMAGE_WIDTH;
$height = PRODUCT_MEDIUM_IMAGE_HEIGHT;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
} $im_s = imagecreatetruecolor(PRODUCT_MEDIUM_IMAGE_WIDTH,PRODUCT_MEDIUM_IMAGE_HEIGHT);
$white = imagecolorallocate($im_s, 255, 255, 255);
imagefill($im_s, 0, 0, $white);
if ($width_orig <= $width){
$wpos=(PRODUCT_MEDIUM_IMAGE_WIDTH - $width_orig)/2;
$hpos=(PRODUCT_MEDIUM_IMAGE_HEIGHT - $height_orig)/2;
imagecopy($im_s,$im,$wpos, $hpos, 0, 0,$width_orig,$height_orig );
}else{
$wpos=(PRODUCT_MEDIUM_IMAGE_WIDTH - $width)/2;
$hpos=(PRODUCT_MEDIUM_IMAGE_HEIGHT - $height)/2;
imagecopyresampled($im_s,$im,$wpos, $hpos, 0, 0,$width,$height,$width_orig,$height_orig );
} //imagecopyresampled($im_s, $im, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );
imagejpeg ( $im_s, $destination_name_medium, 100 );
imagedestroy ( $im );
}



if (!copy($destination_name, $destination_name_large)) {
$messageStack->add('failed to copy '.$destination_name_large.'...', "error" );
}else{
$messageStack->add('Successed to copy '.$destination_name_large.'...', "success" );
if ($data['ImgExtension'] == ".jpg" || $data['ImgExtension'] == ".jpeg")
$im = @imagecreatefromjpeg ($destination_name_large );
if ($data['ImgExtension'] == ".gif")
$im = @imagecreatefromgif ($destination_name_large );
if ($data['ImgExtension'] == ".png")
$im = @imagecreatefrompng ($destination_name_large );
list($width_orig, $height_orig) = getimagesize($destination_name_large );
$width = PRODUCT_MEDIUM_LARGE_WIDTH;
$height = PRODUCT_MEDIUM_LARGE_HEIGHT;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
$im_s = @imagecreatetruecolor ( PRODUCT_MEDIUM_LARGE_WIDTH, PRODUCT_MEDIUM_LARGE_HEIGHT );
$white = imagecolorallocate($im_s, 255, 255, 255);
imagefill($im_s, 0, 0, $white);
if ($width_orig <= $width){
$wpos=(PRODUCT_MEDIUM_LARGE_WIDTH - $width_orig)/2;
$hpos=(PRODUCT_MEDIUM_LARGE_HEIGHT - $height_orig)/2;
imagecopy($im_s,$im,$wpos, $hpos, 0, 0,$width_orig,$height_orig );
}else{
$wpos=(PRODUCT_MEDIUM_LARGE_WIDTH - $width)/2;
$hpos=(PRODUCT_MEDIUM_LARGE_HEIGHT - $height)/2;
//imagecopyresized($im_s,$im,$wpos, $hpos, 0, 0,$width,$height,$width_orig,$height_orig );
imagecopyresampled($im_s,$im,$wpos, $hpos, 0, 0,$width,$height,$width_orig,$height_orig );
}
$im_watermark = @imagecreatefrompng ($watermark );
list($water_w, $water_h) = getimagesize($watermark);
$dst_x = (PRODUCT_MEDIUM_LARGE_WIDTH - $water_w)/2;
$dst_y = (PRODUCT_MEDIUM_LARGE_HEIGHT - $water_h)/2;
imagecopymerge($im_s, $im_watermark, $dst_x, $dst_y, 0, 0,$water_w, $water_h,$wateralpha);
@imagejpeg ( $im_s, $destination_name_large, 100 );
@imagedestroy ( $im );
}

if ($data['ImgExtension'] == ".jpg" || $data['ImgExtension'] == ".jpeg")
$im = @imagecreatefromjpeg ($destination_name_small );
if ($data['ImgExtension'] == ".gif")
$im = @imagecreatefromgif ($destination_name_small );
if ($data['ImgExtension'] == ".png")
$im = @imagecreatefrompng ($destination_name_small );
list($width_orig, $height_orig) = getimagesize($destination_name_small);
$width = PRODUCT_MEDIUM_SMALL_WIDTH;
$height = PRODUCT_MEDIUM_SMALL_HEIGHT;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
$im_s = @imagecreatetruecolor( PRODUCT_MEDIUM_SMALL_WIDTH, PRODUCT_MEDIUM_SMALL_HEIGHT ) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im_s, 255, 255, 255);
imagefill($im_s, 0, 0, $white);
if ($width_orig <= $width){
$wpos=(PRODUCT_MEDIUM_SMALL_WIDTH - $width_orig)/2;
$hpos=(PRODUCT_MEDIUM_SMALL_HEIGHT - $height_orig)/2;
imagecopy($im_s,$im,$wpos, $hpos, 0, 0,$width_orig,$height_orig );
}else{
$wpos=(PRODUCT_MEDIUM_SMALL_WIDTH - $width)/2;
$hpos=(PRODUCT_MEDIUM_SMALL_HEIGHT - $height)/2;
imagecopyresampled($im_s,$im,$wpos, $hpos, 0, 0,$width,$height,$width_orig,$height_orig );
}
imagejpeg ( $im_s, $destination_name_small, 100 );

imagedestroy ( $im );
$products_image_names[] = 's/'.$nameBase.$data['ImgExtension'];

}
if(is_array($existimgArray)){
if(is_array($products_image_names)){
$newArray = array_merge($existimgArray,$products_image_names);
$products_image_name = $newArray[0];
$products_image_name_string = implode ( ',', $newArray);
}else{
$products_image_name = $existimgArray[0];
$products_image_name_string = implode ( ',', $existimgArray);
}
}else{
$products_image_name = $products_image_names[0];
$products_image_name_string = @implode ( ',', $products_image_names);
}

} }else{
if(is_array($existimgArray)){
$products_image_name = $existimgArray[0];
$products_image_name_string = implode ( ',', $existimgArray);
}else{
$products_image_name = $existimgArray[0];
$products_image_name_string = $existimgArray[0];
}

}
?>

至此弄明白
原图 vold originalFileName
大图 largeFileName
中图 l mediumFileName
小图 s smallFileName


自动缩放图像代码如下:
//copy original image
if (!copy($destination_name, $destination_name_original))
{
$messageStack->add('failed to copy '.$destination_name_original.'...', "error" );
}
else
{
$messageStack->add('Successed to copy '.$destination_name_original.'...', "success" );
} if (!copy($destination_name, $destination_name_medium)) {
$messageStack->add('failed to copy '.$destination_name_medium.'...', "error" );
}else{
$messageStack->add('Successed to copy '.$destination_name_medium.'...', "success" );
if ($data['ImgExtension'] == ".jpg" || $data['ImgExtension'] == ".jpeg")
$im = @imagecreatefromjpeg ($destination_name_medium );
if ($data['ImgExtension'] == ".gif")
$im = @imagecreatefromgif ($destination_name_medium );
var_dump($im);
if ($data['ImgExtension'] == ".png")
$im = @imagecreatefrompng ($destination_name_medium );
list($width_orig, $height_orig) = getimagesize($destination_name_medium );
$width = PRODUCT_MEDIUM_IMAGE_WIDTH;
$height = PRODUCT_MEDIUM_IMAGE_HEIGHT;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
} $im_s = imagecreatetruecolor(PRODUCT_MEDIUM_IMAGE_WIDTH,PRODUCT_MEDIUM_IMAGE_HEIGHT);
$white = imagecolorallocate($im_s, 255, 255, 255);
imagefill($im_s, 0, 0, $white);
if ($width_orig <= $width){
$wpos=(PRODUCT_MEDIUM_IMAGE_WIDTH - $width_orig)/2;
$hpos=(PRODUCT_MEDIUM_IMAGE_HEIGHT - $height_orig)/2;
imagecopy($im_s,$im,$wpos, $hpos, 0, 0,$width_orig,$height_orig );
}else{
$wpos=(PRODUCT_MEDIUM_IMAGE_WIDTH - $width)/2;
$hpos=(PRODUCT_MEDIUM_IMAGE_HEIGHT - $height)/2;
imagecopyresampled($im_s,$im,$wpos, $hpos, 0, 0,$width,$height,$width_orig,$height_orig );
} //imagecopyresampled($im_s, $im, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );
imagejpeg ( $im_s, $destination_name_medium, 100 );
imagedestroy ( $im );
}


ok。。。。。。。。。。。。。。。。。。。。。。。。。。。到此 全部明朗了 lightinthebox 采集商品多图切换 也变得非常容易了。


另外给出几个图像操作有用的函数:
imagecreatefromjpeg — 从 JPEG 文件或 URL 新建一图像
imagecreatefromgif — 从 GIF 文件或 URL 新建一图像
getimagesize — 取得图像大小
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。
list — 把数组中的值赋给一些变量
imagecreatetruecolor — 新建一个真彩色图像
imagestring — 水平地画一行字符串


imagecreatetruecolor — 新建一个真彩色图像

imagefill — 区域填充
imagecopy — 拷贝图像的一部分
imagecopyresampled — 重采样拷贝部分图像并调整大小
imagedestroy — 销毁一图像

上一篇: php之性能优化案例 下一篇: lightintheboxzencart自动添加商品多图