unrecognized color `00000f' with Convert Command - php

I am getting the error while trying to run convert command. Here is the command.
convert -colorspace rgb 10338_1.ai -transparent 00000f 10338_1.png
and i am getting the error is
convert: unrecognized color `00000f' # color.c/GetColorInfo/965.
Any Solution?

The color should be given in HTML notation with hash in front of it:
convert -colorspace rgb 10338_1.ai -transparent '#00000f' 10338_1.png

The previous answer is correct about the hashtag. For more info see http://www.imagemagick.org/script/color.php.

We can analyse your image by looking at its histogram, like this:
convert a.ps -format "%c" histogram:info:
608007: ( 0, 0, 0, 0) #0000000000000000 cmyk(0,0,0,0)
58793: ( 0, 0, 0,65535) #000000000000FFFF cmyk(0,0,0,255)
6551: ( 0, 0, 0,34952) #0000000000008888 cmyk(0,0,0,136)
5095: ( 0, 0, 0,48059) #000000000000BBBB cmyk(0,0,0,187)
4350: ( 0, 0, 0,17476) #0000000000004444 cmyk(0,0,0,68)
3297: ( 0, 0, 0,61166) #000000000000EEEE cmyk(0,0,0,238)
2897: ( 0, 0, 0, 4369) #0000000000001111 cmyk(0,0,0,17)
First, you notice it is in CMYK colourspace, not RGB. You can see that the predominant colour is black with 608007 pixels, and that all the other colours in the image are actually just shades of black, but all have zero values for the CMY components. That means you lose nothing by just extracting the blacks into a greyscale image like this:
convert a.ps -channel K out.png
Maybe now you can do what you want, for example you can set the white pixels transparent like this:
convert out.png -transparent white result.png

Related

How to make specified area of an image transparent with Imagick?

I want to make a part of an image transparent, I tried the code below, even tried many constants as COMPOSITE_DSTOUT, but all didn't work, does anyone know how to?
$fooImage->newImage(256, 256, new ImagickPixel('transparent'));
$Image->compositeImage($fooImage, Imagick::COMPOSITE_DSTOUT, $offsetX, offsetY);
I tested the code below, just got yellow with black, not transparent:
$width = 256;
$height = 256;
$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('yellow'));
$x = 50;
$y = 100;
$fooWidth = 100;
$fooHeight = 60;
//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('none'));
$mask->setImageFormat('png');
//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle($x, $y, $x + $fooWidth, $y + $fooHeight);
$mask->drawImage($draw);
//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_DSTOUT, 0, 0,
Imagick::CHANNEL_ALPHA);
$image->setImageFormat('png');
$image->writeImage($path);
Got black inside yellow, not transparent in yellow
You need to make a black and white mask image the size of your input (white where you want it to be opaque and black where you want it to be transparent). Then use the equivalent of -compose copyopacity -composite to put the mask into the alpha channel of the image. Sorry, I do not code Imagick.
Here is an example using ImageMagick command line syntax:
Input:
convert logo.jpg \( -size 640x480 xc:white -size 200x200 xc:black -geometry +200+100 -compose over -composite \) +geometry -alpha off -compose copy_opacity -composite result.png
You can see it is transparent by compositing it over another image (in this case a checkerboard).
convert ( -size 640x480 pattern:checkerboard ) result.png -compose over -composite result2.jpg
Do you try \Imagick::COMPOSITE_COPYOPACITY ?
Because that's probably the right one.

PHP GD image merge changing my image to black

I have a very frustrating situation. I am using PHP GD for the first time, and it's been a bit of a rollercoaster relationship. Basically, I am trying to merge 2 images, a square one (with a height/width of x) onto a rectangle (with a width of x and a height of y).
The square needs to be centered vertically. But this isn't the issue - I've managed to position it correctly.
Whats happening is, my rectangle is white. My square has a white background, so when the images are merged, it should just look like my asset on a white rectangluar background.
When I merge the image though, GD is for some reason changing my background white rectangle to black - so you can see the white square in the middle, with black "bars" on top and bottom. Can anyone help?
Code is:
//create copy of original image to correct size
imagecopyresized($dst_image, $src_image, 0,0,0,0,$x_width,$x_height,$orig_img_x_width,$orig_img_x_height);
imagejpeg($dst_image, "resized_copy.jpg", 100);
$img = imagecreatetruecolor(1333, 2000);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill ( $img, 0, 0, $white );
imagefilledrectangle($img,0,0,1333,2000, $white);
imagejpeg($img, "rectangle.jpg", 100);
//merge images
$dest2 = imagecreatefromjpeg("rectangle.jpg");
$src2 = imagecreatefromjpeg('resized_copy.jpg');
imagecopymerge($dest2, $src2, 0, 0, 0, -333.5, $x_width, $x_height, 100);
imagejpeg($dest2, "final_image.jpg", 100);
I've tried using imagecopy instead of imagecopymerge, but I get the same result. I'm sure there is a simple explanation, but I cant seem to find it trawling through the php manual.
I've read your question a few times but I'm not convinced I understand exactly what you are trying to achieve so I've made a few assumptions in producing the below code.
For the sake of simplicity I've created a 'square.jpg' test image file like so:
(Note that I've used small image sizes here so I can show them inline.)
// read in the square test image.
$square = imagecreatefromjpeg('square.jpg');
$square_x = imagesx($square); // 100px
$square_y = imagesy($square); // 100px
// create the rectangular image to merge with.
$rectangle = imagecreatetruecolor(100, 200);
$rectangle_x = imagesx($rectangle); // 100px
$rectangle_y = imagesy($rectangle); // 200px
// note that this isn't white, but rather a lovely shade of blue to better
// show the image on the white SO background!
$white = imagecolorallocate($rectangle, 128, 128, 255);
imagefill($rectangle, 0, 0, $white);
// merge the images.
imagecopymerge(
$rectangle,
$square,
0,
($rectangle_y / 2) - ($square_y / 2), // to vertically centre the square.
0,
0,
$square_x,
$square_y,
75 // Just to show the merge clearly; change back to 100 for your usage.
);
imagejpeg($rectangle, 'final_image.jpg', 100);
imagedestroy($rectangle);
imagedestroy($square);
This gives me the following image in final_image.jpg:

Imagick Equivalent Of This Imagemagick Command?

I'm trying to create a vignette effect, and I can get desired result through command line. But when I try it with Imagick, I'm not getting the desired result
convert i.jpg ( -size 150x150 radial-gradient:black-white -gravity center -crop 100x100+0+0 +repage ) -compose multiply -flatten o.jpg
I tried the following Imagick commands
$gra = new Imagick();
$gra->newPseudoImage(150, 150, "radial-gradient:black-white");
$gra->cropThumbnailImage(100, 100);
$gra->setImagePage(100, 100, 0, 0);
$img = new Imagick("i.jpg");
$img->compositeImage($gra, Imagick::COMPOSITE_MULTIPLY, 0, 0);
$img->flattenImages();
$img->writeImage("o.jpg");
Thanks for any help!

php imagecolorat returns 0. Why?

Sometimes imagecolorat() returns 0 with some pixels in a PNG I'm analyzing. Why is that?
I looked the pixel and the color really is #111111. So it should return 1118481. Right?
I tried using imagealphablending($img,true) but I still get 0.
Thanks!
$img = #imagecreatefrompng($png);
if(!$img){
throw new Exception("Error loading PNG.");
}
var_dump(imagecolorat($img, 37, 625));
Result:
int 0
If image is loaded correctly and you get 0, then it means color is pure black (RGB => 0x000000 => 0)
Because that is the color of that pixel: black (red = 0, green = 0, blue = 0)
If you try it for most other pixels you will get 16777215 (0xFFFFFF), which is white (red = 255, green = 255, blue = 255).
Or the color is black, or I think the image is not loading correctly. At least not the .png you put as an example. Maybe the # is hiding the error of the image not loading. Its url is redirecting to another, and in that case you should use something like CURL to find the real image.

Imagemagik and php imagecopy doesn't preserve transparency

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.

Categories