I am trying to make background of image transparent.Using following code
$strInputFile = 'test.jpg';
$target = 'test_transparent.png';
$im = new Imagick($strInputFile);
$im->paintTransparentImage($im->getImageBackgroundColor(), 0, 7000);
$im->setImageFormat('png');
$im->writeImage($target);
$im->destroy();
This code is working great for few images.But for most of the images output doesn't come out well.Wanted result come from script.
Unwanted result come from script.
Is it possible to detect edges of model and remove color before model?
Related
i'm using imagick to get the background color of an image
here is my code
$imagick = new Imagick();
$imagick->readImage($_FILES['passport']['tmp_name']);
$imagePixelColor = $imagick->getImageBackgroundColor();
$color = $imagePixelColor->getColorAsString();
but is not working as expected it keeps on given me srgb(255,255,255) for all images i used as sample. i have used an image with black background but still gives me srgb(255,255,255). pls help
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...
Whenever a user uploads a photo using my script, WideImage converts it to JPEG. However, I just noticed that if I upload a PNG picture, with a transparent background, it makes it black instead.
Is there any way to make this white instead?
This is how i save the image:
$img->resizeDown('500', null)->saveToFile('annonce_billeder/'.$bnavn.'.jpeg', 70);
Not really directly. You wnt to read about how transparency is stored in pictures: it is an ordinary color value (any color) that has been marked especially as transparent.
So most likely the color specified in the example pictures you try actually is coded as black and the transparency gets lost whilst converting.
You might have a try to find out if you can detect if there is a color marked as transparent in the incoming picture and then manually change that color to non-transparcy and white before converting the picture.
Might be similar, but I was able to create an empty truecolor image and fill it with its own transparent color before doing any drawing:
$img = WideImage_TrueColorImage::create(100, 100);
$img->fill(0,0,$img->getTransparentColor());
// then text, watermark, etc
$img->save('...');
I assume you'll do something more like:
$img = WideImage::load(<source>);
if( <$img is png> ) {
$img->fill(0,0, $img->getTransparentColor());
}
$img->resizeDown(500, null)->saveToFile('target.jpg', 70);
This is how to do it:
// Load the original image
$original = WideImage::load("image.png");
$original->resizeDown(1000); // Do whatever resize or crop you need to do
// Create an empty canvas with the original image sizes
$img = WideImage::createTrueColorImage($resized->getWidth(),$resized->getHeight());
$bg = $img->allocateColor(255,255,255);
$img->fill(0,0,$bg);
// Finally merge and do whatever you need...
$img->merge($original)->saveToFile("image.jpg");
With some changes (corrections) on Ricardo Gamba's solution code, it does the job...
// Load the original image
$original = WideImage::load("image.png");
$resized = $original->resizeDown('500', null); // Do whatever resize or crop you need to do
$original->destroy(); // free some memory (original image not needed any more)
// Create an empty canvas with the resized image sizes
$img = WideImage::createTrueColorImage($resized->getWidth(), $resized->getHeight());
$bg = $img->allocateColor(255,255,255);
$img->fill(0,0,$bg);
// Finally merge and do whatever you need...
$img->merge($resized)->saveToFile("image.jpg", 70);
After several test i decided use scaleimage method of imagick for reduce images, the problem is because i want to get a square image and some of the images to process are rectangular.
To make it square i placed a blank square image as background after the scaled image, this point took me 4 second per image and it's too much.
// Create an image
$scaledimage = new Imagick();
// Set as black
$scaledimage->newImage(800,600,'black');
//Scale the image
$scaledimage->scaleImage(500,500,true); // I need the same aspect
// Create an image
$image = new Imagick();
// Set as white
$image->newImage(500,500,'white');
// compose an image onto another
$image->compositeImage($scaledimage,Imagick::COMPOSITE_DEFAULT,62,0);
// Clone
$scaledimage = clone $image;
Do you known how could i do to make the scaled image square without use this approach.
In PHP I'm trying to process an image, that is, I'm trying to make the surrounding color transparent in a jpg file. I'm using the GD library by the way.
I can directly output the image by converting it into a png using imagecreatefromjpeg and imagepng functions. But I can't find a way to make the specified color transparent. Also, some images have lighter gray artifacts around black graphics, created during saving. Is there any way I can include those as well?
I'm kind of lost. I found some answers to make a color transparent on an image but I don't know how to first convert the image without saving it into the server and then process it.
Any ideas?
EDIT: Here's my code so far. I managed to make a specified color transparent but I can't make it detect the surrounding one yet.
Most of the time images will be closed because they'l be logos or texts, saved in the allowed image formats. So I don't think I will have a major issue with gradients but it would be great if I could manage to manipulate transparency in the surrounding gradients, if any, such as drop shadows.
Is there also any way to detect if the png/gif image is already transparent? My code paints the transparent parts into black for those files now.
$file = 'images/18.jpg';
$specs = getimagesize($file);
if($specs[2] == 1) $img = imagecreatefromgif($file); //gif
elseif($specs[2] == 2) $img = imagecreatefromjpeg($file); //jpg
elseif($specs[2] == 3) $img = imagecreatefrompng($file); //png
else exit('unsupported file type!');
$newimg = imagecreatetruecolor(imagesx($img), imagesy($img));
// create a new image with the size of the old one
imagecopy($newimg,$img,0,0,0,0,imagesx($img),imagesy($img));
// copy the old one
imagedestroy($img);
// free the memory
$white = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$white);
// make white pixels transparent
header('Content-Type: image/png');
imagepng($newimg);
imagedestroy($newimg);
// and finally output the new image
You can set the transparent color with the imagecolortransparent function.