imagecreatefromjpeg()php

时间:2011-04-07 08:11:14

标签: php image image.createimage

我正在使用imagecreatefromjpeg()函数合并两张图片..

现在我面临的问题是,当我使用服务器上的图片时,它运行得很好,当我使用其他网站的图片时,它不起作用。

例如:当我使用带有函数

的PHP文件http://coolfbapps.in/test/merger.php
 imagecreatefrompng('http://coolfbapps.in/test/1.png');

由于图像在我自己​​的服务器上,它完全正常

但是当我更改此功能时,请将图像的链接放在我的服务器上,

例如,

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

它不起作用。 (图像文件不在我的服务器上)

请建议我替代此功能或解决方案,因为我想在Facebook应用中使用它..

像file-get-contents这样的函数也显示相同的错误。我希望它不是服务器端问题..        allow_url_fopen打开但是        allow_url_include已关闭

更新...实际代码。我用它来合并两张图片

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

3 个答案:

答案 0 :(得分:2)

您可以使用file_get_content来获取图像数据,而不是使用cURL。这是一个资源: http://php.net/manual/en/book.curl.php

获取html的示例(图像也可以):

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

答案 1 :(得分:0)

听起来这个功能没有网址开启功能,或者确实如此,你在php.ini中关闭allow_url_fopen。出于安全原因,您无法使用ini_set()

您可以将文件下载到本地服务器,然后将其打开。

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

您也可以使用copy(),文档提示它可以读取网址。

答案 2 :(得分:0)

这样的事情可能有所帮助。

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

$image = imagecreatefromstring($imagestr);

imagecreatefrompng($image);

<强>已更新 ::

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