Rounded corners on images using PHP?

2019-03-03 06:54发布

Does anyone know how to make a image have rounded corners using a PHP script?

9条回答
\"骚年 ilove
2楼-- · 2019-03-03 07:26

Download easyphpthumbnail.class.php from this link

from this you can resize and convert image into rounded image.

in below example image is converted into transparent circle image.

include_once('easyphpthumbnail.class.php');
$source = 'demo.jpg';
$width      =  100;
$height     =  100;    
$thumb = new easyphpthumbnail;
$thumb -> Thumbheight = $width;
$thumb -> Thumbwidth = $height;
$thumb -> Backgroundcolor = '#FFFFFF';
$thumb -> Clipcorner = array(2,50,0,1,1,1,1);
$thumb -> Maketransparent = array(1,0,'#FFFFFF',10);   
$thumb -> Createthumb($source);
查看更多
Explosion°爆炸
3楼-- · 2019-03-03 07:27

This script shows how to apply rounded corners to images using PHP and GD Library. It is as simple as drawing four quadrants of a circle over the four corners of the image. The circle itself has to be transparent.

This script, on the other hand, generates rounded corner graphics for HTML or CSS based solutions. It generates the four corners that you can overlay over an image using CSS positioning or HTML tables.

查看更多
唯我独甜
4楼-- · 2019-03-03 07:32

its easy to create some rounded thumbs using php, just use Thumbnailer :)

查看更多
乱世女痞
5楼-- · 2019-03-03 07:39

Instead of modifying the image, why not just wrap it in some HTML that has images at each corner that overlay the original to provide the appearance of rounded corners?

By doing the image editing in your .php script, you're going to put undue load on your web server, and that means your application won't scale well.

查看更多
来,给爷笑一个
6楼-- · 2019-03-03 07:42

You will need the GD library for that. There must be a lot of examples on the web. Here is one :

http://www.assemblysys.com/dataServices/php_roundedCorners.php

<?php
$image_file = $_GET['src'];
$corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; 
// The default corner radius is set to 20px
$angle = isset($_GET['angle']) ? $_GET['angle'] : 0; 
// The default angle is set to 0º
$topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; 
// Top-left rounded corner is shown by default
$bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default
$bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default
$topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default

$images_dir = 'images/';
$corner_source = imagecreatefrompng('images/rounded_corner.png');

$corner_width = imagesx($corner_source);  
$corner_height = imagesy($corner_source);  
$corner_resized = imagecreatetruecolor($corner_radius, $corner_radius);
imagecopyresampled($corner_resized, $corner_source, 0, 0, 0, 0,
                   $corner_radius, $corner_radius, $corner_width, 
                   $corner_height);

$corner_width = imagesx($corner_resized);  
$corner_height = imagesy($corner_resized);  
$image = imagecreatetruecolor($corner_width, $corner_height);  
$image = imagecreatefromjpeg($images_dir . $image_file); 
// replace filename with $_GET['src'] 
$size = getimagesize($images_dir . $image_file); 
// replace filename with $_GET['src'] 
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,0,0,0);

// Top-left corner
if ($topleft == true) {
    $dest_x = 0;  
    $dest_y = 0;  
    imagecolortransparent($corner_resized, $black); 
    imagecopymerge($image, $corner_resized, $dest_x, 
                   $dest_y, 0, 0, $corner_width, $corner_height, 
                   100);
} 

// Bottom-left corner
if ($bottomleft == true) {
    $dest_x = 0;  
    $dest_y = $size[1] - $corner_height; 
    $rotated = imagerotate($corner_resized, 90, 0);
    imagecolortransparent($rotated, $black); 
    imagecopymerge($image, $rotated, $dest_x, $dest_y, 
                   0, 0, $corner_width, $corner_height, 100);  
}

// Bottom-right corner
if ($bottomright == true) {
    $dest_x = $size[0] - $corner_width;  
    $dest_y = $size[1] - $corner_height;  
    $rotated = imagerotate($corner_resized, 180, 0);
    imagecolortransparent($rotated, $black); 
    imagecopymerge($image, $rotated, $dest_x, $dest_y, 
                   0, 0, $corner_width, $corner_height, 100);  
}

// Top-right corner
if ($topright == true) {
    $dest_x = $size[0] - $corner_width;  
    $dest_y = 0;  
    $rotated = imagerotate($corner_resized, 270, 0);
    imagecolortransparent($rotated, $black); 
    imagecopymerge($image, $rotated, $dest_x, $dest_y, 
                   0, 0, $corner_width, $corner_height, 100);  
}

// Rotate image
$image = imagerotate($image, $angle, $white);

// Output final image
imagejpeg($image);

// Remove temp files
imagedestroy($image);  
imagedestroy($corner_source);
?>

How to use it?

Needless to say, PHP and the GD library needs to be installed on your server. You’ll also need a png file of the corner. You can use one of these corner images if you want: [http://assemblysys.com/wp/wp-content/uploads/2013/03/rounded_corners.zip]

Just paste the code above and save it in a .php file. Make the necessary changes to the $images_dir and $corner_source variables (lines 10 and 11). Link the src attribute of your image to that file, using the following URL variables:

**src:** name of the image file
**radius** (optional): value that represents the radius (in pixels) of the corners; use this to resize the corner image
**angle** (optional): value that represents the rotation angle of the full image, in degrees
**topleft**=no (optional): do not round the top-left corner
**bottomleft**=no (optional): do not round the bottom-left corner
**bottomright**=no (optional): do not round the bottom-right corner
**topright**=no (optional): do not round the top-right corner

Examples

<img src='roundedCorners.php?src=image.jpg&radius=40' />
<img src='roundedCorners.php?src=image.jpg&radius=40&angle=15' />
<img 
src='roundedCorners.php?src=image.jpg&radius=40&topleft=no&bottomright=no' />
查看更多
聊天终结者
7楼-- · 2019-03-03 07:42

There are a lot of options available, you can find them using Google. The easiest way though is using the Thumbnailer. It's as simple as two lines of code:

// make an object
$th=new Thumbnailer("your-photo.jpg");

// create a 120x90 thumb and round its corners
$th->thumbFixed(120,90)->round()->save("your-thumb.jpg");

Fun it is, isn't it? :) There are a lot of other options available. The corners will be antialiased.

查看更多
登录 后发表回答