9

I'm using imagecreatefromjpeg() function to merge two pictures..

now the problem which I'm facing is that when I use the pictures from my server, it works perfectly and when I use pictures from some other website, it doesn't work.

For example: when I use this PHP file http://coolfbapps.in/test/merger.php with function

 imagecreatefrompng('http://coolfbapps.in/test/1.png');

It works perfectly fine as the image is at my own server

but when I alter this function n put the link of an image which is not on my server,

for example.

  imagecreatefrompng('http://www.businesseconomics/Test.png');

it doesnt work. (the image file is not on my server)

please suggest me an alternative to this function or a solution as I want to use this with Facebook apps..

Functions like file-get-contents are also showing the same error. I hope its not server end problem.. allow_url_fopen is on but allow_url_include is off

Update...Actual code. I'm using this to merger two pictures

 $dest = imagecreatefrompng('http://coolfbapps.in/test/1.png');

 $src = imagejpeg('http://img.allvoices.com/thumbs/image/111/111/75152279-pic.jpg');

 imagealphablending($dest, false);
 imagesavealpha($dest, true);

 imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); 

 header('Content-Type: image/png');
 imagepng($dest);

 imagedestroy($dest);
 imagedestroy($src);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Anurag
  • 141
  • 1
  • 3
  • 12
  • Remove the image/jpeg header and just output the error.. what does it exactly say? – Gerben Jacobs Apr 07 '11 at 08:21
  • 3
    Let me just point out that `http://www.businesseconomics/` is probably not a valid URL. – deceze Apr 07 '11 at 08:25
  • 2
    This can have multiple errors. I would recommend to fetch the image using `file_get_contents()` first, and process it second so you can tell apart where things go wrong. – Pekka Apr 07 '11 at 11:07
  • @deceze thats just an example.. its not working even with correct url – Anurag Apr 08 '11 at 05:12
  • 1
    @Anurag your code works just fine as mine. I think there's some error on server or some misconfiguration – S L Apr 08 '11 at 05:45
  • @ experimentX I think i hv to get in touch with the service providers.. I appreciate your help :) – Anurag Apr 08 '11 at 05:57

3 Answers3

2

Instead of using file_get_content you can use cURL to get your image data. Here is a resource: http://php.net/manual/en/book.curl.php

Example with getting html ( images will also work ):

<?php    
    $ch = curl_init("http://img.allvoices.com/thumbs/image/111/111/75152279-pic.jpg");
    $fp = fopen("example_homepage.jpg", "w");

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    $img = imagecreatefromjpeg("example_homepage.jpg");
?>
user3666197
  • 1
  • 6
  • 50
  • 92
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
  • but my main aim is to merger two pictures.. not just fetching them.. imagecreatefromjpeg() is not working.. – Anurag Apr 08 '11 at 05:47
  • this is the right direction. you just need to write the rest. You need to pass the local file into imagecreatefromjpeg("example_homepage.jpg") instead of the url. – adrianj98 Oct 25 '14 at 03:15
1

Sounds like the function does not have URL opening capabilities, or it does and you have allow_url_fopen off in php.ini. You can't use ini_set() for security reasons.

You could download the file to your local server, and then open it.

file_put_contents('image.jpg',
                  file_get_contents('http://www.businesseconomics/Test.png')
                 );

You could probably use copy() too, the docs hint that it can read URLs.

alex
  • 479,566
  • 201
  • 878
  • 984
  • allow_url_open is on in php.ini – Anurag Apr 07 '11 at 08:15
  • how to add url opening capabilies? can u pls assist me a bit with this? – Anurag Apr 07 '11 at 08:17
  • shows the same error--- Warning: file_get_contents(http://www.flash-slideshow-maker.com/images/help_clip_image020.jpg) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. – Anurag Apr 07 '11 at 08:56
  • @Anurag Sounds like you need to include the protocol. Unlike a browser, PHP won't *assume* one. – alex Apr 08 '11 at 05:01
  • @ alex.. i'm bit confused here.. how should i include the protocol? – Anurag Apr 08 '11 at 05:32
1

Something like this might help.

$imagestr = file_get_contents('http://www.businesseconomics/Test.png');

$image = imagecreatefromstring($imagestr);

imagecreatefrompng($image);

UPDATED::

$imagestr = file_get_contents('http://www.gravatar.com/avatar/95111e2f99bb4b277764c76ad9ad3569?s=32&d=identicon&r=PG');

$image = imagecreatefromstring($imagestr);

header('Content-type: image/jpeg');

imagejpeg($image);
S L
  • 14,262
  • 17
  • 77
  • 116
  • file_get_contents is also showing the same error....Warning: file_get_contents(XXXXX) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:\inetpub\vhosts\coolfbapps.in\httpdocs\test\merger.php on line 4 Fatal error: Maximum execution time of 30 seconds exceeded in D:\inetpub\vhosts\coolfbapps.in\httpdocs\test\merger.php on line 4....I hope its not server problem – Anurag Apr 08 '11 at 04:55
  • @Anurag Well, can you try to do it with the image at the localserver i.e. `http://localhost/img.jpg` – S L Apr 08 '11 at 04:56
  • sir, i can use this function even when the files are on my own server.. problems occurs only with other servers.. yes its working fine with local server as well.. – Anurag Apr 08 '11 at 05:03
  • @Anurag i think you point that url to some small picture `http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif` for now, and lets see what happends. – S L Apr 08 '11 at 05:04
  • @Anurag, well the above updated code seems to work perfectly for me. – S L Apr 08 '11 at 05:15