php-general Digest 4 Oct 2012 14:54:26 -0000 Issue 7992
Topics (messages 319342 through 319355):
Re: generate a thumbnail with imagick and place a logo on top
319342 by: tamouse mailing lists
Differences
319343 by: David McGlone
319344 by: admin
319345 by: Timmy Sjöstedt
319346 by: admin
319347 by: David McGlone
319348 by: James
319349 by: David McGlone
319350 by: David McGlone
319351 by: tamouse mailing lists
319352 by: tamouse mailing lists
319353 by: tamouse mailing lists
319354 by: Rosalind Wills
cURL issues posting to an end point
319355 by: Bastien Koert
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On Wed, Oct 3, 2012 at 10:54 AM, A Freund <ad-fre...@web.de> wrote:
> Hello,
>
> I have a problem creating thumbnails with imagick. The code is working
> ok and the thumbnail is generated in the right size etc but when I try
> to place a PDF logo on the thumbnail it turns half transparent. I guess
> it has something to do with that the PDF file is generated in InDesign
> and probably hasn't any background defined. Has anyone come across this
> problem or has an idea what to do about it? I tried to put a white
> canvas in the background but that didn't help. I also specified a
> channel for the compositeImage function but that didn't help either.
>
> This is the PDF file I'm having issues with:
> https://dl.dropbox.com/u/13712643/Case_Study.pdf
> <http://95.119.206.251/Case_Study.pdf>
> The generated Thumbnail looks like this:
> https://dl.dropbox.com/u/13712643/Case_Study1.jpg
> <http://95.119.206.251/Case_Study1.jpg>
>
> The code I have produced so far: http://pastebin.com/74CYC972
> <http://pastebin.com/74CYC972>
>
> Thank you for your help.
>
> All the best,
> Andreas
Have you tried replicating what your code is doing with convert? This
doesn't sound like a PHP issue.
--- End Message ---
--- Begin Message ---
Hi everyone, I have been playing around with some code the list helped me with
a while back and I'm not grasping the concept between return and echo and the
PHP manual doesn't answer this, unless I'm missing something. There is an
example at the very bottom of PHP's return manual, but it's confusing.
So now I'm left wondering why return will only give me the first result in an
array, but echo will give me all the results of the array. Using stuart's
example he had sent me a while back I've messed around with it and modified it
to better understand it:
function filename($prefix)
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
return $filename;
}
}
echo completeImageFilename($row['MLS_No']);
With the above code I only get the first image of each picture name, but when I
change return to echo, it groups and displays all the pics that have the same
picture name.
--
David M.
--- End Message ---
--- Begin Message ---
> Hi everyone, I have been playing around with some code the list helped me
with a while back and I'm not grasping the concept between return and echo
and the PHP manual doesn't answer this, unless I'm missing something. There
is an > example at the very bottom of PHP's return manual, but it's
confusing.
>
> So now I'm left wondering why return will only give me the first result in
an array, but echo will give me all the results of the array. Using stuart's
example he had sent me a while back I've messed around with it and modified
it > to better understand it:
>
> function filename($prefix)
>{
> $matches = glob('images/property_pics/'.$prefix.'*');
> foreach($matches as $filename){
> return $filename;
> }
>}
>
>echo completeImageFilename($row['MLS_No']);
>
>With the above code I only get the first image of each picture name, but
when I change return to echo, it groups and displays all the pics that have
the same picture name.
>--
>David M.
The first loop and return is all you will get.
Put the information into an array and return the array once the array is
built.
--- End Message ---
--- Begin Message ---
Hi David,
A "return" statement will immediately halt execution of the current
function and return to where it was called.
In your case, the foreach loop will execute once and find a return
statement, and thus halting execution of the function and returning only
the first filename.
echo() is simply another function call (except it's a language construct
and not a function) and will not halt execution as "return" does.
What you want to do is something like:
$filenames = array();
foreach ($matches as $filename) {
$filenames[] = $filename;
}
return $filenames; // this is now an array containing all the filenames
But this is rather unneccesary, as $matches already is an array and
contains everything you need. Thus all you have to do is:
return $matches;
Which in turn can be shortened to:
function filename($prefix)
{
return glob('images/property_pics/'. $prefix .'*');
}
Happy Thursday!
Timmy
On 2012-10-04 02:48, David McGlone wrote:
Hi everyone, I have been playing around with some code the list helped me with
a while back and I'm not grasping the concept between return and echo and the
PHP manual doesn't answer this, unless I'm missing something. There is an
example at the very bottom of PHP's return manual, but it's confusing.
So now I'm left wondering why return will only give me the first result in an
array, but echo will give me all the results of the array. Using stuart's
example he had sent me a while back I've messed around with it and modified it
to better understand it:
function filename($prefix)
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
return $filename;
}
}
echo completeImageFilename($row['MLS_No']);
With the above code I only get the first image of each picture name, but when I
change return to echo, it groups and displays all the pics that have the same
picture name.
--
David M.
--- End Message ---
--- Begin Message ---
function filename($prefix)
{
$array_to_return = array();
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
$array_to_return[] = $filename;
}
return $array_to_return;
}
If this better explains it.
The first return will stop the process you need to put the information into
an array and then return the array outside of the foreach loop.
--- End Message ---
--- Begin Message ---
On Wednesday, October 03, 2012 08:55:29 PM admin wrote:
> > Hi everyone, I have been playing around with some code the list helped me
>
> with a while back and I'm not grasping the concept between return and echo
> and the PHP manual doesn't answer this, unless I'm missing something. There
> is an > example at the very bottom of PHP's return manual, but it's
> confusing.
>
> > So now I'm left wondering why return will only give me the first result in
>
> an array, but echo will give me all the results of the array. Using stuart's
> example he had sent me a while back I've messed around with it and modified
> it > to better understand it:
> > function filename($prefix)
> >
> >{
> >
> > $matches = glob('images/property_pics/'.$prefix.'*');
> > foreach($matches as $filename){
> > return $filename;
> >
> > }
> >
> >}
> >
> >echo completeImageFilename($row['MLS_No']);
> >
> >With the above code I only get the first image of each picture name, but
>
> when I change return to echo, it groups and displays all the pics that have
> the same picture name.
>
> >--
> >David M.
>
> The first loop and return is all you will get.
> Put the information into an array and return the array once the array is
> built.
I think I understand what your saying, but what I don't understand is that
when I leave the current code intact and replace return $filename with echo
$filename in the function, all the images display. Is this intended behavior
between return and echo or is it just bad code on my part?
--
David M.
--- End Message ---
--- Begin Message ---
All of the images are displaying because you're simply instructing the function
to print out each file found with your call to glob(). The glob() function
returns an indexed array containing files found in the path you specified, or
an empty array if no files were found or false if glob() failed. When I say
"print" I'm referring to you using the echo language construct, however, print
is also another language construct.
Therefore using echo in your function allows the foreach loop to continue
iterating through the array of files returned by glob(). Replacing that echo
with the return, the function ones one iteration in the foreach loop and stops,
returning that value. In your case, the function is returning index 0 of the
array returned by glob().
Make more sense?
David McGlone <da...@dmcentral.net> wrote:
>On Wednesday, October 03, 2012 08:55:29 PM admin wrote:
>> > Hi everyone, I have been playing around with some code the list
>helped me
>>
>> with a while back and I'm not grasping the concept between return and
>echo
>> and the PHP manual doesn't answer this, unless I'm missing something.
>There
>> is an > example at the very bottom of PHP's return manual, but it's
>> confusing.
>>
>> > So now I'm left wondering why return will only give me the first
>result in
>>
>> an array, but echo will give me all the results of the array. Using
>stuart's
>> example he had sent me a while back I've messed around with it and
>modified
>> it > to better understand it:
>> > function filename($prefix)
>> >
>> >{
>> >
>> > $matches = glob('images/property_pics/'.$prefix.'*');
>> > foreach($matches as $filename){
>> > return $filename;
>> >
>> > }
>> >
>> >}
>> >
>> >echo completeImageFilename($row['MLS_No']);
>> >
>> >With the above code I only get the first image of each picture name,
>but
>>
>> when I change return to echo, it groups and displays all the pics
>that have
>> the same picture name.
>>
>> >--
>> >David M.
>>
>> The first loop and return is all you will get.
>> Put the information into an array and return the array once the array
>is
>> built.
>
>I think I understand what your saying, but what I don't understand is
>that
>when I leave the current code intact and replace return $filename with
>echo
>$filename in the function, all the images display. Is this intended
>behavior
>between return and echo or is it just bad code on my part?
>
> --
>David M.
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
--- End Message ---
--- Begin Message ---
On Thursday, October 04, 2012 03:01:12 AM Timmy Sjöstedt wrote:
> Hi David,
>
> A "return" statement will immediately halt execution of the current
> function and return to where it was called.
>
> In your case, the foreach loop will execute once and find a return
> statement, and thus halting execution of the function and returning only
> the first filename.
>
> echo() is simply another function call (except it's a language construct
> and not a function) and will not halt execution as "return" does.
That's what I was looking for.
>
> What you want to do is something like:
>
> $filenames = array();
> foreach ($matches as $filename) {
> $filenames[] = $filename;
> }
> return $filenames; // this is now an array containing all the filenames
I see where I wasn't thinking correctly. I thought that $filename had already
contained all the results.
>
> But this is rather unneccesary, as $matches already is an array and
> contains everything you need. Thus all you have to do is:
>
> return $matches;
I had thought this also, and tried this but didn't get any results. probably
because I did something wrong somewhere else and didn't realize it.
>
> Which in turn can be shortened to:
>
> function filename($prefix)
> {
> return glob('images/property_pics/'. $prefix .'*');
> }
I'll mess around with this and see what I can learn..
David M.
>
> Happy Thursday!
> Timmy
>
> On 2012-10-04 02:48, David McGlone wrote:
> > Hi everyone, I have been playing around with some code the list helped me
> > with a while back and I'm not grasping the concept between return and
> > echo and the PHP manual doesn't answer this, unless I'm missing
> > something. There is an example at the very bottom of PHP's return manual,
> > but it's confusing.
> >
> > So now I'm left wondering why return will only give me the first result in
> > an array, but echo will give me all the results of the array. Using
> > stuart's example he had sent me a while back I've messed around with it
> > and modified it to better understand it:
> >
> > function filename($prefix)
> > {
> >
> > $matches = glob('images/property_pics/'.$prefix.'*');
> > foreach($matches as $filename){
> > return $filename;
> >
> > }
> >
> > }
> >
> > echo completeImageFilename($row['MLS_No']);
> >
> > With the above code I only get the first image of each picture name, but
> > when I change return to echo, it groups and displays all the pics that
> > have the same picture name.
> >
> >
> > --
> > David M.
--
David M.
--- End Message ---
--- Begin Message ---
On Wednesday, October 03, 2012 10:01:50 PM James wrote:
> All of the images are displaying because you're simply instructing the
> function to print out each file found with your call to glob(). The glob()
> function returns an indexed array containing files found in the path you
> specified, or an empty array if no files were found or false if glob()
> failed. When I say "print" I'm referring to you using the echo language
> construct, however, print is also another language construct.
>
> Therefore using echo in your function allows the foreach loop to continue
> iterating through the array of files returned by glob(). Replacing that
> echo with the return, the function ones one iteration in the foreach loop
> and stops, returning that value. In your case, the function is returning
> index 0 of the array returned by glob().
>
> Make more sense?
Absolutely. I also think I learned that return can also work like echo if the
code is written correctly.
>
> David McGlone <da...@dmcentral.net> wrote:
> >On Wednesday, October 03, 2012 08:55:29 PM admin wrote:
> >> > Hi everyone, I have been playing around with some code the list
> >
> >helped me
> >
> >> with a while back and I'm not grasping the concept between return and
> >
> >echo
> >
> >> and the PHP manual doesn't answer this, unless I'm missing something.
> >
> >There
> >
> >> is an > example at the very bottom of PHP's return manual, but it's
> >> confusing.
> >>
> >> > So now I'm left wondering why return will only give me the first
> >
> >result in
> >
> >> an array, but echo will give me all the results of the array. Using
> >
> >stuart's
> >
> >> example he had sent me a while back I've messed around with it and
> >
> >modified
> >
> >> it > to better understand it:
> >> > function filename($prefix)
> >> >
> >> >{
> >> >
> >> > $matches = glob('images/property_pics/'.$prefix.'*');
> >> > foreach($matches as $filename){
> >> > return $filename;
> >> >
> >> > }
> >> >
> >> >}
> >> >
> >> >echo completeImageFilename($row['MLS_No']);
> >> >
> >> >With the above code I only get the first image of each picture name,
> >
> >but
> >
> >> when I change return to echo, it groups and displays all the pics
> >
> >that have
> >
> >> the same picture name.
> >>
> >> >--
> >> >David M.
> >>
> >> The first loop and return is all you will get.
> >> Put the information into an array and return the array once the array
> >
> >is
> >
> >> built.
> >
> >I think I understand what your saying, but what I don't understand is
> >that
> >when I leave the current code intact and replace return $filename with
> >echo
> >$filename in the function, all the images display. Is this intended
> >behavior
> >between return and echo or is it just bad code on my part?
> >
> >David M.
--
David M.
--- End Message ---
--- Begin Message ---
On Wed, Oct 3, 2012 at 9:01 PM, James <ja...@nixsecurity.org> wrote:
> All of the images are displaying because you're simply instructing the
> function to print out each file found with your call to glob(). The glob()
> function returns an indexed array containing files found in the path you
> specified, or an empty array if no files were found or false if glob()
> failed. When I say "print" I'm referring to you using the echo language
> construct, however, print is also another language construct.
>
> Therefore using echo in your function allows the foreach loop to continue
> iterating through the array of files returned by glob(). Replacing that echo
> with the return, the function ones one iteration in the foreach loop and
> stops, returning that value. In your case, the function is returning index 0
> of the array returned by glob().
>
> Make more sense?
>
> David McGlone <da...@dmcentral.net> wrote:
>
>>On Wednesday, October 03, 2012 08:55:29 PM admin wrote:
>>> > Hi everyone, I have been playing around with some code the list
>>helped me
>>>
>>> with a while back and I'm not grasping the concept between return and
>>echo
>>> and the PHP manual doesn't answer this, unless I'm missing something.
>>There
>>> is an > example at the very bottom of PHP's return manual, but it's
>>> confusing.
>>>
>>> > So now I'm left wondering why return will only give me the first
>>result in
>>>
>>> an array, but echo will give me all the results of the array. Using
>>stuart's
>>> example he had sent me a while back I've messed around with it and
>>modified
>>> it > to better understand it:
>>> > function filename($prefix)
>>> >
>>> >{
>>> >
>>> > $matches = glob('images/property_pics/'.$prefix.'*');
>>> > foreach($matches as $filename){
>>> > return $filename;
>>> >
>>> > }
>>> >
>>> >}
>>> >
>>> >echo completeImageFilename($row['MLS_No']);
>>> >
>>> >With the above code I only get the first image of each picture name,
>>but
>>>
>>> when I change return to echo, it groups and displays all the pics
>>that have
>>> the same picture name.
>>>
>>> >--
>>> >David M.
>>>
>>> The first loop and return is all you will get.
>>> Put the information into an array and return the array once the array
>>is
>>> built.
>>
>>I think I understand what your saying, but what I don't understand is
>>that
>>when I leave the current code intact and replace return $filename with
>>echo
>>$filename in the function, all the images display. Is this intended
>>behavior
>>between return and echo or is it just bad code on my part?
>>
>> --
>>David M.
>
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
echo and return are not analogous items at all in PHP. They don't do
the same thing because their purpose is to do two entirely different
things.
For purposes of this discussion, the important bit about return is here:
"If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call." [1]
The concept comes from making a call to a function and then returning
from it, where you will use the value that is returned.
echo, on the other hand, is about *displaying* values:
"echo — Output one or more strings" [2]
Let me try a bogus analogy:
A friend gives you a dollar to buy a soda from the machine. You take
the dollar, go over to the soda machine and get the soda.
In the case of return, you hand your friend the soda.
In the case of echo, you drink the soda.
Perhaps an example that is out of the context you are looking at might help:
Example 1:
==========
function testreturn () {
for ($i = 0 ; $i < 10 ; $i++ ) { // this will begin a loop where the
variable $i will start at value 0, and increase in value each time
through the loop by 1, stopping after $i is equal to 9.
return $1; // this will "short circuit" the loop and the enclosing
function, or the php script if called from the global context. In
other words, it *ends* the loop *and* function right there, sending
the value of $i (0 in this case) back to the calling function or
context.
}
}
Example 2:
=========
function testecho () {
// Again, our for statement:
for ($i = 0 ; $i < 10 ; $i++) {
echo $i; // this will emit the current value of $i to standard
output. when it is done, the loop continues
}
}
Here, there is no return from the function, nothing is passed back to
the calling function or context.
Let's take a look at your code again:
function completeImageFilename($prefix)
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
return $filename;
}
}
echo completeImageFilename($row['MLS_No']);
When your loop contains nothing but the return $filename, it doesn't
matter how many entries are in your array, you are telling the
function to return with the first one in the array, no matter what.
If you modify it thusly:
function completeImageFilename($prefix)
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
echo $filename; // here you are telling it to send the value of
$filename to standard output
}
// there is no return value from the function
}
echo completeImageFilename($row['MLS_No']); // when you get here,
there is nothing to echo (or it attempts to echo NULL, which amounts
to the same thing.
(as a side note, the code you supplied would not work, as the name you
gave the function was "filename" yet the function you were trying to
call was "completeImageFileName".)
So, return is not echo, echo is not return.
[1] http://us3.php.net/manual/en/function.return.php
[2] http://us3.php.net/manual/en/function.echo.php
--- End Message ---
--- Begin Message ---
On Wed, Oct 3, 2012 at 9:57 PM, David McGlone <da...@dmcentral.net> wrote:
> Absolutely. I also think I learned that return can also work like echo if the
> code is written correctly.
No, no, no. Return does NOT do the same thing as echo, nor vice versa.
If you do try to make things work this way you are doing things
incorrectly. If you do try to make things work this way and it
actually works, you have gotten lucky, but are still doing it
incorrectly.
--- End Message ---
--- Begin Message ---
On Wed, Oct 3, 2012 at 10:03 PM, tamouse mailing lists
<tamouse.li...@gmail.com> wrote:
> On Wed, Oct 3, 2012 at 9:57 PM, David McGlone <da...@dmcentral.net> wrote:
>> Absolutely. I also think I learned that return can also work like echo if the
>> code is written correctly.
>
>
> No, no, no. Return does NOT do the same thing as echo, nor vice versa.
> If you do try to make things work this way you are doing things
> incorrectly. If you do try to make things work this way and it
> actually works, you have gotten lucky, but are still doing it
> incorrectly.
Let's try another example, based on your code:
Example 1: using return
=======================
function completeImageFilename($prefix) //correcting function name
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
return $filename;
}
}
echo "This is the complete image file name:
".completeImageFilename($row['MLS_No']) . PHP_EOL;
You will get one line of output with the text from the echo statement
and the first filename that matches.
Example 2: using echo
=====================
function completeImageFilename($prefix) //correcting function name
{
$matches = glob('images/property_pics/'.$prefix.'*');
foreach($matches as $filename){
echo "This is the file name from inside completeImageFilename: " .
$filename . PHP_EOL;
}
}
echo "This is the complete image file name:
".completeImageFilename($row['MLS_No']) . PHP_EOL;
You will get several lines of output for each filename that matches
with the text from the echo statement inside the function, then one
more line that will contain *just* the text from the final echo
statement:
This is the file name from inside completeImageFilename:
images/property_pics/151136.jpg
This is the file name from inside completeImageFilename:
images/property_pics/151137.jpg
This is the file name from inside completeImageFilename:
images/property_pics/151138.jpg
This is the file name from inside completeImageFilename:
images/property_pics/151139.jpg
This is the complete image file name:
(the file names are bogus and just made up by me to illustrate)
--- End Message ---
--- Begin Message ---
On 10/3/12 9:57 PM, David McGlone wrote:
Absolutely. I also think I learned that return can also work like echo if the
code is written correctly.
Echo and return are two completely different things in PHP. Echo is used
for printing a value out in a document -- for instance, as follows, in
the midst of a chunk of HTML:
<table>
<tr><td>Name:</td><td><?php echo $name; ?></td><tr>
<tr><td>Email:</td><td><?php echo $email; ?></td></tr>
</table>
In the above example, echo is being used to output the value specified
in the variable $name or $email (which would have been previously set)
It can also be used to print the return value of a function. Return, in
and of itself, does not print anything; it is used to ascribe a
particular value to an instance of a function.
So for instance, if you have a function:
function getEmail() {
$name = 'johnsmith';
$email = $name . '@sample.com';
return $email;
}
then any called instance of the function getEmail() will, in a sense,
have the value of the return statement (in a sense, the code inside the
function runs and *returns* a value in place of itself).
So
$variable = getEmail();
//variable is now equal to 'johnsm...@sample.com'
You could then echo this value, if you wanted to.
echo getEmail(); //prints the value returned by getEmail()
--- End Message ---
--- Begin Message ---
Hi All,
I have a page that receives third party data into my app, xml data via
https post. This works fine and I receive the data as expected. The
issue I am facing is around posting XML data back as a synchronous
response to the post I receive. I am using the following code:
function sendXMLConfirmation($data)
{
/*
* XML Sender/Client.
*/
// Get our XML. You can declare it here or even load a file.
$xml_builder = '<?xml version="1.0" encoding="utf-8"?>
<Envelope version="01.00">
<Sender>
<Id/>
<Credential>25412</Credential>
</Sender>
<Recipient>
<Id/>
</Recipient>
<TransactInfo transactType="response">
<TransactId>'.$hash.'</TransactId>
<TimeStamp>'.date("Y-m-d H:m
").'</TimeStamp>
<Status>
<Code>200</Code>
<ShortDescription>Success</ShortDescription>
<LongDescription>CANDIDATE Data transfer was a success</LongDescription>
</Status>
</TransactInfo>
<Packet>
<PacketInfo
packetType="response">
<PacketId>1</PacketId>
<Action>SET</Action>
<Manifest>Manifest
Data</Manifest>
<Status>
<Code/>
<ShortDescription/>
<LongDescription/>
</Status>
</PacketInfo>
<Payload><![CDATA[]]></Payload>
</Packet>
</Envelope>
';
// We send XML via CURL using POST with a http header of text/xml.
$ch = curl_init();
$url = 'https://staging.somesite.com/Sprocessor/DispatchMessage.asmx';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // for https
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
//Execute the request and also time the transaction ( optional )
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
curl_close($ch);
// Print CURL result.
echo $ch_result;
}
The endpoint recipient says they are not receiving the data and I am
at a loss to figure out why. Any insight would be appreciated.
Thanks,
--
Bastien
Cat, the other other white meat
--- End Message ---