I am creating an application in php in which i upload a image and resize it and place it on a Coffee Mug. Coffee mug is a background image of a div. Now i want to save both images as a single image it would look like a screenshot of a coffee mug with the image printed on it. How to take screenshot of only mug and image on it or if there is an another way to doing it in php.
You need an image library for that. Most PHP versions on many servers are precompiled with GD. This is a limited and not so well performing library, so most people will use Imagemagick.
In GD:
$mug = imagecreatefrompng('mug.jpg');
$overlay = imagecreatefromjpeg('photo.png');
imagecopy($mug, $overlay);
In Imagemagick:
$mug = new Imagick("mug.jpg");
$overlay = new Imagick("logo.png");
$composition = new Imagick();
$composition->compositeImage($mug);
$composition->compositeImage($overlay);
You can do this by using the GD2 library:
<?php
$image = imagecreatefromstring(file_get_contents($image_path);
$coffe_mug= imagecreatefromstring(file_get_contents($coffe_mug_path));
// merge the two
imagecopymerge($image, $coffe_mug, 10, 10, 0, 0, 100, 47, 75);
// save, can also be imagepng, imagegif, etc, etc
imagejpeg($image, '/path/to/save/image.png');
?>
You can also output the image to the browser, for more info check: http://php.net/manual/es/function.imagecopymerge.php.
Related
I want to make the mixed image with fade effect from two images.
Here is my PHP code.
$img1 = new Imagick();
$img1->readImage("images/1.jpg");
$img2 = new Imagick();
$img2->setOption('compose:args', '50');
$img2->readImage("images/2.jpg");
$img1->compositeImage($img2, imagick::COMPOSITE_DISSOLVE, 0, 0);
$img1->writeImage("images/3.jpg");
This code works correctly on my Mac OS.
But it doesn't work on remote server (Cent OS 6.5), the result image is same with 2.jpg.
I tried with COMPOSITE_BLEND constant, it works well.
And I tried the command in terminal.
# composite -dissolve 50 2.jpg 1.jpg 3.jpg
It also works well.
Why doesn't COMPOSITE_DISSOLVE constant work in PHP?
Please help me.
You really ought to just figure out what the difference is between your dev environment and your server. It's very likely to be the case that the version of ImageMagick on the dev server is very out of date, unless you are compiling it yourself, as the Centos packages management system seems to take a long time to get new releases.
However....the way you are blending the images is not fantastic. Although it may work for you, it requires the slightly poorly defined $img2->setOption('compose:args', '50'); line.
A much more powerful technique is to control the alpha channel yourself through the COMPOSITE_COPYOPACITY. This gives you full control for how images are blended. e.g.
$img1 = new \Imagick();
$img1->readImage(realpath("../images/Biter_500.jpg"));
$img2 = new \Imagick();
$img2->readImage(realpath("../images/Skyline_400.jpg"));
//Resize images to the same size, to look pretty.
$img1->resizeimage(
$img2->getImageWidth(),
$img2->getImageHeight(),
\Imagick::FILTER_LANCZOS,
1
);
//Create an image that the alpha will be created in.
$opacity = new \Imagick();
if (true) {
//Create a 50% grey image
$opacity->newPseudoImage($img1->getImageWidth(), $img1->getImageHeight(), "CANVAS:gray(50%)");
}
else {
//Create a far more interesting blend.
//Gradients are created top to bottom, so we need to rotate the image
$opacity->newPseudoImage($img1->getImageHeight(), $img1->getImageWidth(), "gradient:gray(10%)-gray(90%)");
$opacity->rotateimage('black', 90);
}
$img2->compositeImage($opacity, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$img1->compositeImage($img2, \Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/jpg");
echo $img1->getImageBlob();
The first way of creating the opacity image with "CANVAS:gray(50%)" does what your existing image code does. The second way of blending creates a image that is blended from 10% to 90% across the width of the image.
i have 2 images one background and another one is layer first i want to rotate it then i want to place it somewhere using x and y.. it can be done using GD but in gd when rotate the image i get low quality and zigzag border.. so i tried to rotate in imagick it done its job good without zigzag and without loosing quality.. so i want to know how to place that rotated image to background...
Background
Image_2
Output should look like this
EDIT:
The code I have tried so far:
<?php
$imagick = new Imagick();
$imagick->readImage('137.png');
$imagick->rotateImage(new ImagickPixel('none'), -13.55);
$imagick->writeImage('my_rotated.png');
$imagick->clear();
$imagick->destroy();
?>
I was trying to find a solution myself. I guess this should get what you need:
$background = 'background.png';
$image = new Imagick($background);
// you can place the code below in a loop to put more images in order
$imgfile = 'foreground-image.png';
$part1 = new Imagick($imgfile);
$part1->rotateImage(new ImagickPixel('none'), -13.55);
$image->compositeImage($part1, Imagick::COMPOSITE_OVER, $left, $top);
$image->writeImage('my_rotated.png');
//save , flush , destroy etc...
I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.
Any way I can cut out a part of an existing image, and then save the result as a new image(file)?
imagecopy allows you to specify x,y,w,h arguments for the src image. Combine this with imagecreatetruecolor and you should easily be able to achieve what you want. There is even an example in the documentation for imagecopy:
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
Use imagejpeg or imagepng to save the image to a file.
You can use the gd functions for this (manual).
Load the source image (imagecreatefromstring() can be useful, so you don't have to specify the type of the image), create an output image (imagecreatetruecolor()) and use imagecopy() (if you don't want to resize it). At the end use imagepng() to output the image, or save it to a file.
Be warned that gd doesn't use image compression in memory, so your PHP process might require lots of RAM when creating a high resolution mosaic. Use imagefree() as soon as possible.
Is there a PHP function that would allow me to superimpose an image over another one? If not, how can I accomplish this (not asking for code, just a list of steps)?
I suppose there are functions provided by GD (generally enabled on PHP installations) that might do just that.
For instance, maybe one of imagecopy or imagecopymerge functions, I'd say.
See Example #1 Merging two copies of the PHP.net logo with 75% transparency on the manual page of the second one (quoting) :
<?php
// Create image instances
$dest = imagecreatefromgif('php.gif');
$src = imagecreatefromgif('php.gif');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
?>
There are also these two examples that might prove useful :
Adding watermarks to images using alpha channels
and Using imagecopymerge() to create a translucent watermark
Use the GD graphics library. There is an example of creating a watermark, which is basically the same thing using the imagecopymerge() function.