Php 将图像另存为服务器上文件夹中的文件
我正在运行这个代码Php 将图像另存为服务器上文件夹中的文件,php,image,save,Php,Image,Save,我正在运行这个代码 $im = imagecreatefromstring($arr['IMAGE']->load()); $result = $arr['IMAGE']->load(); echo $result; exit(); 这段代码在浏览器上显示图像。我的问题是…如何保存为文件并保存到服务器上 我正在使用这段代码,它正在保存为文件,但没有图像 define('UPLOAD_DIR', 'testuploads/'); // $img = str_replace('da
$im = imagecreatefromstring($arr['IMAGE']->load());
$result = $arr['IMAGE']->load();
echo $result;
exit();
这段代码在浏览器上显示图像。我的问题是…如何保存为文件并保存到服务器上
我正在使用这段代码,它正在保存为文件,但没有图像
define('UPLOAD_DIR', 'testuploads/');
// $img = str_replace('data:image/png;base64,', '', $result);
// $img = str_replace(' ', '+', $img);
$data = imagejpeg($result);
$file = UPLOAD_DIR . uniqid() . '.jpeg';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
如果是png,则使用imagejpeg($result,$file)或imagepng,而不是文件内容
(其中,$result是您的img,$file是您的路径+文件名)
编辑:见文件:
例如:
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Save the image as 'simpletext.jpg'
imagejpeg($im, 'simpletext.jpg');
?>
$im=imagecreatefromstring($arr['IMAGE']->load())代码>似乎是多余的。这一行的目的是什么?你的意思是做imagejpeg($im)代码>以后?还要检查-它返回true或false,而不是数据。必须将输出目标指定为参数。如果你有更多的代码,请用它来编辑问题。无论如何,它仍然不会工作,因为我提到了imagejpeg的返回类型。在编写此代码之前,您是否阅读了文档?