imagecreatefromjpeg() php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5577903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:51:23  来源:igfitidea点击:

imagecreatefromjpeg() php

phpimageimage.createimage

提问by Anurag

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

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

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.phpwith function

例如:当我使用这个 PHP 文件http://coolfbapps.in/test/merger.phpwith 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..

请给我建议此功能的替代方案或解决方案,因为我想将其与 Facebook 应用程序一起使用。

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

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

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

回答by Mark Mooibroek

Instead of using file_get_contentyou can use cURLto get your image data. Here is a resource: http://php.net/manual/en/book.curl.php

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

Example with getting html ( images will also work ):

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

回答by alex

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

听起来该功能没有 URL 打开功能,或者它有,而您allow_url_fopenphp.ini. ini_set()出于安全原因,您不能使用。

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.

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

回答by Santosh Linkha

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