如何在php中使用Storage::get()将图像作为资源而不是字符串获取
我试图给用户的照片与所需的背景,存储在我的服务器上的文字,他们想要在它。但是要使用php动态编辑图像,我需要将背景图像文件作为资源检索,但我将图像作为字符串获取 我尝试使用Storage::get函数。然后我尝试获取文件的url,并获取_file_contents$url,但没有成功如何在php中使用Storage::get()将图像作为资源而不是字符串获取,php,image,laravel-5,Php,Image,Laravel 5,我试图给用户的照片与所需的背景,存储在我的服务器上的文字,他们想要在它。但是要使用php动态编辑图像,我需要将背景图像文件作为资源检索,但我将图像作为字符串获取 我尝试使用Storage::get函数。然后我尝试获取文件的url,并获取_file_contents$url,但没有成功 $bg_image = Storage::disk('public')->get('background.png'); imagettftext($bg_image, 64, 10, 20, 20, $col
$bg_image = Storage::disk('public')->get('background.png');
imagettftext($bg_image, 64, 10, 20, 20, $color, $font, $user_name);
但这让我犯了一个错误
imagettftext要求参数1是给定的资源字符串
通过阅读php文档,您需要的函数是imagecreatefrompng,它创建一个图像资源,您可以将其传递给imagettftext函数
imagettftext ( resource $image , float $size , float $angle ,
int $x , int $y , int $color , string $fontfile , string $text ) : array
// $image: An image resource, returned by one of the image creation
// functions, such as imagecreatetruecolor()
还有其他用于不同图像文件类型的函数
Ctrl+F‘imagecreate’的PHP手册中提到图像参数必须是:
图像资源,由图像创建函数之一返回,
例如ImageCreateTureColor
这意味着您必须首先执行适当的图像创建函数,以从文件中读取数据并返回资源对象。因为在本例中处理的是PNG,所以该函数很可能就是您所需要的
大概是这样的:
$resource = imagecreatefrompng("/path/to/background.png")
imagettftext($resource, 64, 10, 20, 20, $color, $font, $user_name);
$resource = imagecreatefrompng("/path/to/background.png")
imagettftext($resource, 64, 10, 20, 20, $color, $font, $user_name);