PHP将圆角应用于图像

时间:2013-07-29 04:32:51

标签: php

我看到这篇关于使用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表单上传的图像上使用此代码,以便图像自动保存并已应用圆角?即。我如何:

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

2 个答案:

答案 0 :(得分:2)

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

 <img src='/path/to/image.png' style='border-radius: 5px;'/>

答案 1 :(得分:0)

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

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

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

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

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