Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从任何文件类型的Url创建图像_Php_Image_Gd - Fatal编程技术网

Php 从任何文件类型的Url创建图像

Php 从任何文件类型的Url创建图像,php,image,gd,Php,Image,Gd,我知道,但是有没有办法从任何类型的有效图像的url创建图像资源(最好是png)?还是必须确定文件类型,然后使用适当的函数 当我说url时,我的意思是http://sample.com/image.png,而不是数据url您可以分析此代码 $url=$_SERVER['REQUEST_URI']; $url=explode('.',$url); $extension=$url[1]; switch($extension){ case'jpg': imagecreatefromjp

我知道,但是有没有办法从任何类型的有效图像的url创建图像资源(最好是png)?还是必须确定文件类型,然后使用适当的函数

当我说url时,我的意思是
http://sample.com/image.png
,而不是数据url

您可以分析此代码

$url=$_SERVER['REQUEST_URI'];
$url=explode('.',$url);
$extension=$url[1];
switch($extension){
   case'jpg':
      imagecreatefromjpeg();
   break;
}
也许你想要这个:
$jpeg_image=imagecreatefromfile('photo.jpeg');
$gif_image=imagecreatefromfile('clipart.gif');
$png_image=imagecreatefromfile('transparent_checkboard.png');
$other_jpeg=imagecreatefromfile('picture.JPG');
//这要求您删除或重写文件。\u检查:
$jpeg_image=imagecreatefromfile('http://example.net/photo.jpeg' );
//当需要http://ARGS时,请参见以下操作:
$jpeg_image=imagecreatefromfile('http://example.net/photo.jpeg?foo=hello&bar=world' );
这是如何做到的:
函数imagecreatefromfile($filename){
如果(!file_存在($filename)){
抛出新的InvalidArgumentException('File'.$filename.'notfound');
}
开关(strtolower(路径信息($filename,路径信息扩展名))){
案例“jpeg”:
案件‘jpg’:
返回imagecreatefromjpeg($filename);
打破
案例“png”:
返回imagecreatefrompng($filename);
打破
案例“gif”:
返回imagecreatefromgif($filename);
打破
违约:
抛出新的InvalidArgumentException('File“.$filename.”不是有效的jpg、png或gif图像。“);
打破
}
}
通过对
开关进行一些小的修改
即可为web url提供相同的功能:
/*如果(!file_存在($filename)){
抛出新的InvalidArgumentException('File'.$filename.'notfound');

}首先使用
file\u get\u contents($url)
函数获取url,然后将内容保存到文件中。之后,您可以使用适当的图像处理功能进行进一步更改。您可以使用以下代码从url保存图像。以下是示例代码:

$url = "http://sample.com/image.png";
$arr = explode("/",$url);
$img_file = dir(__FILE__).'/'.$arr[count($arr)-1];
$data = file_get_contents($url);
$fp = fopen($img_file,"w");
fwrite($fp,$data);
fclose($fp);

谢谢。

您可能还对最高级的版本感兴趣:


我使用这个函数。它支持所有类型的URL和流包装器,以及php可以处理的所有图像类型

/**
 * creates a image ressource from file (or url)
 *
 * @version: 1.1 (2014-05-02)
 *
 * $param string:    $filename                    url or local path to image file
 * @param [bool:     $use_include_path]           As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search.
 * @param [resource: $context]                    A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL
 * @param [&array:   $info]                       Array with result info: $info["image"] = imageinformation from getimagesize, $info["http"] = http_response_headers (if array was populated)
 *
 * @see: http://php.net/manual/function.file-get-contents.php
 * @see: http://php.net/manual/function.getimagesize.php
 *
 * @return bool|resource<gd>                       false, wenn aus Dateiinhalt keine gueltige PHP-Bildresource erstellt werden konnte (z.b. bei BMP-Datei)
 * @throws InvalidArgumentException                Wenn Datei kein gueltiges Bild ist, oder nicht gelesen werden kann
 *
 */
function createImageFromFile($filename, $use_include_path = false, $context = null, &$info = null)
{
  // try to detect image informations -> info is false if image was not readable or is no php supported image format (a  check for "is_readable" or fileextension is no longer needed)
  $info = array("image"=>getimagesize($filename));
  $info["image"] = getimagesize($filename);
  if($info["image"] === false) throw new InvalidArgumentException("\"".$filename."\" is not readable or no php supported format");
  else
  {
    // fetches fileconten from url and creates an image ressource by string data
    // if file is not readable or not supportet by imagecreate FALSE will be returnes as $imageRes
    $imageRes = imagecreatefromstring(file_get_contents($filename, $use_include_path, $context));
    // export $http_response_header to have this info outside of this function
    if(isset($http_response_header)) $info["http"] = $http_response_header;
    return $imageRes;
  }
}
用法(复杂示例):


最简单的方法是让php决定文件类型:

$image = imagecreatefromstring(file_get_contents($src));
这可能对你有帮助

$image= imagecreatefromstring(文件获取内容(“您的图像路径在这里”)


示例:
$image=imagecreatefromstring(文件获取内容('sample.jpg')

您需要先抓取图像,然后处理它,然后取消链接()。它是可能的(URL可以用作文件名),但这仍然需要获取它。存在问题,因为
$extension
应该是数组的最后一部分,但在您的示例中,如果
$URL=,它始终是第二部分http://www.example.org/images/picture.png“
然后,
$扩展名将是“示例”
。获取最后一部分的正确方法(您的方法)是使用
$extension=array_pop(explode('.',$url))。这将保持
$url
不变,并从字符串末尾正确提取
$extension
。您不能依赖于扩展是否正确,来自@supersan的答案更可靠简单且完美。谢谢supersan!
/**
 * creates a image ressource from file (or url)
 *
 * @version: 1.1 (2014-05-02)
 *
 * $param string:    $filename                    url or local path to image file
 * @param [bool:     $use_include_path]           As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search.
 * @param [resource: $context]                    A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL
 * @param [&array:   $info]                       Array with result info: $info["image"] = imageinformation from getimagesize, $info["http"] = http_response_headers (if array was populated)
 *
 * @see: http://php.net/manual/function.file-get-contents.php
 * @see: http://php.net/manual/function.getimagesize.php
 *
 * @return bool|resource<gd>                       false, wenn aus Dateiinhalt keine gueltige PHP-Bildresource erstellt werden konnte (z.b. bei BMP-Datei)
 * @throws InvalidArgumentException                Wenn Datei kein gueltiges Bild ist, oder nicht gelesen werden kann
 *
 */
function createImageFromFile($filename, $use_include_path = false, $context = null, &$info = null)
{
  // try to detect image informations -> info is false if image was not readable or is no php supported image format (a  check for "is_readable" or fileextension is no longer needed)
  $info = array("image"=>getimagesize($filename));
  $info["image"] = getimagesize($filename);
  if($info["image"] === false) throw new InvalidArgumentException("\"".$filename."\" is not readable or no php supported format");
  else
  {
    // fetches fileconten from url and creates an image ressource by string data
    // if file is not readable or not supportet by imagecreate FALSE will be returnes as $imageRes
    $imageRes = imagecreatefromstring(file_get_contents($filename, $use_include_path, $context));
    // export $http_response_header to have this info outside of this function
    if(isset($http_response_header)) $info["http"] = $http_response_header;
    return $imageRes;
  }
}
$image = createImageFromFile("http://sample.com/image.png");
// even sources with php extensions are supported and e.g. Proxy connections and other context Options
// see http://php.net/manual/function.stream-context-create.php for examples
$options = array("http"=> 
                  array("proxy" => "tcp://myproxy:8080",
                        "request_fulluri" => true
                       )
                  );
$context = stream_context_create($options);

$image = createImageFromFile("http://de3.php.net/images/logo.php", null, $context,$info);

// ... your code to resize or modify the image
$image = imagecreatefromstring(file_get_contents($src));