13

I know it’s possible to use imagecreatefromjpeg(), imagecreatefrompng(), etc. with a URL as the ‘filename’ with fopen(), but I'm unable to enable the wrappers due to security issues. Is there a way to pass a URL to imagecreatefromX() without enabling them?

I’ve also tried using cURL, and that too is giving me problems:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.../image31.jpg"); //Actually complete URL to image
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

$image = imagecreatefromstring($data);
var_dump($image);

imagepng($image);
imagedestroy($image);
TRiG
  • 10,148
  • 7
  • 57
  • 107
iRector
  • 1,935
  • 4
  • 22
  • 30
  • So i don't know the URL you are using but i just tested this myself, and seemed to work. I've used this method with ImageMagick in some projects, and in our case for security reasons, we needed to use a proxy server, so perhaps that could be your issue? – FilmJ Sep 09 '12 at 09:40
  • The var_dump says: resource(65) of type (gd) – iRector Sep 09 '12 at 10:08
  • Try dumping the `$data` to see what you are actually getting back. – FilmJ Sep 09 '12 at 10:22
  • string(215063) "����ExifMM*�����(1�2҇i� ��' ��'Adobe Photoshop CS5 Macintosh2012:08:28 21:47:46�0221�����nv(~�HH����Adobe_CM��Adobed���� ��>�"�� ��? 3!1AQa"q�2���B#$R�b34r��C%�S���cs5���&D�TdE and it continues doing that for hundreds of lines. – iRector Sep 09 '12 at 10:54
  • *It continues the random characters for hundreds of lines*. Not including the string(215063) or Macintosh.... – iRector Sep 09 '12 at 10:55
  • iRector, sounds like it was working, yes? – FilmJ Sep 13 '12 at 06:38

3 Answers3

26

You can download the file using cURL then pipe the result into imagecreatefromstring.

Example:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imageurl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.
    $data = curl_exec($ch);
    curl_close($ch);

    $image = imagecreatefromstring($data);
FilmJ
  • 2,011
  • 3
  • 19
  • 27
  • +1, this is better than my suggestion of using a temporary file. I tried to look for something like `imagecreatefromstring` but missed it among all the other `imagecreatefrom*` functions. – Ilmari Karonen Sep 09 '12 at 08:46
  • Alain thanks for the CURLOPT_RETURNTRANSFER, i had forgotten that one. I also added CURLOPT_BINARYTRANSFER... seemed to make sense. – FilmJ Sep 09 '12 at 09:07
  • I know thats gotta be my answer, but I'm having an issue with displaying the image. See above for code. – iRector Sep 09 '12 at 09:30
  • for those who use binary transfer, we now should avoid using it because it is deprecated. https://bugs.php.net/bug.php?id=55635 – FerdousTheWebCoder Jul 27 '22 at 08:12
  • we may also use this reply here https://stackoverflow.com/a/5175664/3270433 – FerdousTheWebCoder Jul 27 '22 at 08:36
1

You could even implement a cURL based stream wrapper for 'http' using stream_wrapper_register.

Anthony Sterling
  • 2,451
  • 16
  • 10
0

You could always download the image (e.g. with cURL) to a temporary file, and then load the image from that file.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153