Image from RGB To Cmyk in php - php

I've some problem with php and imagick, i want to convert some image from rgb system to cmyk, but i've only black/or non background nothing else.
$icc_cmyk = file_get_contents('USWebUncoated.icc');
$img->profileImage('icc', $icc_cmyk);
$img->setImageColorspace(12);
if ($php_vs < 5.3) {
//ADJUST GAMMA BY 20% for 5.2.x
$img->levelImage(0, 2.0, $range['quantumRangeString']);
} else {
//php 5.3 hack FOR INVERTED COLORS
$img->negateImage(false, Imagick::CHANNEL_ALL);
}
$img->stripImage();
//$img->setImageColorspace(Imagick::COLORSPACE_CMYK);
$img->writeImage('cmyk.png');

Use this working RGB to CMYK Image with Php
$icc_cmyk = file_get_contents('images/CoatedFOGRA27.icc');// Your Cmyk ICC Profile
$img->profileImage('icc', $icc_cmyk);
$img->transformimagecolorspace(Imagick::COLORSPACE_CMYK);
$img->writeImage('cmyk.jpg');// Save as jpg or jpeg`

All right I've some resolve but i don't know it's working, if someone can taste it i will be very greatfull :)
$img->setImageColorspace(13);
$icc_rgb = file_get_contents('AdobeRGB1998.icc');
$img->profileImage('icc', $icc_rgb);
unset($icc_rgb);
$icc_cmyk = file_get_contents('USWebUncoated.icc');
$img->profileImage('icc', $icc_cmyk);
$img->setImageColorspace(12);
unset($icc_cmyk);
$img->setimagecolorspace(Imagick::COLORSPACE_CMYK);
$img->stripImage();
$img->writeImage('cmyk.png');

Related

Imagick convert CMYK JPG to PNG and transparency

I have a CMYK JPG file which I need to cut some parts from and then save into a CMYK PNG file. The problem is that even I just open the JPG file and save it into a PNG file the colors are shifted massively. I've been trying to fix that for a day now and have exhausted all available resources on SO and Google.
Here's my code:
$imageStick = new Imagick("original.jpg"); // CMYK JPG
$largeStick = new Imagick(); // create a bigger PNG file with transparent background
$largeStick->newImage($smallStick->getimagewidth(), $smallStick->getimageheight() * 3, new ImagickPixel('transparent'), 'png');
$largeStick->compositeimage($smallStick, Imagick::COMPOSITE_DEFAULT, 0, 0);
$largeStick->writeimage("resampled.png");
And here are the images:
- Original - Download File
- Resampled
Any ideas why would that be happening?
I've tried copying all possible profiles and image properties from the original jpg file:
foreach ($smallStick->getimageprofiles() as $name => $profile)
!empty($profile) && $largeStick->setimageprofile($name, $profile);
foreach ($smallStick->getimageproperties() as $name => $property)
!empty($profile) && $largeStick->setimageproperty($name, $property);
$largeStick->setcolorspace($smallStick->getcolorspace());
manually setting up an ICC profile but no luck at all:
$icc_cmyk = file_get_contents('USWebUncoated.icc');
$largeStick->profileImage('icc', $icc_cmyk);
Any thoughts on this would be highly appreciated!
Thanks!

Imagick jpg background isn't transparent on rotate

I'm currently using imagemagick version ImageMagick 6.8.4-6 2013-04-04 Q16
with Imagick extension version 1620
I am trying to rotate a jpg image and merge this into another jpg image however when i merge the image i get a black box arround the image.
Please see the code i am using below:
public function image($images,$x,$y,$angle){
if($images != "" && $images != NULL){
$base = $this->instance;
$layer = new Imagick($images);
//resize image
if($this->id == 45){
$layer->scaleImage(329,0);
}
if($this->id == 44){
$layer->scaleImage(280,0);
}
if($this->id == 42){
$layer->scaleImage(350,0);
}
//rotate image
$layer->rotateImage(new ImagickPixel("none"), $angle);
//Merge Image
if($this->id == 44){
$base->compositeImage($layer, imagick::COMPOSITE_OVER, $x, $y);
}else{
$base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, $x, $y);
}
$this->image = $base;
}
}
The the test is currently been run when $this->id uses 44.
Can anyone shed light on this issue?
Thanks in advance
You need to use imagecolortransparent in order to have transparency capabilities.
N.B. JPG does not have transparent properties, only PNG and GIF files (and TIFF) but browsers do not support that format.
Your output file will need to be converted to one of those formats, preferably PNG then set the transparency for the desired color.
"so would you suggest converting to PNG then rotating? then merging the PNG image into the JPG (if it is possible) as the image output is required to be a jpg"
You will lose transparency as soon as you resave as JPG
I came across this via Google, here's the correct answer as others may find it. You need to use setImageMatte(1) to enable the transparency, e.g.
$src->setImageMatte(1);
$mask->rotateImage(new ImagickPixel('#00000000'), 10);
You may also use an image Mask, where black will become transparent using
$src->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);

PHP GD Image Library change image color - gif works but jpegs wont

Thanks for taking the time to read my problem:
I'm using the following code to get an image and then change the color 201,2,255 (r,g,b) - which is a shade of purple then output the image.
$imgname = "input.gif";
$im = imagecreatefromgif ($imgname);
$index = imagecolorclosest ( $im, 201,2,255 ); // get White COlor
imagecolorset($im,$index,60,140,48); // SET NEW COLOR
$imgname = "output.gif";
imagegif($im, $imgname ); // save image as gif
imagedestroy($im);
This works perfectly which can be seen here : http://www.office-desks.co.uk/cache_images/test.php (top 2 images)
The problem is when I try todo exactly the same but using a jpeg instead it doesn't work.. (bottom 2 images)
$imgname = "input.jpg";
$im = imagecreatefromjpeg ($imgname);
$index = imagecolorclosest ( $im, 201,2,255 ); // get pink/purple COlor
imagecolorset($im,$index,60,140,48); // SET NEW DECENT COLOR
$imgname = "output.jpg";
imagejpeg($im, $imgname ); // save image as gif
imagedestroy($im);
If anyone could help me shed some light on the problem, would be much appreciated. Thanks in advance all.
A bit late, but I think that has to do with the 'artifacts' (compression errors) generated by JPG. Zoom in on a JPG and you see 'grains' of pixels that won't match the neighbors. This means big planes of a single color will become many colors and not 1 single value.
Stick with lossless image formats like gif or png.

circularize an image with imagick

Trying to take a rectangular photo, crop it into a square region, and then mask it into a circular with a transparent background.
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$circle = new \Imagick();
$circle->newImage($dims['w'], $dims['h'], 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new \ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle($dims['w']/2, $dims['h']/2, $dims['w']/2, $dims['w']);
$circle->drawimage($draw);
$imagick = new \Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage($dims['w'], $dims['h'], $dims['x'], $dims['y']);
$imagick->compositeimage($circle, \Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($tempfile);
$imagick->destroy();
The result is the rectangular image, uncropped and without being circularized. What am I doing wrong?
Example image:
Example input for $dims = {"x":253,"y":0,"x2":438.5,"y2":185.5,"w":185.5,"h":185.5}
Rough expected output:
Image i'm getting looks roughly like the input image.
For those with an older version of Imagick (setimagematte does not exist in version lower than 6.2.9), I came up with an easy solution. The thing here is to copy opacity from the mask to the original image.
Original Image:
Mask:
Result:
The code:
$base = new Imagick('original.jpg');
$mask = new Imagick('mask.png');
$base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$base->writeImage('result.png');
You could use an Imagick black circle as mask but I though it wasn't perfect so I used my own.
Of course you will certainly have to resize / crop your images but that's another story.
Hope this helps.
J.
This works for me:
<?php
//$dims is an array with the width, height, x, y of the region in the rectangular image (whose path on disk is $tempfile)
$tempfile = 'VDSlU.jpg';
$outfile = 'blah.png';
$circle = new Imagick();
$circle->newImage(185.5, 185.5, 'none');
$circle->setimageformat('png');
$circle->setimagematte(true);
$draw = new ImagickDraw();
$draw->setfillcolor('#ffffff');
$draw->circle(185.5/2, 185.5/2, 185.5/2, 185.5);
$circle->drawimage($draw);
$imagick = new Imagick();
$imagick->readImage($tempfile);
$imagick->setImageFormat( "png" );
$imagick->setimagematte(true);
$imagick->cropimage(185.5, 185.5, 253, 0);
$imagick->compositeimage($circle, Imagick::COMPOSITE_DSTIN, 0, 0);
$imagick->writeImage($outfile);
$imagick->destroy();
?>
<img src="blah.png">
I always try to keep the code simple until I get it working and then add all the variables etc. That could be the problem or there could be a problem with your version of Imagick.
It's namespaced
Still do not know what it means! - I am getting a bit behind with php as I do not use it very much these days.
There's also another workaround that I suggest here :
// create an imagick object of your image
$image = new \Imagick('/absolute/path/to/your/image');
// crop square your image from its center (100px witdh/height in my example)
$image->cropThumbnailImage(100, 100);
// then round the corners (0.5x the width and height)
$image->roundCorners(50, 50);
// force the png format for transparency
$image->setImageFormat("png");
// write the new image
$image->writeImage('/absolute/path/to/your/new/image');
// done!
Many thanks to all previous answers and contributors that lead me to this code!
Feel free to test/comment my solution!
I stumbled upon this as I was searching for a similar solution for Ruby on Rails, notice that this Stackoverflow question uses vignette instead which seems to be a much simpler way to solve the problem.
I used vignette to solve my problem with rounded images in Ruby on Rails using Dragonfly.

Imagick Resize, Center, and Sparse Fill problems

My end goal here is to resize the input image to 100px width, 125px height. Some of the input images are a different Aspect Ratio, so I wish for them to be in a 100x125 container with the background sparse filled from their edge color.
Ok, so this works for the basic resize:
$image = new Imagick($imgFile);
$image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
$image->writeImage("$Dir/$game.png");
header("Content-type: ".$image->getImageFormat());
echo $image;
$image->clear();
$image->destroy();
However I've been searching for hours, and I cannot find a simple "This is how you center an image in a canvas" bit for PHP's Imagick library. Everything is for the actual ImageMagick convert application, which is not really what I'm after. I've tried compositing the resized image into an empty newImage with the set width and height, but it just seems to overwrite the dimensions regardless of the composite type, setting the Gravity to center and then the extent to 100x125 has no effect ( It always sits at 0,0, and trying to set the y offset to ((125-imageheight)/2) resulted in an offset that was way more than it should have been )
Edit:
$imageOutput = new Imagick();
$image = new Imagick($imgFile);
$image->resizeImage(100,0, Imagick::FILTER_LANCZOS, 1, false);
$imageOutput->newImage(100, 125, new ImagickPixel('black'));
$imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((125 - $image->getImageHeight()))/2 );
$imageOutput->setImageFormat('png');
$imageOutput->writeImage("$Dir/$game.png");
header("Content-type: ".$imageOutput->getImageFormat());
echo $imageOutput;
$image->clear();
$image->destroy();
So I got my centering working, gravity apparently has no effect on actual images.
I have absolutely no idea where I would even begin to try and recreate a command line edge-in sparse fill in PHP with the library.
I ended up using a combination of Imagick and shell calls to convert itself, I'll eventually rewrite it to use entirely shell calls. I also changed my dimensions, here's the code:
$imageOutput = new Imagick(); // This will hold the resized image
$image = new Imagick($imgFile); // Open image file
$image->resizeImage(120,0, Imagick::FILTER_LANCZOS, 1, false); // Resize it width-wise
$imageOutput->newImage(120, 150, "none"); // Make the container with transparency
$imageOutput->compositeImage($image, Imagick::COMPOSITE_ADD, 0, ((150 - $image->getImageHeight())/2) ); // Center the resized image inside of the container
$imageOutput->setImageFormat('png'); // Set the format to maintain transparency
$imageOutput->writeImage("$Dir/$game.temp.png"); // Write it to disk
$image->clear(); //cleanup -v
$image->destroy();
$imageOutput->clear();
$imageOutput->destroy();
//Now the real fun
$edge = shell_exec("convert $Dir/$game.temp.png -channel A -morphology EdgeIn Diamond $Dir/$game.temp.edge.png"); // Get the edges of the box, create an image from just that
$shepards = shell_exec("convert $Dir/$game.temp.edge.png txt:- | sed '1d; / 0) /d; s/:.* /,/;'"); // get the pixel coordinates
$final = shell_exec("convert $Dir/$game.temp.edge.png -alpha off -sparse-color shepards '$shepards' png:- | convert png:- $Dir/$game.temp.png -quality 90 -composite $Dir/$game.jpg"); // Sparse fill the entire container using the edge of the other image as shepards , then composite that on top of this new image
unlink("$Dir/$game.temp.png"); // cleanup temp files
unlink("$Dir/$game.temp.edge.png");
set_header_and_serve("$Dir/$game.jpg"); // serve the newly created file

Categories