if i upload image then edit work fine but if i update other field and not image that time it display array to sting erro in cakephp2.4.5 - edit

it will display error if i update other filed and not image.
public function edit($id = null) {
$this->helpers = array('TinyMCE.TinyMCE');
$this->layout = 'adminpanel';
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$this->layout = 'adminpanel';
//save data
if ($this->request->is(array('post', 'put'))) {
$this->Tour->id = $id;
//Save image
if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
$fileNameFull = $this->request->data['Tour']['varbigimg']['name'];
$uploadFolder = "upload";
//full path to upload folder
$uploadPath = WWW_ROOT . $uploadFolder;
$oldFile = $uploadPath.'/'.$fileNameFull;
move_uploaded_file(
$this->request->data['Tour']['varbigimg']['tmp_name'],$oldFile
);
$newFile = WWW_ROOT.'courseImages/thumb/'.$fileNameFull;
$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);
$this->request->data['Tour']['varbigimg'] = $fileNameFull;
}
else{//Img not uploaded
$this->request->data['Tour']['vartitle']= $this->data['Tour']['vartitle'];
$this->request->data['Tour']['varsubtitle']= $this->data['Tour']['varsubtitle'];
$this->request->data['Tour']['txtsortdesc']= $this->data['Tour']['txtsortdesc'];
$this->request->data['Tour']['txtdeasc']= $this->data['Tour']['txtdeasc'];
$this->request->data['Tour']['vardeparts']= $this->data['Tour']['vardeparts'];
$this->request->data['Tour']['decadultprice']= $this->data['Tour']['decadultprice'];
$this->request->data['Tour']['decchildprice']= $this->data['Tour']['decchildprice'];
$this->request->data['Tour']['varimgtitle']= $this->data['Tour']['varimgtitle'];
$this->request->data['Tour']['enumstatus']= $this->data['Tour']['enumstatus'];
$this->request->data['Tour']['id']= $this->data['Tour']['id'];
//In this way do for All Except Image.
}
// pr($this->$this->request->data);
if ($this->Tour->save($this->request->data)) {
$this->Session->setFlash(__('Unable to add your schedule.'));
//Save image
$this->Session->setFlash(__('Your tour has been updated.'));
return $this->redirect(array('controller'=>'admin','action' => 'tour'));
$this->Session->setFlash(__('Unable to update your Tour.'));
}
}
$tour = $this->Tour->findByid($id);
if (!$tour) {
throw new NotFoundException(__('Invalid post'));
}
if (!$this->request->data) {
$this->request->data = $tour;
}
}
my cont code
my view is below. so when i upload image it will work fine. but in edit if i dont upload image then it display array to sting error. means it not take ast image. thanks
echo $this->Form->create('Tour',array('autocomplete' => 'off','enctype'=>'multipart/form-data'));
echo $this->Form->input('varbigimg',array('type' => 'file'));?>

Write Else for
if(is_uploaded_file($this->request->data['Tour']['varbigimg']['tmp_name']))
{
.........
}else{
//**TWO GOLDEN LINES OF YOUR LIFE**
$tourForImg = $this->Tour->findByid($id);
$this->request->data['Tour']['varbigimg'] = $tourForImg['Tour']['varbigimg'];
//**TWO GOLDEN LINES OF YOUR LIFE**
//AS Img not uploaded by user
//Write All data EXPLICITELY that you want to save WITHOUT Image.
$this->request->data['Tour']['Tourname']= $this->data['Tour']['Tourname'];
$this->request->data['Tour']['YourFormField1']= $this->data['Tour']['YourFormField1'];
$this->request->data['Tour']['YourFormField2']= $this->data['Tour']['YourFormField2'];
//In this way do for All Except Image.
}

Related

Photoshop Scripting: Relink Smart Object

I'm working on a script that should go through a photoshop document and relink all visible linked objects to a new specified file. I've gotten the loop working so that it cycles through every layer and collects only the visible layers, but for the life of me I can't find if there's a method available to relink a smart object. The closest I've found is this script:
https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c
but when it gets to desc3.putPath(idnull, new File(newFile));, it spits out an error indicating that the functionality may not be present in the current Photoshop version. The script itself is 4 years old so it may be out of date.
Any help would be appreciated!
MY script as it stands is below:
// SELECT FILE //
var files = File.openDialog("Please select new linked file");
var selectedFile = files[0];
// GET ALL LAYERS //
var doc = app.activeDocument;
var allLayers = [];
var allLayers = collectAllLayers(doc, allLayers);
function collectAllLayers (doc, allLayers)
{
for (var m = 0; m < doc.layers.length; m++)
{
var theLayer = doc.layers[m];
if (theLayer.typename === "ArtLayer")
{
allLayers.push(theLayer);
}
else
{
collectAllLayers(theLayer, allLayers);
}
}
return allLayers;
}
// GET VISIBLE LAYERS //
var visibleLayers = [];
for (i = 0; i < allLayers.length; i++)
{
var layer = allLayers[i];
if (layer.visible && layer.kind == LayerKind.SMARTOBJECT)
{
visibleLayers.push(layer);
}
}
// REPLACE LAYERS
for (i = 0; i < visibleLayers.length; i++)
{
var layer = visibleLayers[i];
//--> REPLACE THE FILE HERE
}
Note: I am aware that this script currently may be error-prone if you don't know exactly how it works; I'm not intending to publish it at this time so I'm not super concerned with that at the moment. Mostly I just need the core functionality to work.
I used an AM function for getting visible smart objects — it works much faster. But if you want you can use yours. The important bit is relinkSO(path);: it'll also work in your script (just don't forget to select a layer: activeDocument.activeLayer = visibleLayers[i];)
Note that it works similar to Photoshop Relink to File command — if used on one instance of Smart Object all the instances are going to be relinked. If you want to relink only specific layers you'll have to break instancing first (probably using the New Smart Object via Copy command)
function main() {
var myFile = Folder.myDocuments.openDlg('Load file', undefined, false);
if (myFile == null) return false;
// gets IDs of all smart objects
var lyrs = getLyrs();
for (var i = 0; i < lyrs.length; i++) {
// for each SO id...
// select it
selectById(lyrs[i]);
// relink SO to file
relinkSO(myFile);
// embed linked if you want
embedLinked()
}
function getLyrs() {
var ids = [];
var layers, desc, vis, type, id;
try
{
activeDocument.backgroundLayer;
layers = 0;
}
catch (e)
{
layers = 1;
}
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try
{
desc = executeActionGet(ref);
}
catch (err)
{
break;
}
vis = desc.getBoolean(charIDToTypeID("Vsbl"));
type = desc.getInteger(stringIDToTypeID("layerKind"));
id = desc.getInteger(stringIDToTypeID("layerID"));
if (type == 5 && vis) ids.push(id);
layers++;
}
return ids;
} // end of getLyrs()
function selectById(id) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
desc.putReference(charIDToTypeID('null'), ref);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
} // end of selectById()
function relinkSO(path) {
var desc = new ActionDescriptor();
desc.putPath( charIDToTypeID('null'), new File( path ) );
executeAction( stringIDToTypeID('placedLayerRelinkToFile'), desc, DialogModes.NO );
} // end of relinkSO()
function embedLinked() {
executeAction( stringIDToTypeID('placedLayerConvertToEmbedded'), undefined, DialogModes.NO );
} // end of embedLinked()
}
app.activeDocument.suspendHistory("relink SOs", "main()");

Extendscript with Photoshop: Importing an image

I'm trying to select some image files via a prompt, then add those files to the active document. Here's what I have so far:
#target photoshop
doc = app.activeDocument;
// choose image files
var files = File.openDialog(undefined,undefined,true);
// for each image, add to new layer and insert into doc
for (var file in files) {
var layer = doc.artLayers.add();
layer.image = file; // this doesn't work.
}
What's layer.image? ArtLayer doesn't have this property. Maybe placing will work better in your case:
doc = app.activeDocument;
// choose image files
var files = File.openDialog(undefined, undefined, true);
// for each image, add to new layer and insert into doc
for (var i = 0; i < files.length; i++)
{
var layer = placeImage(files[i]);
}
function placeImage(imageFile)
{
var desc554 = new ActionDescriptor();
desc554.putPath(cTID('null'), imageFile);
desc554.putEnumerated(cTID('FTcs'), cTID('QCSt'), cTID('Qcsa'));
var desc555 = new ActionDescriptor();
desc555.putUnitDouble(cTID('Hrzn'), cTID('#Pxl'), 0.000000);
desc555.putUnitDouble(cTID('Vrtc'), cTID('#Pxl'), 0.000000);
desc554.putObject(cTID('Ofst'), cTID('Ofst'), desc555);
executeAction(cTID('Plc '), desc554, DialogModes.NO);
return activeDocument.activeLayer
};
function cTID(s)
{
return app.charIDToTypeID(s);
};
function sTID(s)
{
return app.stringIDToTypeID(s);
};
Also Photoshop doesn't like when for...in loops are used on Array (sometimes it works, sometimes it doesn't: in case of File object it doesn't work)

Replacing smart objects in bulk with Photoshop

Just facing this issue: I have a mockup in Photoshop with two smart-objects: Rectangle 14.psb and Place your logo.psb
I have 100+ images in png that should be applied to create mockups.
For this reason, I would like your help to create a script that:
Let me select the png file that I would like to use
Open the smart objects (Rectangle 14.psb and Place your logo.psb)
Re-Link the same png to the layers "place your logo" of both the smart objects.
Finally, the script should save the file as png with the same file name of the selected png file adding just _new after its name.
So far I have tried this code without any luck:
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// PSD Options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// Check if layer is SmartObject;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {
// Select Files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files",
"*.psd;*.tif;*.jpg;*.png", true)
} else {
var theFiles = File.openDialog("please select files", getFiles,
true)
};
if (theFiles) {
for (var m = 0; m < theFiles.length; m++) {
// Replace SmartObject
theLayer = replaceContents(theFiles[m], theLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
// Save JPG
myDocument.saveAs((new File(thePath + "/" + theName + "_" +
theNewName + ".psd")), psdOpts, true);
}
}
}
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|png|jpg)$/i) != null ||
theFile.constructor.name == "Folder") {
return true
};
};
// Replace SmartObject Contents
function replaceContents(newFile, theSO) {
app.activeDocument.activeLayer = theSO;
// =======================================================
var idplacedLayerReplaceContents =
stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
};
The above code substitute the smart object but I would like just to re-link the layer withing the smartobject to a new image and save the file. Any help would be much appreciated!
Are you familiar with Scriptlistener? You can use it to get all the functions you need and then modify the output to run within your loop of 100 pngs, it should be straightforward.

Make OCG layer visible when field isn't empty?

Is it possible to check when a field is not empty and if not, make an ocg layer visible?
var ocg = FindOCG("Item 1 Arrow");
if (+event.value === '') {
ocg.state = !ocg.state;
} else {
ocg.state = !ocg.state;
}
Something like this (which doesn't work)!
Put this in the custom format script of the field in question. Replace "Square" with the name of your layer. You can see an example of it working here.
function getOCGByName(cName, nPage) {
var ocg = null;
var ocgArray = this.getOCGs(nPage);
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name == cName) {
ocg = ocgArray[i];
}
}
return ocg;
}
var field = event.target;
var ocg = getOCGByName("Square", this.pageNum);
if (field.value.length > 0) {
ocg.state = true;
}
else {
ocg.state = false;
}
Note: This will only work in Adobe Acrobat and Reader and a few other viewers that are JavaScript capable.

Mask over image

I want to replace the transparent pixels of a image with a mask, I'm using this function but I keep getting errors.
When I try:
<?php
function image_mask($src, $mask)
{
imagesavealpha($src, true);
imagealphablending($src, false);
// scan image pixels
// imagesx = get image width
for ($x = 0; $x < imagesx($src); $x++) {
// imagesy = get image height
for ($y = 0; $y < imagesy($src); $y++) {
$mask_pix = imagecolorat($mask,$x,$y);
//return r,g,b,alpha
$mask_pix_color = imagecolorsforindex($mask, $mask_pix);
if ($mask_pix_color['alpha'] < 127) {
$src_pix = imagecolorat($src,$x,$y);
$src_pix_array = imagecolorsforindex($src, $src_pix);
imagesetpixel($src, $x, $y, imagecolorallocatealpha($src, $src_pix_array['red'], $src_pix_array['green'], $src_pix_array['blue'], 127 - $mask_pix_color['alpha']));
}
}
}
}
image_mask('source.png', 'mask.png');
?>
I get the following errors:
Warning: imagesavealpha() expects parameter 1 to be resource, string given in ... on line 7
Warning: imagealphablending() expects parameter 1 to be resource, string given in ... on line 8
Warning: imagesx() expects parameter 1 to be resource, string given in ... on line 11
I tried adding imageCreateFromPng and header('Content-Type: image/png'); to the images but then I just get a empty page.
i don`t know what result were you want get , and i feel the page show is wrong . you can try run the program
<?php
header('Content-Type: image/png');
function image_mask(&$src, &$mask)
{
imagesavealpha($src, true);
imagealphablending($src, false);
// scan image pixels
// imagesx = get image width
for ($x = 0; $x < imagesx($src); $x++) {
// imagesy = get image height
for ($y = 0; $y < imagesy($src); $y++) {
$mask_pix = imagecolorat($mask,$x,$y);
//return r,g,b,alpha
$mask_pix_color = imagecolorsforindex($mask, $mask_pix);
if ($mask_pix_color['alpha'] < 127) {
$src_pix = imagecolorat($src,$x,$y);
$src_pix_array = imagecolorsforindex($src, $src_pix);
imagesetpixel($src, $x, $y, imagecolorallocatealpha($src, $src_pix_array['red'], $src_pix_array['green'], $src_pix_array['blue'], 127 - $mask_pix_color['alpha']));
}
}
}
}
$src = imagecreatefrompng('source.png');
$mask = imagecreatefrompng('mask.png');
image_mask($src, $mask);
imagepng($src);
imagedestroy($src);
imagedestroy($mask);
?>
“imagesavealpha() expects parameter 1 to be resource, string given”
the gaved param 1 of imagesavealpha is wrong,
it need resource,the resource may imagecreatetruecolor/imagecreatefrompng create