How to create canvas effect with Imagick? - php

I am trying to replicate the canvas texture style effect on a photo using the Imagick php class as seen here under the heading 'texture':
http://www.imagemagick.org/Usage/photos/
Using the command line it can be achieved like this:
composite texture_fabric.gif pagoda_sm.jpg \
-tile -compose Hardlight photo_texture.jpg
However I am using the 'Imagick' php class for ImageMagick and I can't seem to work out how to tile the texture.
I have tried:
$im = new Imagick("source.jpg")
$img2 = new Imagick("texture.gif");
$im->compositeImage($img2, imagick::COMPOSITE_HARDLIGHT, 0, 0);
But all I end up with is the texture image composited on the the source image in the top left corner.
How can I recreate the effect in the link?

Looking at the imagick php there is http://php.net/manual/en/imagick.textureimage.php
You may not be able to do what you want with Imagick as it has limited options.
A bit of lateral thinking:
Make a canvas the size of the image and tile the texture across it then composite it?
You could do it with ( untested ):
exec("composite texture_fabric.gif pagoda_sm.jpg -tile -compose Hardlight photo_texture.jpg");

Related

ImageMagick crop Image with a surrounding background

I am using imagemagick to crop an image (using the PHP interface, but i don't think that matters too much).
I wish to crop an image, but if the crop portion goes over the image, I want it to show a background colour.
Here is the code I have so far:
$newImg = new Imagick($imgUrl);
$newImg->cropImage($cropW, $cropH, $x, $y);
$newImg->resizeImage($resizedW, $resizedH, Imagick::FILTER_CATROM, 1);
$newImg->writeImage($output_filename);
However for some reason, imagemagick refuses to show any portion of the image that is further than boundary of the image (i.e. if x and y is larger than the original image width and height, it pushes it down into view of the image).
e.g.
I want it so that if x and y is beyond the image portion, it shows a background color instead. Thanks!
UPDATE
Thanks to namelivia suggestion I decided to use the "extent" tool.However I am unable to set a background colour using this tool through PHP. For example, the following produces a larger image but with a black background, NOT purple.
$newImg = new Imagick($imgUrl);
$newImg->setImageBackgroundColor('#e7609a'); //Doesn't return an error (i.e. returns true) but also does not work!
$newImg->setImageExtent(2000, 2000);
$newImg->writeImage($output_filename);
UPDATE 2
Seems like you should use extentImage (NOT setImageExtent) if you wish to use a background color.
I think you should use the extent option first, using extent you can also pick a background color for the area "behind" then you can crop the extended image.

How to generate images from PDF files in php?

I am using Imagick to product images from massive pdf files. I also want those images with RGB or sRGB color mode so Internet Explorer can display the images correctly.
I have tried
$im = new imagick($fileName.'[0]');
//$im->setImageColorspace(Imagick::COLORSPACE_SRGB); //try this already
// $im->setImageColorSpace(1); //try this already
$im->setResolution(300,300);
$im->setImageFormat('jpeg');
$im->writeImage($imageFile);
$im->clear();
$im->destroy();
I did get images but the color is way off with setImageColorspace and setImageColorSpace methods. (ex: color is inverted.)
If I comment out those methods, the images look right but some of them are not RGB mode and create problems in Internet Explorer.
I really need the RGB color mode on the images. Are there anyways to do it? Thanks so much!
You seem to encounter a problem with CMYK pdfs. Have you tried converting them to PNG? PNG -contrary to jpeg - only encodes RGB so the images will in any case be in the correct colorspace.
You might also want to have a look at ghostscript (the engine behind imagemagicks PDF conversion) and it's --UseCIE switch.
I wrote a php-wrapper to ghostscript which you can find at github that you might find usefull when you want to use ghostscript.

php image manipulation - fade to transparency

does anyone know how to apply fade effect to an image using PHP ? what I am looking for is a way to apply gradient transparency ( i mean : at the top , the image is opaque , which gradually gets more and more transparent , and at the bottom it is completely transparent).
i have been reading up on http://php.net/manual/en/function.imagecolortransparent.php , but did not see anything about applying a gradient effect to an image.
i also read : PHP - Generate transparency (or opacity) gradient using image , but it kinda trailed off without any solution!
I am also open to any other suggestion / libraries that can do this from command line.
Obviously you'll need to work with a png for this effect, but you can convert any png into a jpg using php. The following question I believe covers what you are asking about. Part of the code will have to be removed to clear the image reflection effect.
Can You Get a Transparent Gradient using PHP ImageMagick?
The piece of code which seems to do what you are trying to accomplish is:
$im = new Imagick('image.jpg'); //Reference image location
if (!$im->getImageAlphaChannel()) {
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
}
$refl = $im->clone();
$refl->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($refl->getImageWidth() + 10, $refl->getImageHeight() + 10, "gradient:transparent-black");

Image manipulation using PHP: force 1:1 aspect ratio (sqaure images)

I am looking to add a background to images that users upload that are not square. So if they upload a tall and skinny photo I want to add a white background to the sides of the image to make the resulting image have an aspect ratio of 1:1. Is this possible using PHP or javascript?
You can use the GD library for what, with a library called Wideimage it's a breeze:
$image = WideImage::load('img_form_field_name');
$size = max($image->getHeight(), $image->getWidth());
$white = $image->allocateColor(255, 255, 255);
$image->resizeCanvas($size, $size, 'center', 'center', $white);
See the documentation and examples, many functions can even be tested interactively.
The GD library is the most commonly used image manipulation package. It's a set of functions often installed with PHP which handle image manipulation.
What you'll want to do is either scale and crop your image to a specific aspect ratio so that you place your image on a square canvas and cut off whatever does fit or
You'll want to simply resize your image to a fixed aspect ratio and place it on a square canvas with whitespace around it.
Either way, this tutorial should point you in the right direction
http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/
Yep you'll want to look into either the GD library or ImageMagik. There are plenty of tutorials available for this task.
Functions like imagecreatetruecolor() etc will allow you to create a new image, and then stack the uploaded image on top of it and save it as a new file.
Yes.
http://www.php.net/manual/en/refs.utilspec.image.php

How to create watermark with imagemagick

I'm trying to create a watermark with ImageMagick however, the guides on layering are pretty daunting. Basically I'll have one base image say "base.jgp" and an overlay image say "overlay.jpg". Overlay.jpg would be smaller than base.jpg. Which exec command would I run to place overlay centered on top of base.jpg?
Thanks!
shell_exec("composite -gravity center ./images/watermark_horizontal.png {$this->path} {$this->path}");
Here we go
Check out ImageMagick examples, especially the Compositing Images chapter. It has a number of ready-made real-world examples.
$image = new Imagick();
$image->readImage("image.jpg");
// Open the watermark
$watermark = new Imagick();
$watermark->readImage("watermark.png");
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());

Categories