在 PHP 中向图像添加样式背景

发布于 2024-09-27 13:16:57 字数 214 浏览 7 评论 0原文

我正在努力改进我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-10-04 13:16:57

使用imagecopy()。该页面上的示例是使用 imagecopymerge() 的透明度选项完成的,但我认为您不需要它。

使用 imagecopy(),您将指定用于定位的 X/Y 坐标:

imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);

其中 < code>$width 和 $height 将是顶部图像的整个宽度和高度。您需要将 2020 替换为边框图像在边框周围显示的量。您可能需要将顶部图像的大小调整为您想要的确切尺寸,否则它可能会与右侧或底部的边框重叠得有点远。 (参见imagecopyresampled()

编辑:

这是完成整个过程的粗略方法处理(假设 chosenborder.png 是他们选择的边框,uploadedimage.png 是他们上传的图像。如果是不同的图像类型,您将使用 相应函数)。

$borderx = 20; // The width of our border
$border = imagecreatefrompng("chosenborder.png");
$topimage = imagecreatefrompng("uploadedimage.png");
$bordersize = getimagesize($border);
$topimagesize = getimagesize($topimage);

/* The new dimensions of topimage. $borderx*2 means we account for
   the border on both left and right, top and bottom. */
$newx = $bordersize[0] - ($borderx*2);
$newy = $bordersize[1] - ($borderx*2);
imagecopyresampled( $topimage_scaled, $topimage, 0, 0, 0, 0,
              $newx, $newy, $topimagesize[0], $topimagesize[1]);

/* Merge the images */
imagecopy( $border, $topimage_scaled, $borderx, $borderx,
              0, 0, $width, $height);
/* Output the image */
imagepng($border, "newimage.png");
/* Free up the memory occupied by the image resources */
imagedestroy($border);
imagedestroy($topimage);

用户上传图像后,找到 chosenborder.pnguploadedimage.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 replace 20 and 20 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, and uploadedimage.png is the image they uploaded. If it's a different image type you'll use the corresponding function).

$borderx = 20; // The width of our border
$border = imagecreatefrompng("chosenborder.png");
$topimage = imagecreatefrompng("uploadedimage.png");
$bordersize = getimagesize($border);
$topimagesize = getimagesize($topimage);

/* The new dimensions of topimage. $borderx*2 means we account for
   the border on both left and right, top and bottom. */
$newx = $bordersize[0] - ($borderx*2);
$newy = $bordersize[1] - ($borderx*2);
imagecopyresampled( $topimage_scaled, $topimage, 0, 0, 0, 0,
              $newx, $newy, $topimagesize[0], $topimagesize[1]);

/* Merge the images */
imagecopy( $border, $topimage_scaled, $borderx, $borderx,
              0, 0, $width, $height);
/* Output the image */
imagepng($border, "newimage.png");
/* Free up the memory occupied by the image resources */
imagedestroy($border);
imagedestroy($topimage);

After the user uploads their image, find chosenborder.png and uploadedimage.png, run the above script, then display newimage.png to the user and you're good to go. Just make sure you call imagedestroy() 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.

娜些时光,永不杰束 2024-10-04 13:16:57

使用 css3 的客户端解决方案:

检查 css3 属性 border-image
(不满足保存带边框的img的要求)

服务器端合并2张不同图像的解决方案:

<?php


$imgFile = 'img.jpg';
$brdFile = 'brd.jpg';
$img = addBorder($imgFile,$brdFile);
outputImage($img);

function addBorder($imgFile,$brdFile)
{
    $img=imagecreatefromjpeg($imgFile);
    $brd=imagecreatefromjpeg($brdFile);

    $imgSize = getimagesize($imgFile);
    $brdSize = getimagesize($brdFile);


    //NOTE: the border img MUST be bigger then the src img
    $dst_x = ceil(($brdSize[0] - $imgSize[0])/2);
    $dst_y = ceil(($brdSize[1] - $imgSize[1])/2);


    imagecopymerge  ( $brd, $img, $dst_x,  $dst_y,  0, 0, $imgSize[0], $imgSize[1] ,100  );

    return $brd;
}   

function outputImage($img)
{
    header('Content-type: image/png');  
    imagepng($img);
}

?>

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:

<?php


$imgFile = 'img.jpg';
$brdFile = 'brd.jpg';
$img = addBorder($imgFile,$brdFile);
outputImage($img);

function addBorder($imgFile,$brdFile)
{
    $img=imagecreatefromjpeg($imgFile);
    $brd=imagecreatefromjpeg($brdFile);

    $imgSize = getimagesize($imgFile);
    $brdSize = getimagesize($brdFile);


    //NOTE: the border img MUST be bigger then the src img
    $dst_x = ceil(($brdSize[0] - $imgSize[0])/2);
    $dst_y = ceil(($brdSize[1] - $imgSize[1])/2);


    imagecopymerge  ( $brd, $img, $dst_x,  $dst_y,  0, 0, $imgSize[0], $imgSize[1] ,100  );

    return $brd;
}   

function outputImage($img)
{
    header('Content-type: image/png');  
    imagepng($img);
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文