imagecreatefromjpeg() php
Asked Answered
D

3

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);
Dew answered 7/4, 2011 at 8:11 Comment(6)
Remove the image/jpeg header and just output the error.. what does it exactly say?Beata
Let me just point out that http://www.businesseconomics/ is probably not a valid URL.Clayborne
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.Arronarrondissement
@Clayborne thats just an example.. its not working even with correct urlDew
@Dew your code works just fine as mine. I think there's some error on server or some misconfigurationHinz
@ experimentX I think i hv to get in touch with the service providers.. I appreciate your help :)Dew
W
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");
?>
Within answered 8/4, 2011 at 5:33 Comment(2)
but my main aim is to merger two pictures.. not just fetching them.. imagecreatefromjpeg() is not working..Dew
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.Filiation
M
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.

Monjan answered 7/4, 2011 at 8:12 Comment(5)
allow_url_open is on in php.iniDew
how to add url opening capabilies? can u pls assist me a bit with this?Dew
shows the same error--- Warning: file_get_contents(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.Dew
@Dew Sounds like you need to include the protocol. Unlike a browser, PHP won't assume one.Monjan
@ alex.. i'm bit confused here.. how should i include the protocol?Dew
H
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);
Hinz answered 7/4, 2011 at 8:32 Comment(5)
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 problemDew
@Dew Well, can you try to do it with the image at the localserver i.e. http://localhost/img.jpgHinz
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..Dew
@Dew 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.Hinz
@Anurag, well the above updated code seems to work perfectly for me.Hinz

© 2022 - 2024 — McMap. All rights reserved.