Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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-将带水印的图像保存到目录_Php_Image Processing_Gd - Fatal编程技术网

PHP-将带水印的图像保存到目录

PHP-将带水印的图像保存到目录,php,image-processing,gd,Php,Image Processing,Gd,嗨,我正在使用下面的脚本,它工作得非常好 我的问题是,如何用保留相同文件名和扩展名的带水印图像替换原始图像 $stamp = imagecreatefrompng(base_static_url().$this->marker_url); $im = imagecreatefromjpeg($img_path); // Set the margins for the stamp and get the height/width of the stamp image $marge_right

嗨,我正在使用下面的脚本,它工作得非常好

我的问题是,如何用保留相同文件名和扩展名的带水印图像替换原始图像

$stamp = imagecreatefrompng(base_static_url().$this->marker_url);
$im = imagecreatefromjpeg($img_path);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
我试着:

file_put_contents($img_path, imagecreatefromjpeg($im));
但是得到:

Error: failed to open stream: HTTP wrapper does not support writeable connections
我也试过:

file_put_contents($img_path, $im);
然后我发现了一个新的错误:

Error: file_put_contents(): supplied resource is not a valid stream resource
你可以试试:

imagejpeg($im, $img_path); 
获取一个文件名参数,该参数描述为:

保存文件的路径。如果未设置或为空,则直接输出原始图像流

但是,正如另一位用户所提到的,如果您试图将文件保存到远程服务器,则必须采用不同的方式。一种方法可能是使用PHP的FTP函数:

您可以尝试:

imagejpeg($im, $img_path); 
获取一个文件名参数,该参数描述为:

保存文件的路径。如果未设置或为空,则直接输出原始图像流


但是,正如另一位用户所提到的,如果您试图将文件保存到远程服务器,则必须采用不同的方式。一种方法可能是使用PHP的FTP函数:

错误解释了这一切:

Error: failed to open stream: HTTP wrapper does not support writeable connections
HTTP包装器(PHP的一部分,允许您在URI中使用
HTTP://
协议)无法写入web地址。不过,这是有道理的,因为想象一下如果有人可以运行这个:

file_put_contents('http://google.com', '<!--malicious script-->');
file\u put\u内容('http://google.com', '');
并接管谷歌

要将文件保存到远程Web服务器,您需要使用FTP、SFTP等访问其文件系统。PHP有内置的


但是,我怀疑您试图修改的文件位于执行此PHP脚本的服务器上。在这种情况下,您需要使用服务器上文件的路径(可能类似于
/var/www/images/image.jpg
),而不是网址(
http://www.yoursite.com/images/image.jpg
)在
文件中放置内容()

错误解释了这一切:

Error: failed to open stream: HTTP wrapper does not support writeable connections
HTTP包装器(PHP的一部分,允许您在URI中使用
HTTP://
协议)无法写入web地址。不过,这是有道理的,因为想象一下如果有人可以运行这个:

file_put_contents('http://google.com', '<!--malicious script-->');
file\u put\u内容('http://google.com', '');
并接管谷歌

要将文件保存到远程Web服务器,您需要使用FTP、SFTP等访问其文件系统。PHP有内置的


但是,我怀疑您试图修改的文件位于执行此PHP脚本的服务器上。在这种情况下,您需要使用服务器上文件的路径(可能类似于
/var/www/images/image.jpg
),而不是网址(
http://www.yoursite.com/images/image.jpg
)在
文件中放置内容()

确定itryed imagejpeg(),因为原始图像是要替换的jpg。问题是http不允许取消链接,并且似乎传递到imagejpeg()的路径不正常,而水印正常:/i接收到:imagejpeg()[]:无法打开“”进行写入:没有这样的文件或目录您确定这是正确的URL吗?错误消息表示路径不存在。您的php错误日志中是否有任何其他错误或警告?是的,路径绝对正确:(我授予777访问权限只是为了测试:(好的,如果您在浏览器中导航到该url,是否显示正确的图像?水印是否正常工作?用imagejpeg()替换imagepng())在您的回答中,我将接受它。非常感谢您的回答。好吧!我编辑了imagejpeg(),因为原来的是一个jpg,所以它将被替换。问题是http不允许取消链接,并且似乎传递到imagejpeg()的路径不正确,而水印是正确的:/i收到:imagejpeg()[]:无法打开“”进行写入:没有这样的文件或目录您确定这是正确的URL吗?错误消息说该路径不存在。您的php错误日志中是否有任何其他错误或警告?是的,该路径绝对正确:(我授予777访问权限只是为了测试:(好的,如果您在浏览器中导航到该url,是否会显示正确的图像?水印是否正常工作?在您的答案中用imagejpeg()替换imagepng(),我会接受它。非常感谢您的正确回答!我尝试了:*文件放置内容('./上传/img/iphone.jpg',imagejpeg($im));**它用相同的名称和ext替换原始图像,但图像现在为空:P@badbetonbreakbutbedbackbone这是因为
imagejpeg($im)
会回显图像,而不是返回图像(有一个参数,它使用布尔值表示成功或失败)。要写入文件,请将文件名作为第二个参数传递:
imagejpeg($im'。/uploads/img/iphone.jpg')
+1好的,你救了我的命,很抱歉,但我不得不接受另一个家伙的回答,因为他告诉我首先使用imagepng():Pi tryed:**文件内容('./uploads/img/iphone.jpg',imagejpeg($im));**它用相同的名称和ext替换原始图像,但图像现在为空:P@badbetonbreakbutbedbackbone这是因为
imagejpeg($im)
会回显图像,而不是返回图像(有一个参数,它使用布尔值表示成功或失败)。要写入文件,请将文件名作为第二个参数传递:
imagejpeg($im'./uploads/img/iphone.jpg')
+1好的,你救了我的命,很抱歉,但我不得不接受另一个家伙的回答,因为他让我先用imagepng():P