从php图像资源获取图像文件
问题描述:
当我在php上使用imagecopyresized()
函数时,它返回为图像资源,我想要的是从此图像获取图像数据resource.may可以像字符串或数组中的文件位置包含文件的多个数据,就像您在$ _FILES []全局数组中获得的数据一样。感谢从php图像资源获取图像文件
$source = $_FILES['image']['tmp_name'];
list($width,$height) = getimagesize($source);
echo BR.$height." ".$width;
$desired_height = 28;
$scale = $width/$height;
$new_width = $desired_height * $scale;
$new_height = $desired_height;
$original_image = imagecreatefromjpeg($source);
$resized_image = imagecreatetruecolor($new_width, $new_height);
if(imagecopyresized($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height)){
echo BR."image resized".BR;
}
我想存储在数据库中$resized_image
的位置,但我似乎无法从这个变量来访问文件位置。
答
这会将您的图像内容以jpeg的形式放到$ buffer变量中。
ob_start();
imagejpeg($im, null, 90);
$buffer = ob_get_contents();
ob_end_clean();
可以再用file_put_contents
将其保存到期望的路径和路径保存到数据库中。
见https://secure.php.net/manual/en/function.imagejpeg.php
或者,你可以使用第三方库,具有更好的API,如Nette\Utils\Image或Intervention\Image。
答
imagecreatetruecolor
在内存中创建一个图像资源并返回其标识符。它不会在图像中创建任何物理文件。
使用imagejpeg
,如:
imagejpeg($resized_image, '/path/to/save/img.jpg')
它将创建和图像文件保存到磁盘。然后您可以将该路径存储在数据库中。
见:http://php.net/manual/en/function.imagejpeg.php和http://php.net/manual/en/function.imagecreatetruecolor.php
答
$ resized_image不与å文件关联。 您需要使用imagejpeg,imagepng,..方法将资源保存到文件。
imagejpeg($resized_image, 'path/file.jpg');