2011-04-07 118 views
4

我使用imagecreatefromjpeg()函数合并两个图片..imagecreatefromjpeg()PHP

我现在面临的问题是,当我使用的照片从我的服务器,它完美的作品,当我使用其他网站的图片,它不起作用。

例如:当我使用这个PHP文件http://coolfbapps.in/test/merger.php与功能

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

这工作完全正常的图像在自己的服务器

但是当我改变这个函数N放链接一个不在我的服务器上的图像,例如

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

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

请建议我到这个功能的替代或解决方案,我想与Facebook应用程序使用此..

像文件的获取,内容的功能也显示出同样的错误。我希望它不是服务器端的问题.. 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); 
+0

删除图像/ JPEG头并输出的错误..什么它究竟说什么? – 2011-04-07 08:21:24

+3

让我指出'http:// www.businesseconomics /'可能不是一个有效的URL。 – deceze 2011-04-07 08:25:35

+2

这可能有多个错误。我建议先使用'file_get_contents()'获取图像,然后再处理它,这样你就可以分辨出哪里出了问题。 – 2011-04-07 11:07:15

回答

0

听起来像函数不具有URL打开功能,或者它,你必须allow_url_fopen关闭在php.ini。出于安全原因,您不能使用ini_set()

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

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

你也许可以使用copy()过的文档暗示,它可以读取的URL。

+0

allow_url_open是在php.ini – Anurag 2011-04-07 08:15:35

+0

如何添加URL开放capabilies?你可以帮我一点吗? – Anurag 2011-04-07 08:17:52

+0

@Auurag查看我的更新。 – alex 2011-04-07 08:18:26

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

file_get_contents也显示相同的错误....警告:file_get_contents(XXXXX)[function.file-get-contents]:无法打开流:连接尝试失败,因为连接方在一段时间后没有正确响应或建立的连接失败,因为连接的主机未能响应。在D:\ inetpub \ vhosts \ coolfbapps.in \ httpdocs \ test \ merger.php上第4行 致命错误:D:\ inetpub \ vhosts \ coolfbappsin \ httpdocs \ test \ merger.php在第4行....我希望它不是服务器问题 – Anurag 2011-04-08 04:55:16

+0

@Auurag那么,你可以尝试使用本地服务器上的图像,即'http:// localhost/img.jpg' – 2011-04-08 04:56:57

+0

先生,我可以使用此功能,即使当文件在我自己的服务器上..问题只发生在其他服务器..是它的工作正常与本地服务器以及.. – Anurag 2011-04-08 05:03:34

1

而不是使用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"); 
?> 
+0

但我的主要目的是合并两张图片..不只是抓取它们。 。imagecreatefromjpeg()is not working .. – Anurag 2011-04-08 05:47:41

+0

这是正确的方向,你只需要写下剩下的部分,你需要将本地文件传入imagecreatefromjpeg(“example_homepage.jpg”)而不是url。 – adrianj98 2014-10-25 03:15:29

相关问题