This is my form to upload a photo
$form['Background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Choose a background image'),
'#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/',
'#default_value' => $this->options['Background_image'],
);
I tried this function that get the current uploaded image and return its path, but still not working: what is missing:
function image_path()
{
$f = file_load($this->options['Background_image']);
//this too is not working:
//$f = file_load($form_state['values']['Background_image']);
$url_image = file_create_url($f->uri);
print_r($url_image);
return ($url_image);
}
Assuming your value $this->options['Background_image'] is the file id (a.k.a. fid) then the only issue I see with the second set of code is with your print_r statement. file_create_url returns a string, not an object or array, so you only have to print the output.
So replace:
print_r($url_image);
With:
print $url_image;
and it'll output the image's full url (including http) to the browser. Or simply remove the print_r statement if you are returning the value for use in another function.
Related
want to get wp_attachment_metadata in my own way. I want to get the file name :
a:5:{s:5:"width";i:500;s:6:"height";i:500;s:4:"file";s:25:"2016/08/sprite_1500ml.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:4:{s:4:"file";s:25:"sprite_1500ml-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:25:"sprite_1500ml-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:25:"sprite_1500ml-180x180.jpg";s:5:"width";i:180;s:6:"height";i:180;s:9:"mime-type";s:10:"image/jpeg";}s:12:"shop_catalog";a:4:{s:4:"file";s:25:"sprite_1500ml-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:11:"shop_single";a:4:{s:4:"file";s:25:"sprite_1500ml-400x400.jpg";s:5:"width";i:400;s:6:"height";i:400;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
Well, I am stuck. I don’t know how to separate that array. Anyone knows how to separate that array in order I can get the file name (url file).
my code now :
$command = $_GET['command'];
switch ($command) {
case 'list_product':
$loop = new WP_Query(
array(
'post_type' => 'product'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"stock_status" => get_post_meta( get_the_ID(), '_stock_status', true ),
"price" => get_post_meta( get_the_ID(), '_price', true ),
"reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
"image" => get_post_meta( get_the__ID(), '_wp_attachment_metadata', true ),
);
endwhile;
endif;
echo json_encode($meta);
break;
i mean when i use :
"image" => get_post_meta( get_the__ID(), '_wp_attachment_metadata', true ),
my result doesnt show anything
what improvement in my code so its can work like what i want ?
If you want an image url, I show you another way to to this. I suppose you want the post thumbnail of a spicific post.
// First we get the image id
$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// Then we get the image data, we will get an array with some informations
$image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
// the image url is the first index of this array
$image_url = $image[0];
In this example you get the image url for the largeversion. If you want antoher size, just change it to e.g. meidum or full. Read the documentation of wp_get_attachment_image_src() for more details.
This code must be placed inside the loop. Make sure that get_the_ID() returns the current POST ID. Make also sure that the POST have a post thumbnail.
Basically you can get an image url if you have an ID of an attachmend post (image).
I'm using meioupload and everything works well till I try to make thumbnails. Then I can see only kind of phpThumb error message which says
"C:/wamp/cakephp/vendors/phpTmub/img/uploads/product/filename/image.png" does not exist
I'm new with cakePHP so I never faced this problem before. Does anyone know what should I do?
here is my model code:
var $actsAs = array(
'MeioUpload' => array(
'filename' => array(
'create_directory' => true,
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
'allowed_ext' => array('.jpg', '.jpeg', '.png'),
'zoomCrop' => true,
'thumbsizes' => array(
'normal' => array('width' => 400, 'height' => 300),
'small' => array('width' => 80, 'height' => 80,'maxDimension' => '', 'thumbnailQuality' => 100, 'zoomCrop' => true),
),
'default' => 'default.jpg'
)
));
UPDATE:
I found special phpThumb for cakePHP 2.0 so the Path changed into this:
"C:/wamp/cakephp/img/uploads/product/filename/image.png"
and if I open default image in my browser th path is like:
localhost:8080/cakephp/img/uploads/product/filename/image.png
Thanks
I solved the problem, maybe not very elegant, but it works!
For the first time, as I said, I downloaded special version of phpThumb for cakePHP.
Here is link: https://github.com/simkimsia/phpThumb-for-cakephp-2.0
After there was my problem with the path because my images was in folder: C:\wamp\www\cakephp\app\webroot\img\uploads\product\filename\image.png
So I had to find this part of code(begins on line 1078):
if ($this->useCake) {
if ($this->config_document_root != null) {
$AbsoluteFilename = $this->config_document_root.DIRECTORY_SEPARATOR.$filename;
} else {
$AbsoluteFilename = WWW_ROOT.DIRECTORY_SEPARATOR.$filename;
}
}
And edit a the path to fit into my folder:
$AbsoluteFilename = $this->config_document_root.DIRECTORY_SEPARATOR.'cakephp'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.$filename;
$AbsoluteFilename = WWW_ROOT.DIRECTORY_SEPARATOR.'cakephp'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'webroot'.DIRECTORY_SEPARATOR.$filename;
and now its working perfectly...
after try many things.. and finally read the answer of Jan Omacka, i only have to edit phpthumb.class.php, line 223 in the constructor function phpThumb()
First
// public: constructor
function phpThumb() {
.....
//comment line
//$this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
.....
}
//and put the constant CakePHP Var WWW_ROOT (Full path to the webroot.)
$this->config_document_root = WWW_ROOT;
//and if this don't work.. put your real path in linux, or windows
//$this->config_document_root = '/srv/www/htdocs/sic/app/webroot/';
In my case inside webroot, have a folder with estructure 'Usuarios/ID_Prestamo/IDUsuario/', all works great uploading the picture but thumbs not.. so after download the last version of phpThumb, i saw that show an error of File does not exist (URL misformat, for original JPG File), after that download the special versión for CakePhp and change the lines as mentioned.. and all works... I use OpenSuse Linux, Apache2, with Cake 2.4.2
Notes: I verified that if not have zoomCrop it not works, this is my structure
'dir' => 'galeria/', //main folder webroot/galeria/
'createDirectory' => true,
'maxSize'=>'5 Mb',
'allowedExt' => array('.jpg','.jpeg','.png'),
'zoomCrop' => true,
'thumbsizes' => array(
'small' => array('width'=>165, 'height'=>115),
'medium' => array('width'=>800, 'height'=>600)
),
I can't seem to be able to save image files uploaded using the FAPI.
The images are uploaded as managed files, I need to save them permanently but they are always saved in the database with a status of 1.
Any idea what I'm doing wrong? Thanks,
//fapi
$form['fieldset_3']['pic_3'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('pic_3'),
'#description' => t('Allowed extensions: gif png jpg jpeg.'),
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array('MAX_FILE_SIZE' * 1200 * 300),
),
'#theme' => 'image_multifield_multitype',
'#upload_location' => 'public://carousel',
);
//in submit function
$file = file_load($form_state['fieldset_1']['pic_1']);
$file = file_save_upload($file->uri,$validators, 'public://carousel/' . $file->filename, FILE_EXISTS_REPLACE);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
My bad, the problem was the submit handler was not being called...
Load image by the fid and set status to permanent
$file = file_load($form_state['values']['image_fid']);
$file->status = FILE_STATUS_PERMANENT;
$file_saved =file_save($file);
variable_set('image_fid', $file_saved->fid);
I am trying to test a controller function that accepts a json payload.
As per the documentation of testAction() this can be done via setting $options['data'] to the appropriate string. Its not working for me.
See the documentation quoted here: http://api20.cakephp.org/class/controller-test-case (Please scroll down the the testAction() section).
Here is my test case.
public function testCreate(){
//Some code here
$this->testAction('/shippingbatches/create', array('data' => '[3,6]', 'method' => 'post'));
//Some more code here
}
Here is my controller function
public function create(){
debug($this->request); //This debug shows me an empty array in $this->request->data
ob_flush();
$order_ids = json_decode($this->request->data);
//Some more code here
}
The first line of the controller function is showing me an empty array in $this->request->data. If the 'data' passed from the testAction() is an actual array it comes in nice & fine. But not when it is set to a string (unlike it says in the documentation).
Here is the output of the debug.
object(Mock_CakeRequest_ef4431a5) {
params => array(
'plugin' => null,
'controller' => 'shippingbatches',
'action' => 'create',
'named' => array(),
'pass' => array(),
'return' => (int) 1,
'bare' => (int) 1,
'requested' => (int) 1
)
data => array()
query => array(
'case' => 'Controller\ShippingBatchesController'
)
url => 'shippingbatches/create'
base => ''
webroot => '/'
here => '/shippingbatches/create'
}
Please help.
Gurpreet
When passing data like that, you must receive it using CakeRequest::input().
public function create() {
$json = $this->request->input('json_decode', true);
debug($json);
}
I should note that I discovered this by reading Cake's test cases for ControllerTestCase::testAction. Reading test cases can give you insight into how Cake's internals work and give you hints on writing tests.
I have CakePHP 1.3 and I have an action that is supposed to view a file, the file maybe a .pdf, .doc, .... or any document, but I get a blank page instead.
code sample:
public function view_attachments($attachment_id){
$attachment = $this->get_attachment($attachment_id);
if($attachment){
$path = pathinfo($attachment['EmailAttachment']['file']);
$this->view = 'Media';
$this->autoRender = false;
$params = array(
'id' => $path['basename'],
'name' => $path['filename'],
'download' => false,
'mimeType'=> $this->Common->get_mime_content_type($path['basename']),
'extension' => strtolower($path['extension']), // must be lower case
'path' => APP . $path['dirname'] . DS // don't forget terminal 'DS'
);
$this->set($params);
}
}
Any Ideas? Please advise.
check that media.ctp file must be blank... to get proper output...
Thanks
Checking on my own working code, the only difference I see is
$this->autoRender = false;
Of course your not supposed to have a "view_attachments.ctp" file.
Be aware that browser's can't open ".doc" natively. So even if you want him to display it, he will offer you to download it.
function download($id) {
$data = $this->OdmPieceJointe->read(null, $id);
$path = pathinfo($data['OdmPieceJointe']['fichier']);
$this->view = 'Media';
$params = array(
'id' => $path['basename'],
'name' => $path['filename'],
'download' => false,
'extension' => $path['extension'],
'path' => 'uploads/',
);
$this->set($params);
}
maybe :
Configure::write('debug', 0);