Im facing a strange problem when i use imagemagik converted image with php imagecopy.. Here is my code
$cmd = "convert ".realpath($temp1)." -alpha set -channel alpha -background none -vignette 0x3 ".$dest_img;
exec($cmd);
This code makes the edges feathered so i can have a clean merging.. Here is my code for merging image
$image = imagecreatefromjpeg($postcard_img);
$frame = imagecreatefrompng($thumb_img);
list($w, $h) = getimagesize($postcard_img);
imagealphablending($frame,true);
//imagecopymerge($image, $frame, $coords[0], $coords[1], 0, 0, $w, $h, 100);
imagecopy($image, $frame, $coords[0], $coords[1], 0, 0, $w, $h);
And here is the result
Okay now the strange stuff.. IF I don't use ImageMagik and simply merge it by replacing imagecopy() with imagecopymerge() it works then but leaves rough edges.. Here is the code of imagecopymerge()
$postcard_img = $postcard[0]["ms_filepath"].$postcard[0]["ms_filename"];
$thumb_img = $thumb[0]["ms_thumbpath"].$thumb[0]["ms_thumbname"];
$image = imagecreatefromjpeg($postcard_img);
$frame = imagecreatefrompng($thumb_img);
list($w, $h) = getimagesize($postcard_img);
imagealphablending($frame,true);
imagecopymerge($image, $frame, $coords[0], $coords[1], 0, 0, $w, $h, 100);
And here is the result (See the rough edges)
All i need is the feathered image.. Any idea why its happening and how to overcome it?
No. Resistance is futile.
convert ( frame.png -channel alpha -blur 0x5 -level 50%,100% ) ( avatar.jpg -geometry +70+120 ) -compose dstover -composite output.png
You can always make frame with feathered edges in Photoshop (or similar) and just put one on top of another.
Related
I want to display an image into three parts as triptych with the help of imagemagick PHP library. I saw the same example here
http://www.rubbleimages.com/Canvas.php.
Can anyone help me on this issue please
I am hopeless at PHP but something like this seems to work for me:
#!/usr/local/bin/php -f
<?php
$padding=10;
$info = getimagesize("input.jpg");
$width=$info[0];
$height=$info[1];
// Calculate dimensions of output image
$canvasWidth=$width+4*$padding;
$canvasHeight=$height+2*$padding;
// create white canvas
$output = imagecreatetruecolor($canvasWidth, $canvasHeight);
$background = imagecolorallocate($output, 255, 255, 255);
imagefill($output, 0, 0, $background);
// read in original image
$orig = imagecreatefromjpeg("input.jpg");
// copy left third to output image
imagecopy($output, $orig,$padding, $padding, 0, 0, $width/3, $height);
// copy central third to output image
imagecopy($output, $orig,2*$padding+$width/3, $padding, $width/3, 0, $width/3, $height);
// copy right third to output image
imagecopy($output, $orig,3*$padding+2*$width/3,$padding, 2*$width/3, 0, $width/3, $height);
// save output image
imagejpeg($output,"result.jpg");
?>
If I start with this:
I get this as a result
I added the black outline afterwards so you can see the extent of the image.
$cmd = " convert $photo -crop 33.5%x100% +repage -raise 5x1 "." monet_horizontal_%d.jpg";
exec($cmd);
$cmd = " convert monet_horizontal_0.jpg wt.png monet_horizontal_1.jpg wt.png monet_horizontal_2.jpg +append "." abc.jpg";
exec($cmd);
I'm trying to adapt the script available here [http://www.fmwconcepts.com/imagemagick/redeye/index.php][1] in PHP using the API version of Imagick.
Actually, the selection is done in jQuery then passed to the following script to draw the selection.
Here's my code so far:
<?php
extract($_POST);
// Load the original image
$image = new Imagick($path);
// Duplicate and desaturate the original image
$image2 = clone $image;
$image2->modulateImage(15, 0, 100);
// Create the mask on which the selection will be drawn
$image3 = clone $image;
$image3->colorfloodfillimage(new ImagickPixel('white'), 18, new ImagickPixel('white'), 0, 1);
foreach ($redeye as $selection){
try{
$draw = new ImagickDraw();
$draw->setstrokewidth(0);
$draw->setstrokecolor(new ImagickPixel('black'));
$draw->setFillColor(new ImagickPixel( 'black' ));
$draw->setfillalpha(1);
$draw->ellipse( $selection['ox'], $selection['oy'], $selection['rx'], $selection['ry'], 0, 360 );
$image3->drawImage($draw);
} catch (ImagickException $ie){
echo $ie->getMessage();
}
}
?>
I know very little about image processing and don't know how to merge the 3 layers.
I don't really understand the code of the convert function when merging the layer.
Any help would be greatly appreciated!
EDIT:
convert $tmpA1 $tmpA2 $tmpA3 -compose over -composite $tmpA2
As I understand this should be written like the following using the API:
$image2->compositeImage($image3,Imagick::COMPOSITE_OVER, 0, 0);
$image2->compositeImage($image2,Imagick::COMPOSITE_OVER, 0, 0);
$image2->compositeImage($image,Imagick::COMPOSITE_OVER, 0, 0);
Then
convert $tmpA2 \( $tmpA1 -modulate 100,0,100 \) $tmpA4 -compose over -composite $outfile
Should be written like that:
$image->modulateImage(100, 0, 100);
$image->compositeImage($image,Imagick::COMPOSITE_OVER,0, 0);
$image->compositeImage($image2,Imagick::COMPOSITE_OVER,0, 0);
But I don't understand this part:
convert $tmpA3 ( +clone -morphology close disk:$rad $dilation ) -compose difference -composite -auto-level -negate -threshold 0 -negate $tmpA4
(1) imagecreate
(2) imagecreatetruecolor
(3) imagecopy
(4) imagecopymerge
I use above PHP function and get confused.
First I prepare two png files...
1.png http://www.capbite.com/subfolder/lion/test/1.png
A QRcode with transparent background and black dot, I create it by these code (only keypoint, not all).
$image = imagecreate($width, $height);
imagesavealpha($image, true);
$color1 = imagecolorallocatealpha($image, 0, 0, 0, 127);
imageColorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $color1);
...
imagepng($image, $filename);
2.png http://www.capbite.com/subfolder/lion/test/2.png
I make a png and use a color to fill its background by MS paint.
Prepare done. Then the code is
<?php
//use imagecreate or imagecreatetruecolor
$image = imagecreate(50, 50);
//$image = imagecreatetruecolor(50, 50);
//save the alpha channel
imagesavealpha($image, true);
//alphablending set false, then transparent color can cover the canvas
imagealphablending($image, false);
//take a transparent color and fill it
imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));
//draw the ellipse
imagefilledellipse($image, 15, 15, 30, 30, imagecolorallocate($image, 255, 0, 0));
imagepng($image, '3.png');
imagedestroy($image);
//merge image
$im_background = imagecreatefrompng('1.png');
$im_foreground = imagecreatefrompng('3.png');
list($width, $height) = getimagesize('3.png');
//use imagecopy or imagecopymerge
imagecopy($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height);
//imagecopymerge($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height, 100);
imagepng($im_background, 'x-x.png');
imagedestroy($im_background);
imagedestroy($im_foreground);
The code make 3.png like this www.capbite.com/subfolder/lion/test/3.png
Now if I use
1.png + imagecreate + imagecopy will get http://www.capbite.com/subfolder/lion/test/4-1.png
1.png + imagecreate + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/4-2.png
1.png + imagecreatetruecolor + imagecopy will get http://www.capbite.com/subfolder/lion/test/4-3.png
1.png + imagecreatetruecolor + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/4-4.png
2.png + imagecreate + imagecopy will get http://www.capbite.com/subfolder/lion/test/5-1.png
2.png + imagecreate + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/5-2.png
2.png + imagecreatetruecolor + imagecopy will get http://www.capbite.com/subfolder/lion/test/5-3.png
2.png + imagecreatetruecolor + imagecopymerge will get http://www.capbite.com/subfolder/lion/test/5-4.png
It is hard to describe the image that made by different ways, so I paste the link about the images, but my reputation is not enough to post more links or images... -- Edited by Niet ;)
My question is, just the 3.png, it has a transparent background, even use different create function, but when it copy to another image which I created or by MS paint, the result is I meet.
What's different between imagecreate & imagecreatetruecolor make 4-1.png and 4-3.png ?
What's different between imagecopy & imagecopymerge(even pct set 100) make 4-3.png and 4-4.png ?
What's different between the images which I created or by MS paint make 4-3.png and 5-3.png ?
Thanks a lot!
I would recommend using imagecreate, not imagecreatetruecolor.
When using imagecreate, the first colour you define will be the background colour that fills the entire canvas. This will usually be your "transparent" colour, however it is generally better (read: more efficient in the file) to do it this way:
imagecolortransparent($img, imagecolorallocate($img, 255, 0, 255));
In this way, you aren't using an alpha channel, you're just telling it "anything I draw in bright pink will be transparent" - this is how transparency works in GIF images, by the way, and it's also the "traditional" way to do it, way back on old games from many many years ago!
Using imagecreate-generated images, copying works a lot easier, because GD knows exactly what colour is "transparent" and therefore shouldn't be copied on to the target. When using imagecreatetruecolor, then you get into the complicated and messy business of compositing...
I hope this helps you out. GD can be a tricky beast to master, but once you know the basics you should be good to go.
Before I start, I just want to note that I am a PHP noob. What I want to do is tint a PNG image to one color. So all transparent pixels will remain transparent, and all non-transparent pixels will be that color. I have searched many sites for this answer but for some reason I can't find what I want.
Here is my first attempt based on different examples I found:
<?php
header('Content-Type: image/png');
$color = $_GET['color'];
$im = imagecreatefrompng($_GET['img']);
$width = imagesx($im);
$height = imagesy($im);
$imn = imagecreatetruecolor($width, $height);
imagealphablending($imn,false);
$col=imagecolorallocatealpha($imn,255,255,255,127);
imagesavealpha($imn,true);
imagefilledrectangle($imn,0,0,$width,$height,$col);
imagealphablending($imn,true);
imagecopy($imn, $im, 0, 0, 0, 0, $width, $height);
imagefilter($imn, IMG_FILTER_GRAYSCALE);
if ($color[0] == '#')
$color = substr($color, 1);
if (strlen($color) == 6)
$r = $color[0].$color[1];
$g = $color[2].$color[3];
$b = $color[4].$color[5];
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
imagefilter($imn, IMG_FILTER_COLORIZE, $r, $g, $b);
imagepng($imn);
imagedestroy($imn);
?>
Essentially a perfect example of what I want can be seen here. The only change will be that instead of black, I want it to be converted to the color the user specifies.
Convert non-transparent pixels to black
Thank You
===============================
10/17/2012 Update
So based on xception's answer, here is the code that I used to execute his script:
<?php
$source = "test.png";
$temp = "temp.png";
$color = "red";
$final = "FINAL.png";
exec("convert $source -alpha extract -threshold 0 -negate -transparent white $temp");
exec("convert $temp -fill $color -opaque black $final");
?>
It worked, however there is a small problem. As illustrated in the screenshots below, there are jagged edges. Any ideas on how to smooth the image so it looks as nice as it did in the BEFORE screenshot?
BEFORE:
AFTER:
Example in 2 steps based on the link you pointed to:
convert <source> -alpha extract -threshold 0 -negate -transparent white <tmp>
convert <tmp> -fill red -opaque black <destination>
replace <source>, <tmp>, <destination> with appropriate file names, replace red with the color you want.
EDIT:
shorter version found by question author:
exec("convert $source -threshold 100% +level-colors '#00FF00', $final");
I want to convert a alpha transparent png image to palette based png image.
In GD I can do it easy:
// We have already the image loaded in $source_img
$w=200; $h=200; // We supose that img dimensions are 200x200
$img = imagecreatetruecolor($w, $h); // New black image
list($r, $g, $b) = array(200, 200, 200); // Some color that doesn't appear in image to avoid conflict
$color = imagecolorallocate($img, $r, $g, $b);
imagefill($img, 0, 0, $color); // Fill the black image with the chosen color.
imagecolortransparent($img, $color); // Set the chosen color as transparent
$res = imagecopyresampled($img, $source_img, 0, 0, 0, 0, $w, $h, $w, $h);
But in Imagick I don't know how set a color as transparent (imagecolortransparent() in GD). I have spent hours searching on the internet, but the help in the php site isn't very comphrensive and there are many undocumented functions.
Thanks.
On the command line I would do something like this:
convert test.png -transparent-color white PNG8:converted.png
But there seems to be a problem in some IM versions with this type of conversion, I found this usergroup post by some guy who seems to have similiar problems: http://studio.imagemagick.org/pipermail/magick-users/2009-May/022534.html
Are you using the command line when working with IM or do you use the PHP module ( http://de.php.net/manual/en/book.imagick.php )?