3

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

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Aditya
  • 395
  • 6
  • 18
  • Actual file size should not really be an issue. If PNG works for you then use PNGS since you get transparency built in. – zaf Nov 27 '12 at 07:40
  • Problem is that if i use png then not only the file size increase but so does the execution time For Jpeg it is 5 Sec for PNG it is 80 Secs – Aditya Nov 27 '12 at 07:43
  • Then what you need is more RAM! :) – zaf Nov 27 '12 at 07:44
  • Can you just test the above code in your pc because even the photoshop is taking too long to convert and save a jpeg file which is 5 mb in size to a png file. Also i have 3 gb RAM with me. The resolution of the file is 5200x3200 – Aditya Nov 27 '12 at 08:46
  • 1
    I don't have the same setup to test. – zaf Nov 27 '12 at 11:34

3 Answers3

3

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");
?>
Bonzo
  • 5,169
  • 1
  • 19
  • 27
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.

keyboardSmasher
  • 2,661
  • 18
  • 20
  • the output will get printed so i don't know if css will work over here – Aditya Nov 27 '12 at 07:34
  • 1
    You're pulling an image from the file system, adding rounded corners and serving it up. What a waste of resources. I just realized that there is no backtick key on my iPad in Jump remote desktop...ha – keyboardSmasher Nov 27 '12 at 07:44
  • Ha, so now we have to read their minds when they ask specific questions with blocks of code attached? :) – keyboardSmasher Nov 27 '12 at 07:50
  • my first comment should be – keyboardSmasher Nov 27 '12 at 07:55
  • @keyboardSmasher i have already said that i will be printing the image in the end, if that weren't the case then i would have simply used webkit solution, but i need to modify the image itself, you really don't need to read my mind just read the question more carefully also $echo is only for testing purpose – Aditya Nov 27 '12 at 08:29
  • 1
    Use print stylesheet, it's with media="print" when you including you css on a page. – starikovs Nov 27 '12 at 08:56
  • can you please tell me something more about it i have no idea about it, also my site will only just generate the round corner image then afterwards it will get downloaded using a FTP program by the admin and then using a system software will get printed – Aditya Nov 27 '12 at 09:01
  • 1
    @Aditya - in that case this solution won't work. You have the lines `header('Content-type: image/jpeg'); echo $image;` in your example, which makes it look like your image is served via HTTP to a webpage, where CSS can be applied. If that's not the case, then CSS is not an option. – Ben Nov 28 '12 at 00:22
  • yes the header('Content-type: image/jpeg'); echo $image; is for only testing purpose i will remove it from the final draft – Aditya Dec 01 '12 at 14:17
2

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.

Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224
  • transparent is not what i am aiming for i just need to change the black color that is getting displayed as the background – Aditya Nov 27 '12 at 07:40
  • I tried "setImageBackgroundColor" even then it won't work I also tried $image->setBackgroundColor(new ImagickPixel("red")); $image->setBackgroundColor("red"); $image->setImageBackgroundColor("red"); One at a time but none of them seems to work – Aditya Nov 27 '12 at 08:30
  • i once read someone wrote that you need to add -alpha off:- "adding this prevents the JPG to be created with a black background" But it didn't say where exactly or how am i suppose to add it to my php script – Aditya Nov 27 '12 at 08:40