Add round corners to a jpeg file

假装没事ソ 提交于 2019-12-22 09:55:38

问题


I am trying to add round corners to a jpeg file, but the problem is that after adding round corners, I am getting a black background color. Somehow I am not able to change it to any other color (white, transparent, red). It just simply shows black background where the image has rounded corners.

The code that I am using is:

<?php

    $image = new Imagick('example.jpg');
    $image->setBackgroundColor("red");
    $image->setImageFormat("jpg");
    $image->roundCorners(575,575);
    $image->writeImage("rounded.jpg");
    header('Content-type: image/jpeg');
    echo $image;

?>

I cannot use png as the jpeg files are huge, about 5 MB, so if I used png, the file size would go up to 26 MB, even though the png adds transparent round corners.

Also the IMagick version that i am using is:

ImageMagick 6.6.2-10 2010-06-29 Q16 http://www.imagemagick.org 

Also the output(image generated) will get printed so I don't know if css will work over here.

Sorry, I am trying to actually create a new jpeg file with rounded corners from an already existing jpeg file that doesn't have round corners this is actually a photograph taken from a camera, so there are multiple/too many colors so I can't use gif as well.

Also my site will only just generate the round corner image then afterwards it will get downloaded using a FTP program by the admin of the site and then using a system software will get printed, so in short my website will not be printing the image but rather just generate it


回答1:


Try this:

<?php 
$input = 'example.jpg';
$size = getimagesize($input);
$background = new Imagick();
$background->newImage($size[0], $size[1], new ImagickPixel('red'));
$image = new Imagick($input);
$image->setImageFormat("png");
$image->roundCorners(575,575);
$image->compositeImage($background, imagick::COMPOSITE_DSTATOP, 0, 0);
$image->writeImage("rounded.jpg");
?>



回答2:


I may get downvoted, but I say let css deal with the corners and take some load off of your server :)

CSS rounded corners.




回答3:


JPG doesn't have a transparent color(s) (alpha channels) in its palette.

The output image must use either PNG or GIF (or another image format that supports alpha channels).

setImageBackgroundColor is another option if you want an opaque background.


EDIT

Your comment reminds me that you could try to use the command line; shell_exec() will run a command line argument from PHP. The command in the ImageMagick API you'll need to start with is convert example.jpg, and then you can pass flags with the various parameters you want.

Since ImageMagick is already installed, it will work right away. You may need to point your system PATH to the ImageMagick directory where all of the executables are.

There's plenty of questions and forums dedicated to rounded corners with this method so I'll leave that up to you.

Here's a helpful tip though - there is a silly confusion with the convert command, since Windows also has a convert.exe that is rarely used, but will confuse your command line, so make sure you're calling the right convert. ;) To test if it's working, try convert example.jpg example.gif (which should convert your example to a gif).

To get output from your command line, finish all commands with 2>&1 which will pipe cmd output back into PHP.



来源:https://stackoverflow.com/questions/13579272/add-round-corners-to-a-jpeg-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!