using image path returned by drupal 7 - php

am unable to use the path returned by drupal 7 for an image which was uploaded for custom content type. the path returned is as follow:
Array ( [und] => Array ( [0] => Array ( [fid] => 6 [alt] => [title] => [width] => 464 [height] => 261 [uid] => 1 [filename] => _71135631_c0166859-snotty_child_with_a_cold-spl.jpg [uri] => public://_71135631_c0166859-snotty_child_with_a_cold-spl.jpg [filemime] => image/jpeg [filesize] => 22168 [status] => 1 [timestamp] => 1384797114 [rdf_mapping] => Array ( ) ) ) )
but when using the vale of uri, the image is not shown.
any assistance appreciated.
regards,
a.ali

Such uri-fields in Drupal always refer to the internal data-storage logic, hence the public:// at the beginning. Drupal uses Image Styles for any images attached to content.
To retrieve the full URL for an image, you should use image_style_url():
$imgPath = image_style_url('default', $object['und'][0]['uri']);

Related

How can I get a specific value in this PHP array?

I'm setting up a Cloudinary listing of uploaded videos and I want to get the metadata of that video.
I have a print_r($result):
<pre>
Cloudinary\Api\Response Object
(
[rate_limit_reset_at] => 123123123
[rate_limit_allowed] => 123
[rate_limit_remaining] => 123
[storage:ArrayObject:private] => Array
(
[public_id] => dog
[format] => mp4
[version] => 123123123
[resource_type] => video
[type] => upload
[created_at] => 2019-07-29T07:32:50Z
[bytes] => 123123
[width] => 321
[height] => 456
[backup] => 1
[access_mode] => public
[url] => http://res.cloudinary.com/demo/video/upload/dog.mp4
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[next_cursor] => 123123123
[derived] => Array
(
[0] => Array
(
[transformation] => /jpg
[format] => jpg
[bytes] => 86438
[id] => 123123123
[url] => http://res.cloudinary.com//demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[extension] => jpg
)
[1] => Array
(
[transformation] => t_media_lib_thumb/jpg
[format] => jpg
[bytes] => 3293
[id] => 12123123
[url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[extension] => jpg
)
)
)
)
</pre>
I tried getting the value using this format but it says undefined index. echo $result[4]['width'] or echo $result['storage:ArrayObject:private']['width'];
I talked to a cloudinary support ad he said that "you should be able to access the data returned within [storage:ArrayObject:private] by simply ignoring it and requesting the index that you desire directly from $result. Meaning you can use or and so on with the other paramters."
So basically, just removing the index solved my problem like so:
<?php echo $result['height']; ?> <?php echo $result['width']; ?>
Have a look at this https://github.com/cloudinary/cloudinary_php/blob/master/src/Api/Response.php in order to better understand the object you are getting back.
Afterwards, look at the ArrayObject documentation here https://www.php.net/manual/en/class.arrayobject.php, as your Response object extends this class. Using getArrayCopy() to transform your object into an array should be what you need.
Bear in mind that I am not familiar with the API and that there may be better options to get what you need.

How to get pin image using pinterest api in php?

I have all the boards of logged in user and using pinterest api i am able to fetch the pins from given board but when it returns the json there is no image link, the image field return as null. So how to get image with oin content. Here is json retrieved.
Array ( [data] => Array ( [0] => Array ( [id] => 400327854358866609 [link] => https://www.pinterest.com/r/pin/400327854358866609/4837342446222323658/8737cad9ec4b46439289fbd4f22353f02255549d33a8999691d82b8642f42625 [url] => https://www.pinterest.com/pin/400327854358866609/ [creator] => [board] => [created_at] => [note] => Hello this is simple test [color] => [counts] => [media] => [attribution] => [image] => [metadata] => [original_link] => ) [1] => Array ( [id] => 400327854358814014 [link] => https://www.pinterest.com/r/pin/400327854358814014/4837342446222323658/3f1103b8d1c97de21f913293ee6062f4122313c931b054764167e96745f64465 [url] => https://www.pinterest.com/pin/400327854358814014/ [creator] => [board] => [created_at] => [note] => 55 Awesome Men’s Tattoos | InkDoneRight We’ve collected 55 Awesome Different Mens Tattoo Designs to inspire you! We also have the meaning and symbolism behind the common men’s tattoo designs... [color] => [counts] => [media] => [attribution] => [image] => [metadata] => [original_link] => ) ) [page] => )
In above json image or media key has no values.
If you use the "API explorer", and add the "image" field, one of the outputs is the URL of the original image.
https://api.pinterest.com/v1/boards/BOARD_OWNER/BOARD_NAME/pins/?access_token=MY_TOKEN&fields=id%2Clink%2Cnote%2Curl%2Cattribution%2Cboard%2Ccolor%2Ccreated_at%2Ccreator%2Cimage%2Cmedia%2Cmetadata%2Coriginal_link%2Ccounts
Or using the JS SDK eg:
PDK.request(thisRequest, {fields: 'id,note,link,url,image'}, thisCmd,
function( PDKdata){};
See my example here:
Example of using Pinterest API

php array building with foreach

I'm making a class that lists files of a given extension inside a folder.
the trouble I'm having is that some files are images and I want to extract more information from those files and not for files that are not images.
I created this foreach that adds an entry to an array. later that array is returnd and contains all the files:
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? array("info"=>getimagesize($filename)) : null
);
As i said earlier, this works.
the problem that I want to correct is the output:
Array
(
[0] => Array
(
[basename] => 1.JPG
[size] => 56533
[extension] => jpg
[file] => folder\1.JPG
[path] => folder
[0] => Array
(
[info] => Array
(
[0] => 897
[1] => 616
[2] => 2
[3] => width="897" height="616"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
)
)
[1] => Array
(
[basename] => 2.JPG
[size] => 56533
[extension] => jpg
[file] => folder\2.JPG
[path] => folder
[0] => Array
(
[info] => Array
(
[0] => 897
[1] => 616
[2] => 2
[3] => width="897" height="616"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
)
)
)
the problem I want to correct is that I want to access the file info like $file_array[0]['info'] and not $file_array[0][0]['info']. That second index is useless since there can be only one info for a file.
Any way to correct this?
Then don't add an array around your info key:
'info' => in_array(...) ? getimagesize($filename) : null
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
"info" => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : array()
);
This should work
Your keys are incorrect. Try this. Note that I've added info as an index which sets the value from the condition.
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
'info' => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : null
);

How to display php array as HTML?

I am working with the Yahoo BOSS API to build an image search for motherpipe.co.uk.
I have managed to create a valid request for image listings and have received the response. My problem is that I don't understand how I can use the different elements of that response to build my nice-looking page of image listings.
Ideally I want to loop through the array and display a thumbnail and link for each item in the list but somehow I can't abstract the relevant bits from the $results.
The (example) output from the query with two listings is in this $results:
stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [images] => stdClass Object ( [start] => 0 [count] => 2 [totalresults] => 107000 [results] => Array ( [0] => stdClass Object ( [clickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [size] => 191.8KB [format] => jpeg [height] => 586 [refererclickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [refererurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [title] => Stockholm is a beautiful city with Lake Mälaren on it’s WestSide ... [url] => http://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [width] => 793 [thumbnailheight] => 118 [thumbnailurl] => htt://ts4.mm.bing.net/th?id=H.4970051277687231&pid=15.1&H=118&W=160 [thumbnailwidth] => 160 ) [1] => stdClass Object ( [clickurl] => http://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [size] => 2.3MB [format] => jpeg [height] => 1272 [refererclickurl] => htt://summerventures.wordpress.com/tag/stockholm/ [refererurl] => htt://summerventures.wordpress.com/tag/stockholm/ [title] => stockholm | Summer adventures [url] => htt://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [width] => 1800 [thumbnailheight] => 113 [thumbnailurl] => htt://ts2.mm.bing.net/th?id=H.4581116279128437&pid=15.1&H=113&W=160 [thumbnailwidth] => 160 ) ) ) ) )
Question:
What approach can I use to simply display a thumbnail picture with a link from each of the listings in this array, using the info in [thumbnailurl] and [refererclickurl] kind of like
<div> <<a href="[refererclickurl]"><img src="[thumbnailurl]" alt="alt text" /> </div>
I think I need a for each approach but after having tried for three days I can't make it work.
Any help much appreciated.
Try the following. The quoted object in your question is in the $o variable.
$html = '';
$images = $o->bossresponse->images->results;
foreach ($images as $image) {
$html .= "<a href='{$image->refererclickurl}'><img src='{$image->thumbnailurl}'></a>";
}
echo $html;
Note that if $obj->prop equals 'foo' then "{$obj->prop} is not bar" would equal 'foo is not bar'.
Its also good to note that the html would be better if you also output the image dimensions and may change depending on what html spec you're implementing.

getting story tags from graph api returns strange id results for the tagged

The best way for me to explain this is to show you. Seems like a float() error in a 64bit system.
when i call /anotherfeed/feed or any page for that matter, posts with story_tags return some of the id's as a float error.
sample story tag with float error in id. [id] => 1.7153566624E+14
My question is, how do i fix this, or what am i doing wrong? all i am doing is looping in a foreach statement.
if($fvalue[story_tags]){
echo 'Tags: ';
$sTags=$fvalue[story_tags];
foreach ($sTags as $skey=>$svalue){
foreach ($svalue as $gkey=>$hvalue){
$id=$hvalue[id];
echo ''.$hvalue[name].' '.$id.' ';
}
}
}
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
Array
(
[data] => Array
(
[0] => Array
(
[id] => 171535666239724_156133294510726
[from] => Array
(
[name] => Another Feed
[category] => App page
[id] => 171535666239724
)
[story] => Another Feed shared Non-Profits on Facebook's photo.
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
[20] => Array
(
[0] => Array
(
[id] => 41130665917
[name] => Non-Profits on Facebook
[offset] => 20
[length] => 23
[type] => page
)
)
)
[picture] => http://photos-d.ak.fbcdn.net/hphotos-ak-ash3/557037_10150932300320918_1908237167_s.jpg
[link] => http://www.facebook.com/photo.php?fbid=10150932300320918&set=a.85612830917.95996.41130665917&type=1
[name] => Wall Photos
[caption] => Have you heard of the #[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by #[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.
During the 2012 campaign, #[20531316728:274:Facebook] drove 20% of traffic to the ODWS microsite and TOMS even launched a Facebook-exclusive "Barefoot & Blue" giveaway with #[25266987484:274:Essie Nail Polish] for the second year in a row.
We think this is a pretty cool example of creating exclusive content around an important initiative that keeps people engaged and involved!
[properties] => Array
(
[0] => Array
(
[name] => By
[text] => Non-Profits on Facebook
[href] => http://www.facebook.com/nonprofits
)
)
[icon] => http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[type] => photo
[object_id] => 10150932300320918
[application] => Array
(
[name] => Photos
[id] => 2305272732
)
[created_time] => 2012-07-02T17:57:23+0000
[updated_time] => 2012-07-02T17:57:23+0000
[comments] => Array
(
[count] => 0
)
)
solution:
cURL - had to use number format with PHP_EOL to solve in cURL.
// $locs = curl call to graph api for /anotherfeed/feed, still need solution for foreach.
$locs=json_decode($returned, true);
$stId=number_format($locs[data][1][story_tags][0][0][id], 0, '', '').PHP_EOL;
echo $stId;
PHP-SDK
solution is same, long numbers in the foreach loop need to be ran through number_format.

Categories