0

我看到这篇关于使用 PHP 对图像应用圆角的博客文章:http: //salman-w.blogspot.ca/2009/05/generate-images-with-round-corners-on.html

代码如下

<?php
/*
 * Apply Round Corner PHP-GD
 * http://salman-w.blogspot.com/2009/05/generate-images-with-round-corners-on.html
 *
 * Adds rounded corners to the specified image
 */

/*
 * source: path or url of a gif/jpeg/png image -- php fopen url wrapper must be enabled if using url
 * radius: corner radius in pixels -- default value is 10
 * colour: corner colour in rgb hex format -- default value is FFFFFF
 */

$source = $_GET["source"];
$radius = isset($_GET["radius"]) ? $_GET["radius"] : 10;
$colour = isset($_GET["colour"]) ? $_GET["colour"] : "FFFFFF";

/*
 * open source image and calculate properties
 */

list($source_width, $source_height, $source_type) = getimagesize($source);
switch ($source_type) {
    case IMAGETYPE_GIF:
        $source_image = imagecreatefromgif($source);
        break;
    case IMAGETYPE_JPEG:
        $source_image = imagecreatefromjpeg($source);
        break;
    case IMAGETYPE_PNG:
        $source_image = imagecreatefrompng($source);
        break;
}

/*
 * create mask for top-left corner in memory
 */

$corner_image = imagecreatetruecolor(
    $radius,
    $radius
);

$clear_colour = imagecolorallocate(
    $corner_image,
    0,
    0,
    0
);

$solid_colour = imagecolorallocate(
    $corner_image,
    hexdec(substr($colour, 0, 2)),
    hexdec(substr($colour, 2, 2)),
    hexdec(substr($colour, 4, 2))
);

imagecolortransparent(
    $corner_image,
    $clear_colour
);

imagefill(
    $corner_image,
    0,
    0,
    $solid_colour
);

imagefilledellipse(
    $corner_image,
    $radius,
    $radius,
    $radius * 2,
    $radius * 2,
    $clear_colour
);

/*
 * render the top-left, bottom-left, bottom-right, top-right corners by rotating and copying the mask
 */

imagecopymerge(
    $source_image,
    $corner_image,
    0,
    0,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    0,
    $source_height - $radius,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    $source_width - $radius,
    $source_height - $radius,
    0,
    0,
    $radius,
    $radius,
    100
);

$corner_image = imagerotate($corner_image, 90, 0);

imagecopymerge(
    $source_image,
    $corner_image,
    $source_width - $radius,
    0,
    0,
    0,
    $radius,
    $radius,
    100
);

/*
 * output the image -- revise this step according to your needs
 */

header("Content-type: image/png");
imagepng($source_image);
?>

我的问题是,如何在使用 html 表单上传的图像上使用此代码,以便图像自动保存并应用圆角?IE。我如何:

  1. 将代码应用于上传/上传的图像
  2. 将此图像输出到本地文件而不是提供给浏览器
4

2 回答 2

2

我建议不要将此处理应用于服务器上的用户图像,如果您的设计要求发生变化,除非您还保存原始图像的副本,否则无法返回。您可以使用 css border-radius 属性实现相同的效果。

 <img src='/path/to/image.png' style='border-radius: 5px;'/>
于 2013-07-29T05:12:55.467 回答
0

尝试这样的事情。确保您的表单标签如下所示:

<form action="..." method="post" enctype="multipart/form-data">

那么你的 php 代码应该是这样的:

$data = file_get_contents($_FILES['uploadedfile']['tmp_name'])
$image = imagecreatefromstring($data)

然后将您的流程应用于 $image

于 2013-07-29T04:39:55.127 回答