-2

I have a dynamic image as PHP file:

<?php
header('Content-type: image/png');
echo file_get_contents('image.png');

Now I want to read content image and store it into a file with PHP and CURL and executable commands.

First for sure, I test this command in cmd:

# curl -s http://localhost/image.php > output.png

This works good.

Now I use of same command in the php code with shell_exec function:

<?php
$cmd = 'curl -s http://localhost/image.php';
$out = shell_exec($cmd);
file_put_contents('output.png', $out);

But the output incomplete:

‰PNG

And this may be binary in the photo because of NULL characters.

Another test I try with exec function.

<?php
$cmd = 'curl -s http://localhost/image.php';
exec($cmd, $out);
file_put_contents('output.png', $out);

Seem the output file contain real binary image:

‰PNG   
IHDR   ً   P   Z'ï      PLTEےےے   –––¹¸vٍ       pHYs  ؤ  ؤ•+  tIDATX…¥کQ®©„Mdٍl"ْ®"کgˆ`ض“Hة{"e61«¼_u–0™(sخك
کr¹\‏يكےًا~¶’¼ïk¥‘’¥½ZKأb¬Zkّ‰¶»¯½|إj–|ظ.nضـ‏²¯‍آ‡y*üµàٌب›g–Z²'¹چضِê¶طج£µث5³kjںىkoُ

But the image will not be shown.

I diff real image and new generated image:

diff real and out images

Apparently, because the content of the $out is an array. Not well written in the new file.

Now, Where are my forms of work and how can I get to my goal?

EDIT: seem this problem is just on windows.

Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
  • its already a file `image.png` i dont understand why you can't just use that –  Dec 06 '17 at 04:35
  • @nogad It's just a small laboratory environment for what I'm going to do. The fact is that the original is a photo on another server, and after requesting the php file, I will be redirected to the URL of photo. For this I use the `-L` CURL param. So what I finally got is a binary photo and I want to generate base64 from that image. Of course If I would not succeed. I might have to remove the `-L` param and get the final URL image of the CURL response header and grab it with `file_get_contents` and work directly on it. or try and use CURL object php core. – Nabi K.A.Z. Dec 06 '17 at 04:45
  • When voting negative. Write down the reason. – Nabi K.A.Z. Dec 06 '17 at 07:12

2 Answers2

0

The shell_exec doesn't work on my computer either. For exec, the problem may be introduced by the additional characters in the exec results which may contain different characters than the picture.

To solve this, you can simply try these codes. It works on my computer.

<?php
$cmd = 'curl http://localhost/image.php > output.png';
exec($cmd);
Phil
  • 1,444
  • 2
  • 10
  • 21
  • I do not want to have the content in the file. I want to have the content in the variable, and I'll do it on base64_encode or other things. Of course, I tried to put it in the file in my example questions. But this is not to explain my problem and purpose. – Nabi K.A.Z. Dec 07 '17 at 15:12
-1

exec tries to explode lines and returns an array that is hard to deal with

shell_exec will work just fine.

<?php
    $cmd = 'curl -s http://localhost/image.php';
    $out = shell_exec($cmd);
    file_put_contents('output.png', $out);
JoshKisb
  • 742
  • 7
  • 9