I am using imagemagick 6.8.4-6 and i'm having an issue with setting the PNG on rotate to have a transparent background. My code is below and i am using imagick through php
$base = new Imagick("images/Champs-43.png");
$layer = new Imagick("images/coporate1.png");
$base->setFormat("png32");
$layer->setFormat("png32");
$layer->rotateImage(new ImagickPixel("none"), 45);
$base->compositeImage($layer, imagick::COMPOSITE_DEFAULT, 20, 20);
header('Content-Type: image/png');
echo $base;
the above gives a black background to the area rotated. i have tried "none", "transparent", "#00000000", and "rgba(0, 0, 0, 0.0)" and none of them seem to work.
Any help appreciated
Try explicitly setting the ImagickPixel.
You can try these:
$layer->rotateImage(new ImagickPixel('#FFFFFF'), 45);
or
$layer->rotateImage(new ImagickPixel('#00000000', 45);
Related
I am using PHP to rotate an PNG image, with a transparant background. But whatever I try, there are still some black lines around the original image.
How do I remove the black lines. Everything else works fine. The image is transparant, the image is rotated, the new corners are also transparant. Just the black lines around the original square (which is rotated) are annoying me.
I use this code:
$angle = -100;
header('Content-type: image/png');
$image = 'http://mapning.com/img/plane.png';
$file = imagecreatefrompng($image);
$rotate = imagerotate($file, $angle, 0);
imageSaveAlpha($rotate, true);
ImageAlphaBlending($rotate, false);
$transparentColor = imagecolorallocatealpha($rotate, 200, 200, 200, 127);
imagefill($rotate, 0, 0, $transparentColor);
imagepng($rotate);
I found my answer here:
http://ru2.php.net/manual/en/function.imagerotate.php#47985
I think better use imagick
Here is an extension for PHP
Or if you want with GD see here
http://ru2.php.net/manual/en/function.imagerotate.php#46338
I tried literally all day yesterday trying to figure this out. I rotate an image via imagerotate(). I get a black background where the image isn't covering anymore. I have tried everything i cant think of to make that background transparent..
here is my current code..
function rotate($degrees) {
$image = $this->image;
imagealphablending($image, false);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$rotate = imagerotate($image, $degrees, $color);
imagecolortransparent($rotate, $color);
imagesavealpha($image, true);
$this->image = $rotate;
}
I am really starting to get ticked off. Can someone show me some working code? please?
Could it be something wrong with my WAMP server and dreamweaver? because i even tried this.. http://www.exorithm.com/algorithm/view/rotate_image_alpha and it still puts out either a black or white background..
Try setting imagesavealpha() on your rotated image.
Currently you are running imagesavealpha() on your original image. [ eg. imagesavealpha($image, true); ]
Instead you want to run imagesavealpha() on the rotated image and then set $this->image... try:
...
$rotate = imagerotate($image, $degrees, $color);
imagecolortransparent($rotate, $color);
imagesavealpha($rotate, true); // <- using $rotate instead of $image
$this->image = $rotate;
}
How do you correctly output PNG images with PHP so their shading, and other transparent effects don't fail.
seems to be outputting as
...is there a way so this doesn't happen?
I merged two images together.
<?php
// Create image instances
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 180, 180, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
imagealphablending and imagesavealpha.
See this post: PNG Image Transparency in PHP GD
I am trying to create the silhouette of PNG image with transparencies.
Here is my code
//file that i am working with
$file='http://www.google.com/mapfiles/turkey.png';
$im =imagecreatefrompng($file);
imagealphablending($im, false);
imagesavealpha($im, true);
$imw =imagesx($im);
$imh =imagesy($im);
//turkey body color
$bodycolor=imagecolorallocatealpha($im, 144, 144, 144, 50);
//imageholder
$imnew =imagecreatetruecolor($imh, $imh);
imagealphablending($imnew, false);
imagesavealpha($imnew, true);
$transparent_color=imagecolorallocatealpha($imnew, 0, 0, 0, 127);
imagecolortransparent($imnew, $transparent_color);
imagefilledrectangle($imnew, 0, 0, $imh, $imh, $transparent_color);
for ($i=0; $i > 24;
//all not transparent pixels are copied to imageholder
if ($alpha != 127)
{
imagesetpixel($imnew, $i, $j, $bodycolor);
}
}
}
//blur filter
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($imnew, IMG_FILTER_GAUSSIAN_BLUR);
header ("Content-type: image/png");
imagepng ($imnew);
imagedestroy ($imnew);
?>
So, I need to get the silhouette ot the turkey with blurred edges. I need edges to be blurred "outside", so the farther the pixel was from edge, the more transparent it was with the same rbg. And imagefilter also lifts the turkey a bit :)
For more complex image manipulations like these, I would highly recommend you use ImageMagick: http://www.imagemagick.org
It's pretty easy to install unto your server, very fast and has great documentation. They have a whole section for what you're talking about: http://www.imagemagick.org/Usage/channels/
Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);