how i can make php that cut part of photos? - php

I have a project I'm working on right now... I want to know is there any idea how i can make an app that every time i upload a photo it, with the specific size it cut like the middle part only and puts it on another photo i chose.
here is an example : 1 - Upload man photo with white background 2- it takes the middle part of the photo 3 - i upload a garden photo and the man shows there.
I just need to know the way to make it.
Is there also a way to delete white backgrounds like Chrom key or something?
example

Look into ImageMagick
ImageMagick allows you to cut, crop, colorize, etc. For example, here is some sample code showing how to crop a photo:
<?php
function cropImage($imagePath, $startX, $startY, $width, $height) {
$imagick = new \Imagick(realpath($imagePath));
$imagick->cropImage($width, $height, $startX, $startY);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}
?>
A brief scan of questions regarding ImageMagick and PHP will be helpful:
https://stackoverflow.com/questions/tagged/imagemagick+php
Resources:
https://phpimagick.com/
ImageMagick options and settings
Using ImageMagick to crop from center with PHP

Related

Uploading and Merging images in PHP

So I'm looking to create an internal tool where our employees can upload a picture of themselves, which automatically merges it with a white border at the bottom (company logo border) and their name on top of it. This way the offices can easily print the pictures for employee boards
So what I need is:
- merge border, picture and text into one image.
- Upload function with crop tool.
What I found is:
- PHP Image Magician (http://phpimagemagician.jarrodoberto.com/)
This basically has all functions I need available so naturally I got excited but I ran across one thing:
In the 14.1_upload.php file it refers to the following:
require_once('image_lib/upload_class.php');
the image_lib/upload_cass.php file doesnt come with the download from the website.
Is there something I'm missing or would you guys recommend not to use PHP Image Magician at all?
I'm looking to make it a very basic and simple tool but functional.
Thanks a bunch in advance
Uploading Image is a very easy process, you can use W3School reference to understand it well http://www.w3schools.com/php/php_file_upload.asp
About Image merge you can use below code:
<?php
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>

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 Combine/merge two image using PHP imagecopy() or imagecopymerge()?

Please help to combine three jpg images using php image processing functions imagecopy, imagecopymerge
image 1- b.jpg
image 2- l.jpg
image3: r.jpg
output look like
I tried some example codes of the PHP functions imagecopy() or imagecopymerge()
but not working with my images.
Could you give more information about: "not working"
I would suggest you to take a look at: imagecopyresampled
For the code, you have to load the images as image ressources, so you can use something like:
$im1 = imagecreatefrompng($path.'image1.png');
$im2 = imagecreatefrompng($path.'image2.png');
imagecopyresampled($im1,$im2,250,150,0,0,100,150,100,150);
unset($im2);
$im3 = imagecreatefrompng($path.'image3.png');
imagecopyresampled($im1,$im3,550,150,0,0,100,150,100,150);
unset($im3);
So $im1 now have the 3 images.
Just make sure to unset the image ressources, because you will run out of memory really fast.
Edit: I used imagecopyresampled because image 2-3 seem smaller on the last image, I don't know how more resource intensive it is, I guess imagecopymerge could work too.

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");

PHP: how to create an image from another PNG image

I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp

Categories