imagecreatefromjpeg() php

2019-04-30 00:33发布

问题:

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);

回答1:

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");
?>


回答2:

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.



回答3:

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);