How to get multiple images from a single Drupal node using PHP - php

I have a custom content type in Drupal that allows multiple image uploads through a single field. I want to programmatically access the image URI's, apply my theme, and then get the output one by one. I can do this with a single image like so,
<?php
$image_style_name = 'my_theme';
$image_uri = $entity->field_image['und'][0]['uri'];
$image = theme('image_style', array('style_name' => $image_style_name, 'path' => $image_uri));
$image = image_style_url($image_style_name, $image_uri); ?>
but I am unsure how to access the entire array of images.
For anyone who needs it... the full solution:
<?php
$image_style_name = 'my_theme';
foreach($entity->field_image['und'] as $key => $value){
$image_uri = $entity->field_image['und'][$key]['uri'];
$image = theme('image_style', array('style_name' => $image_style_name, 'path' => $image_uri));
$output = image_style_url($image_style_name, $image_uri);
echo $output;
}
?>

They should be in the $entity->field_image['und'] array so you should be able to loop over the array and theme each one with something like
foreach($entity->field_image['und'] as $image_field){
..etc
}

Related

How to integrate manifest.json into wordpress theme

I have searched the internet, but the results I have seen are not clear enough.
I was able to add a manifest.json file into my wordpress theme by adding this code to the functions.php file.
//manifest file
add_action( 'wp_head', 'inc_manifest_link' );
// Creates the link tag
function inc_manifest_link() {
echo '<link rel="manifest" href="'.get_template_directory_uri().'/manifest.json">';
}
The manifest.json is as it should be.
However I wish to use some theme_mod functions within the manifest file. Which I can't do without php.
So is there anyway to write php code within the manifest.json file.
Thanks in advance.
So you can do something like this:
<?php
$filename = "manifest.json";
// Grab contents and decode them into an array
$data = json_decode(file_get_contents($filename), true);
//this is where you're adding your content so below you can write to the file
//Creating new var
//(make sure no var has the name "['newnewname']" unless you want to edit it)
$data['newname'] = 'Adding NEW contents';
//Creating new 2 dimensional array
$data['newname'] = array("another1" => "val1", "another2" => "val2");
//adding content to an existing 2 dimensional array
$data['exists'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 3 dimensional array
$data['exists']['name'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 4 dimensional array
$data['exists']['name']['name'] += array("another1" => "val1", "another2" => "val2");
// encode back into json
$jencode = json_encode($data);
// write over file with new contents
$fopen = fopen($filename, "w");
if(fwrite($fopen, $jencode)){
echo('Success!<hr>');
echo(file_get_contents($filename));
}
fclose($fopen);
Heres a live example (with fopen/write/read stuff removed):
Advanced: http://sandbox.onlinephpfunctions.com/code/eacdd6b8254c2127521769461d5f6d9daeda224d
Simple: http://sandbox.onlinephpfunctions.com/code/3589151881aac2e442738b381c05a37240081901

Echo file contents inside params array

I'm trying to finish creating a php keygen for my product.
Is it possible for me to echo a text value inside on array (specifically the HWID which is taken from an uploaded text file)?
<?php
$file = file_get_contents('text.txt');
$params = array(
hwid => "echo $file;", // Exactly as returned by VMProtectGetCurrentHWID
user_data => base64_decode("CGCvRvMWcPHGdMjQ"), // string of bytes
);
?>
Is there a better way of doing it?
You don't need to echo just do this:
<?php
$file = file_get_contents('text.txt');
$params = array(
hwid => $file, // Exactly as returned by VMProtectGetCurrentHWID
user_data => base64_decode("CGCvRvMWcPHGdMjQ"), // string of bytes
);

update existing move_uploaded_file code to handle an array instead of a single image upload

I have extended functionality of banner manager to allow for it to be multi language. This gives the user the opportunity to have language specific title, html banner, or image banner.
All the data is correctly being saved to the database now, but I can only ever get the first image of the three to be uploaded to the specified directory.
The original two code lines that uploaded the images are
if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
chmod($this->destination . $this->filename, $this->permissions);
I thought that changing it to the following word work, but it still only sends a single image.
$languages = zen_get_languages();
for ($i=0, $n = sizeof($languages); $i<$n; $i++) {
$language_id = $languages[$i]['id'];
if (move_uploaded_file($this->file['tmp_name'][$language_id] , $this->destination . $this->filename[$language_id])) {
chmod($this->destination . $this->filename[$language_id], $this->permissions);
}
I can see that the correct data is available for the uploads from the following print_r commands
print_r($this->file['tmp_name']);
results in
Array ( [2] => /Applications/MAMP/tmp/php/phpVr9JSd [1] => /Applications/MAMP/tmp/php/phpGpYRG9 [4] => /Applications/MAMP/tmp/php/phpfbOoQ4 )
and
print_r($this->destination);
results in
/Applications/MAMP/htdocs/confetti/images/banners/
and
print_r($this->filename);
results in
Array ( [2] => stislide1.jpg [1] => stislide2.jpg [4] => stislide3.jpg )
You may be wondering why I made the assumption that putting [$language_id] into the move_uploaded_file command would work. That thought was based on the fact that the code I used to upload the correct path/name information to the database was
for ($i=0, $n = sizeof($languages); $i<$n; $i++) {
$language_id = $languages[$i]['id'];
$db_image_location = (zen_not_null($banners_image_local_array[$language_id])) ? $banners_image_local_array[$language_id] : $banners_image_target . $banners_image->filename[$language_id];
Obviously what works as part of the mysqli upload doesn't work in the class file where this upload function is added.
Any pointers on where I've gone wrong here would be appreciated.
The answer was to rewrite the code so that the array contents were handled by a foreach
foreach($this->file['tmp_name'] as $key => $tmp_name){
$file_name = $this->filename[$key];
$file_tmp =$this->file['tmp_name'][$key];
move_uploaded_file($file_tmp, $this->destination. $file_name);
chmod($this->destination . $file_name, $this->permissions);
}
This is now uploading however many images are in the array

programatically insert content with image field in Drupal

I'm trying to write PHP to insert lots of content into my website's drupal 6 database.
The content type has got 3 custom fields:
field_theme_preview_demo_link - text field
field_theme_preview_content_styl - text selector
field_theme_preview_thumb - image field with an 'alt'
I just copied the images over to the correct directory manually.
I created the content type (and manually inserted some content to confirm it was all ok).
I then found some code sample for programatically inserting content...
Creating Drupal CCK content programatically/ via API
and
http://drupal.org/node/458778#comment-1653696
(for this one I couldnt understand the mod that is suggested for D6).
I tried to modify it but couldnt get it to work - it does create the new content but the image field is blank. (To run this code I just paste into the 'Execute PHP Code' block.)
global $user;
$newnode = new stdClass();
$newnode->title = 'New node title';
$newnode->body = "the body";
$newnode->uid = $user->uid;
$newnode->type = 'theme_preview';
$newnode->status = 1;
$newnode->promote = 0;
$newnode->active = 1;
$newnode->field_theme_preview_demo_link[0]['value'] = 'the link';
$newnode->field_theme_preview_content_styl[0]['value'] = 'Books';
$newnode->field_theme_preview_thumb = array(
array(
'fid' => 'upload',
'alt' => 'the alt',
'filename' => 'sites/default/files/theme_preview_thumbs/41531.jpg',
'filepath' => 'sites/default/files/theme_preview_thumbs',
'filesize' => filesize('sites/default/files/theme_preview_thumbs/41531.jpg'),
),
);
node_save($newnode);
Setting the fid field to 'upload' is something that is done by a node's form when there actually is a file being uploaded. If you are programatically generating content, you will need to fetch the fid from somewhere else.
You will probably need to manually use the file api to enter a file from somewhere on the filesystem into the file database, and then draw your fid from the resulting object.
Use http://drupal.org/project/node_export this module to import or export content to particular content type, it really works for me and also image will be uploaded automatically.
One thing you have to consider for using this module, after importing nodes, make sure that images are matched with its content.
Using this module i have uploaded 100s of news article to my website from my local server site.
#Seth Battin, # Ayesh K, thanks for the tips,
working code...
global $user;
$newnode = new stdClass();
$newnode->title = 'New node title';
$newnode->body = "this is a new node, created by import function";
$newnode->uid = $user->uid;
$newnode->type = 'theme_preview';
$newnode->status = 1;
$newnode->promote = 0;
$newnode->active = 1;
$newnode->field_theme_preview_demo_link[0]['value'] = 'test 1';
$newnode->field_theme_preview_content_styl[0]['value'] = 'Books';
$field = field_file_save_file(
'sites/default/files/srcImage1.jpg',
array(),
'sites/default/files/theme_preview_thumbs');
if( !isset($field['data']) ) $field['data'] = array();
$field['data']['title'] = "the image title";
$field['data']['alt'] = "the image alt";
$newnode->field_theme_preview_thumb = array(0 => $field);
$newnode = node_submit($newnode);
node_save($newnode);

Upload Multiple Files with FuelPHP

I currently have a script that allows me to upload 1 file to my server. It works great.
Here is a portion of the code I am using to do this:
// Custom configuration for this upload
$config = array(
'path' => DOCROOT.DS.'foldername/tomove/your/images',
'randomize' => true,
'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
);
Upload::process($config);
// if a valid file is passed than the function will save, or if its not empty
if (Upload::is_valid())
{
// save them according to the config
Upload::save();
//if you want to save to tha database lets grab the file name
$value = Upload::get_files();
$article->filename = $value[0]['saved_as'];
}
I was now wondering, how do I loop through multiple files and upload these to my server?
I'm guessing using a foreach loop but I'm a little out of my depth with this I'm afraid.
Ultimately, I plan to store these filenames in a separate table on my database.
Many thanks for any help with this.
You already have the result in your code.
You already store it
$value = Upload::get_files();
so
$value = Upload::get_files();
foreach($value as $files) {
print_r($files);
}
And you will get everything what you need

Categories