在 PHP 中向图像添加样式背景
我正在努力改进我的 Facebook 应用程序之一,允许用户上传图像并对其应用样式边框或框架(即云、星星、天空等)。用户还可以保存应用后带有边框的图像。这更好地解释了我的需要:
http://zbrowntechnology.info/ImgDisp/imgdisp.php
如果您有任何其他问题或需要更多详细信息,请告诉我..我将编辑这篇文章。
I am working on improving one of my Facebook apps by allowing the user to upload an image and apply a styled border, or frame to it (i.e. clouds, stars, sky etc). The user chould also be able to save the image, with the border after it has been applied. This explains a little better what I need:
http://zbrowntechnology.info/ImgDisp/imgdisp.php
If you have any other questions or need more details, please let me know.. I'll edit this post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用imagecopy()。该页面上的示例是使用 imagecopymerge() 的透明度选项完成的,但我认为您不需要它。
使用 imagecopy(),您将指定用于定位的 X/Y 坐标:
imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);
其中 < code>$width 和
$height
将是顶部图像的整个宽度和高度。您需要将20
和20
替换为边框图像在边框周围显示的量。您可能需要将顶部图像的大小调整为您想要的确切尺寸,否则它可能会与右侧或底部的边框重叠得有点远。 (参见imagecopyresampled())编辑:
这是完成整个过程的粗略方法处理(假设
chosenborder.png
是他们选择的边框,uploadedimage.png
是他们上传的图像。如果是不同的图像类型,您将使用 相应函数)。用户上传图像后,找到
chosenborder.png
和uploadedimage.png
,运行上述脚本,然后向用户显示newimage.png
现在就可以走了。只需确保对临时图像资源调用imagedestroy()
即可,否则它们会耗尽内存。如果您不想将生成的图像保留在服务器上,可以省略 imagepng() 的第二个参数,这将使其直接将图像信息作为图像发送到浏览器,其中如果您需要编写正确的图像 HTTP 标头。
Use imagecopy(). The example on that page is done using the transparency option with imagecopymerge() but I don't think you need that.
Using imagecopy() you'll specify the X/Y coordinates to use for positioning:
imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);
Where
$width
and$height
will be the entire width and height of the top image. You'll want to replace20
and20
with the measurement for how much of the border image will be showing around the borders. You will probably have to resize the top image to the exact dimensions you want, or else it might overlap the border a little too far to the right or bottom. (see imagecopyresampled())Edit:
Here's a rough way to do the whole process (assuming
chosenborder.png
is the border they chose, anduploadedimage.png
is the image they uploaded. If it's a different image type you'll use the corresponding function).After the user uploads their image, find
chosenborder.png
anduploadedimage.png
, run the above script, then displaynewimage.png
to the user and you're good to go. Just make sure you callimagedestroy()
on the temporary image resources or they'll eat up memory.If you don't want to keep the generated image on your server, you can omit the second argument to
imagepng()
which will make it send the image information directly as an image to the browser, in which case you'll want to write the correct image HTTP headers.使用 css3 的客户端解决方案:
检查 css3 属性 border-image
(不满足保存带边框的img的要求)
服务器端合并2张不同图像的解决方案:
Client-side solution by using css3:
checkout the css3 property border-image
(dosen't meet the requirement of saving the img with the border)
Server-side solution by merging 2 different images: