// Load the lightbox
application.ptPhotoGallery.scripts.loadJQuery();
application.ptPhotoGallery.scripts.loadADFLightbox();
// Check if the action is defined
if ( NOT StructKeyExists(request.params, "action") )
request.params.action = "form";
appConfig = application.ptPhotoGallery.getAppConfig();
// Config - UPLOADPATH
if ( (StructKeyExists(appConfig, "UPLOAD_PATH")) AND (LEN(appConfig.UPLOAD_PATH)) )
docPath = appConfig.UPLOAD_PATH;
else
docPath = "#request.site.dir#cs_apps/ptPhotoGallery/uploads/";
// Config - UPLOADURL
if ( (StructKeyExists(appConfig, "UPLOAD_URL")) AND (LEN(appConfig.UPLOAD_URL)) )
docURL = appConfig.UPLOAD_URL;
else
docURL = "#request.site.CSAPPSURL#ptPhotoGallery/uploads/";
// Check if a callback is NOT defined
if ( NOT StructKeyExists(request.params, "callback") )
request.params.callback = "";
if ( NOT StructKeyExists(request.params, "category"))
request.params.category = "";
#application.ptPhotoGallery.ui.lightboxHeader()#
// Get the category data structure
catData = application.ptPhotoGallery.photoService.loadCategoryData(request.params.category);
application.ADF.scripts.loadJcrop();
// Load the category data structure
catData = application.ptPhotoGallery.photoService.loadCategoryData(request.params.category);
// Check that we have category data
if ( ArrayLen(catData.INITIALSIZEARRAY) ){
// Check the initial size array
maxWidth = catData.INITIALSIZEARRAY[1].values.width;
maxHeight = catData.INITIALSIZEARRAY[1].values.height;
// Set the aspect ratio
aspectRatio = NumberFormat(catData.INITIALSIZEARRAY[1].values.width/catData.INITIALSIZEARRAY[1].values.height, "_.__");
}
else {
maxWidth = 0;
maxHeight = 0;
aspectRatio = 1;
}
// Default image width
defaultPhotoDim = 700;
// Read the original image
imageData = ImageRead(photoTempDirPath);
//application.ADF.utils.dodump(imageData, "imageData", false);
// Check if the file extension is not PNG
if ( fileExtension NEQ "png" ){
// Create the new image paths
newPhotoTempDirPath = ReplaceNocase(photoTempDirPath,".#fileExtension#", ".png" );
newPhotoTempURLPath = ReplaceNocase(photoTempURLPath,".#fileExtension#", ".png" );
// Convert the image to PNG
ImageWrite(imageData, newPhotoTempDirPath);
// Delete the old image format
fileDeleteOp = application.ptPhotoGallery.csdata.CSFile(action="delete", file="#photoTempDirPath#");
// Store the new image paths back into the paths
photoTempDirPath = newPhotoTempDirPath;
photoTempURLPath = newPhotoTempURLPath;
// Reload the image data for processing
imageData = ImageRead(photoTempDirPath);
}
// Check if the width and height meet the minimum reqs
if ( (imageData.width LT catData.INITIALSIZEARRAY[1].values.width) OR (imageData.height LT catData.INITIALSIZEARRAY[1].values.height) ) {
// Resize the image to be the minimum size to crop
newWidth = catData.INITIALSIZEARRAY[1].values.width;
newHeight = catData.INITIALSIZEARRAY[1].values.height;
// Resize the image
ImageResize(imageData, newWidth, newHeight);
// Update the original to the new croppped image
ImageWrite(imageData, photoTempDirPath);
}
// Check if the width and height are over defaultPhotoDim px
if ( (imageData.width GT defaultPhotoDim) OR (imageData.height GT defaultPhotoDim) ) {
// Determine the current ratio
currAspectRatio = NumberFormat(imageData.width/imageData.height, "_.__");
//application.ADF.utils.dodump(currAspectRatio, "currAspectRatio", false);
// If the width is too large, then rescale based on the width
if ( imageData.width GT defaultPhotoDim ){
newWidth = defaultPhotoDim;
newHeight = Round(newWidth/currAspectRatio);
}
else if ( imageData.height GT defaultPhotoDim ){
// Else the height is too large
newHeight = defaultPhotoDim;
newWidth = Round(newHeight * currAspectRatio);
}
//application.ADF.utils.dodump(newWidth, "newWidth", false);
//application.ADF.utils.dodump(newHeight, "newHeight", false);
// Resize the image
ImageResize(imageData, newWidth, newHeight);
// Update the original to the new croppped image
ImageWrite(imageData, photoTempDirPath);
}
imageData = ImageRead(photoTempDirPath);
// Load Lightbox
application.ADF.scripts.loadADFLightbox();
// Read the original image in
origImg = ImageRead(request.params.photoTempDir);
// Check if the image is the exact size of the crop
if ( (request.params.final_width EQ ImageGetWidth(origImg))
AND (request.params.final_height EQ ImageGetHeight(origImg)) ){
croppedImg = origImg;
}
else {
// Crop out the new image
croppedImg = imageCopy(origImg, request.params.crop_x, request.params.crop_y, request.params.crop_w, request.params.crop_h);
//application.ADF.utils.dodump(croppedImg, "croppedImg - 1", false);
// Check that we have category data
// Resize the cropped image to the category dimensions
//if ( ArrayLen(catData.INITIALSIZEARRAY) )
// ImageResize(croppedImg, catData.INITIALSIZEARRAY[1].values.width, catData.INITIALSIZEARRAY[1].values.height);
if ( (request.params.final_width GT 0) AND (request.params.final_height GT 0) ){
ImageResize(croppedImg, request.params.final_width, request.params.final_height);
//application.ADF.utils.dodump(croppedImg, "croppedImg - 2", false);
}
// Check if the height and width are under the final dimensions
// Resolve the bug in CFIMAGE that will create the image 1 pixel less than defined.
if ( (request.params.final_width GT croppedImg.width) AND (request.params.final_height GT croppedImg.height) ) {
ImageResize(croppedImg, request.params.final_width, request.params.final_height);
//application.ADF.utils.dodump(croppedImg, "croppedImg - 3", false);
}
}
// Update the original to the new croppped image
ImageWrite(croppedImg, request.params.photoTempDir);
// Build the data struct to create the photo record
photoUID = createUUID();
dataStruct = StructNew();
dataStruct.photoID = photoUID;
dataStruct.category = request.params.category;
dataStruct.photo = request.params.photoTempURL;
//application.ADF.utils.dodump(dataStruct);
// Check if a title was defined
if ( LEN(request.params.photoTitle) )
dataStruct.title = request.params.photoTitle;
else
dataStruct.title = "#request.params.categoryTitle# Photo";
// Set the photo caption
dataStruct.caption = request.params.photoCaption;
createStatus = application.ptPhotoGallery.photoDAO.photoCCAPI(dataStruct);
Error cropping and uploading the image. Please try again.