html
<link rel="stylesheet" href="/css/uploadify.css" type="text/css" />
<script type="text/javascript" SRC="/js/jquery.uploadify.min.js"></script>
<script type="text/javascript" SRC="/js/swfobject.js"></script>
<div id="createWeightExceptionProblemDialog" style="display: none"></div>
<div id="uploadPicturesDialog" style="display: none" title="图片上传">
<div id="fileQueue"></div>
<input type="file" id="uploadify" class="half" value="" name="uploadify"/>
</div>
js
$(function () {
//重量异常问题件列表
$("#createWeightExceptionProblemDialog").dialog({
title: "创建重量异常问题件",
autoOpen: false,
modal: true,
width: 500,
height: 500,
show: "slide",
buttons: {
"关闭": function () {
$(this).dialog("close");
}
}
});
//上传图片
$(".uploadPictures").live("click", function () {
$("#uploadPicturesDialog").dialog("open");
});
$("#uploadPicturesDialog").dialog({
title: "上传图片",
autoOpen: false,
modal: true,
width: 500,
height: 200,
show: "slide",
buttons: {
"上传": function () {
$('#uploadify').uploadifyUpload();
},
"取消": function () {
$('#uploadify').uploadifyClearQueue();
},
"关闭": function () {
$(this).dialog("close");
}
}
});
//图片对应处理
$("#uploadify").uploadify({
uploader: "/js/uploadify.swf",
script: "/default/index/upload-picture",
cancelImg: "/images/cancel.png",
folder: "UploadPictures",
//默认上传文件数
queueSizeLimit: 3,
//每次最大上传文件数
simUploadLimit: 3,
queueID: "fileQueue",
auto: false,
multi: true,
fileDataName: "picture",
fileExt: "*.png;*.jpg;*.gif;*.bmp",
fileDesc: "*.png *.jpg *.gif *.bmp",
"onSelectOnce": function (event, data) {
var qu = data.filesSelected, has = $(".product-img-view2").length;
if ((qu + has) > 3) {
alert("最多只能上传3张图片");
$("#uploadify").uploadifyClearQueue();
return false;
}
},
"onComplete": function (event, queueId, fileObj, response, data) {
var jsonData = eval("(" + response + ")");
if (jsonData.ask) {
$("#images").before("<div style='display: table-cell;padding-right: 10px' class='product-img-view2' onclick='daletePictures(this)'><img src='" + jsonData.filepath + "' width='100px' height='100px'/><input type='hidden' name='picture[]' value='" + jsonData.filepath + "'/></div>");
} else {
alert(jsonData.error);
return false;
}
},
"onAllComplete": function () {
$('#uploadify').uploadifyClearQueue();
$("#uploadPicturesDialog").dialog("close");
},
"onError": function (event, ID, fileObj, errorObj) {
alert(errorObj.type + " Error: " + errorObj.info);
}
});
});
//重量异常问题件列表
function weightExceptionProblemHTML(tip, customer_id) {
$("<div title='提示'><p align='center'>" + tip + "</p></div>").dialog({
modal: true,
width: 500,
buttons: {
'确定': function () {
$(this).dialog("close");
var HTML = "";
HTML += "<div>质检异常描述: 重量异常 <textarea rows='10' name='message' cols='35' style='resize: none;vertical-align: top'>备注:重量异常 " + $("#weight").val() + "kg</textarea></div><br/>";
HTML += "<div>请输入客户 ID: <input type='text' name='product_sku' value='" + $(".sku").html() + "' style='width: 350px'></div><br/>";
HTML += "<div>";
HTML += "<span style='display: table-cell;vertical-align: top'>";
HTML += "<div style='width: 80px; margin-left: 36px'>Image:</div>";
HTML += "<div style='width: 80px;'><button class='uploadPictures'>上传图片</button></div>";
HTML += "</span><span id='images' style='display: table-cell'></span>";
HTML += "</div><br>"
HTML += "<div style='text-align: center'><input type='button' name='save' value='保存' onclick='createWeightExceptionProblem(\"" + customer_id + "\")'></div>";
$("#createWeightExceptionProblemDialog").dialog("open").html(HTML);
}
}
});
}
//删除图片
function daletePictures(object) {
if (confirm("确定删除图片")) {
$(object).remove();
}
}
//创建重量异常问题件
function createWeightExceptionProblem(customer_id) {
var message = $("[name=message]").val(), images = [], i = 1;
//去左边空格
message = message.replace(/^\s+/g, "");
//去右边空格
message = message.replace(/\s+$/g, "");
if (message == "备注:") {
editAlertTip("异常描述不能为空");
return false;
}
if ($("[name=product_sku]").val() == "") {
editAlertTip("客户 ID 不能为空");
return false;
}
$("[name='picture[]']").each(function (index, value) {
images[index] = value.value;
i = i + index;
});
$.ajax({
url: "/product/qc/create-qcexception-order",
dataType: "json",
type: "post",
async: false,
data: {
qc_code: $("[name=qc_code]").val(),
ecp_desc: message,
consumerid: customer_id,
exception_img: images,
ecp_type: 170
},
success: function (json) {
if (json.ask) {
$("#createWeightExceptionProblemDialog").dialog("close");
$("#editProductInfoDialog").dialog("close");
}
editAlertTip(json.msg);
}
});
}
Controller
class Default_IndexController extends Zend_Controller_Action {
private $customerId, $productId;
public function __construct($customerId = null){
$this->customerId = $customerId;
$this->productId = $productId;
}
public function uploadPicturesAction() {
$return = array('ask' => 0);
$sizeArr = getimagesize($_FILES['picture']['tmp_name']);
if ($sizeArr[0] > 1024 || $sizeArr[1] > 1024) {
$return['error'] = '每张图片的大小不能超过 1M 且尺寸不能超过 1024 * 1024';
die(json_encode($return));
}
$fileSize = $_FILES['picture']['size'];
//大于1m不允许传
if ($fileSize > 1048576) {
$return['error'] = '每张图片的大小不能超过 1M 且尺寸不能超过 1024 * 1024';
die(json_encode($return));
}
$tempFile = $_FILES['picture']['tmp_name'];
//获得图片存储的临时位置
$Object = new Product_Service_Product();
$targetDir = $Object->getPictureUploadDir($this->customerId, $this->productId);
$ext = substr($_FILES['picture']['name'], strripos($_FILES['picture']['name'], '.'));
$filename = strtolower($this->_request->customerId) . substr(md5($_FILES['picture']['name'] . time()), 0, 11) . $ext;
//$filename = $_FILES['picture']['name']
$targetFile = $targetDir . '/' . $filename;
$targetFileThumb = $targetDir . '/thumb_' . $filename;
if (move_uploaded_file($tempFile, $targetFile)) {
//生成略缩图
Product_Service_Attached::makeThumbnail($targetFile, $targetFileThumb, 75, 75);
$return = array(
'ask' => 1,
'filename' => $filename,
'filepath' => '/default/index/view-image/fileName/' . $filename,
);
} else {
$return['error'] = 'Inner Error';
}
die(json_encode($return));
}
}
Service
class Product_Service_Product {
//获得创建产品图片上传的临时目录
public function getPictureUploadDir($customerId, $productId) {
$hashDir = Product_Service_Attached::tableHash($productId);
$customerDir1 = '/var/www/CFF/front/data/images/' . $customerId . '/' . $hashDir . '/' . $productId;
$customerDir2 = '/var/www/CFF/front/data/images/' . $customerId;
if (!file_exists($customerDir2)) {
if (mkdir($customerDir2)) {
chmod($customerDir2, 0777);
}
}
if (!file_exists($customerDir1)) {
if (mkdir($customerDir1, 0777, true)) {
chmod($customerDir1, 0777);
}
}
return $customerDir1;
}
}
class Product_Service_Attached {
public static function tableHash($k, $m = 5000, $rate = 1) {
//把字符串K转换为1~m之间的一个值作为对应记录的散列地址
$l = strlen($k);
$b = bin2hex($k);
$h = 0;
for ($i = 0; $i < $l; $i++) {
//采用一种方法计算K所对应的整数
$h += substr($b, $i * 2, 2);
}
$hash = ($h * $rate) % $m + 1;
return $hash;
}
/* 只考虑 jpg, png, gif, bmp 格式
* $srcImgPath 源图象路径
* $targetImgPath 目标图象路径
* $targetW 目标图象宽度
* $targetH 目标图象高度
*/
public static function makeThumbnail($srcImgPath, $targetImgPath, $targetW, $targetH) {
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函数不向页面输出错误信息
switch ($imgType) {
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
case 6:
$srcImg = @self::imagecreatefrombmp($srcImgPath);
break;
}
//取源图象的宽高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if ($srcW > $targetW || $srcH > $targetH) {
$targetX = 0;
$targetY = 0;
if ($srcW > $srcH) {
$finaW = $targetW;
$finalH = round($srcH * $finaW / $srcW);
$targetY = floor(($targetH - $finalH) / 2);
} else {
$finalH = $targetH;
$finaW = round($srcW * $finalH / $srcH);
$targetX = floor(($targetW - $finaW) / 2);
}
//function_exists 检查函数是否已定义
//ImageCreateTrueColor 本函数需要 GD2.0.1 或更高版本
if (function_exists("ImageCreateTrueColor")) {
$targetImg = ImageCreateTrueColor($finaW, $finalH);
} else {
$targetImg = ImageCreate($finaW, $finalH);
}
$targetX = ($targetX < 0) ? 0 : $targetX;
$targetY = ($targetX < 0) ? 0 : $targetY;
$targetX = ($targetX > ($finaW / 2)) ? floor($finaW / 2) : $targetX;
$targetY = ($targetY > ($finalH / 2)) ? floor($finalH / 2) : $targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255, 255, 255);
ImageFilledRectangle($targetImg, 0, 0, $finaW, $finalH, $white);
/*
* PHP的GD扩展提供了两个函数来缩放图象:
* ImageCopyResized 在所有 GD 版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。
* ImageCopyResampled 需要 GD2.0.1 或更高版本,其像素插值算法得到的图象边缘比较平滑,
* 该函数的速度比ImageCopyResized慢。
*/
if (function_exists("ImageCopyResampled")) {
ImageCopyResampled($targetImg, $srcImg, 0, 0, 0, 0, $finaW, $finalH, $srcW, $srcH);
} else {
ImageCopyResized($targetImg, $srcImg, 0, 0, 0, 0, $finaW, $finalH, $srcW, $srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg, $targetImgPath);
break;
case 2:
ImageJpeg($targetImg, $targetImgPath);
break;
case 3:
ImagePNG($targetImg, $targetImgPath);
break;
case 6:
ImageJpeg($targetImg, $targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
} else {
//不超出指定宽高则直接复制
copy($srcImgPath, $targetImgPath);
ImageDestroy($srcImg);
}
}
}