In the images folder i have a notfound.php file with
<?php
header('Content-type: image/png');
$im = imagecreatefrompng('simnotfound.png');
imagepng($im);
imagedestroy($im);
?>
The image is a 256 by 256. The notfound.php page shows a black 256 by 256 square. The image is not all black tho. Its just some black text on a transparent background in the center.
The fix is
<?php
header('Content-type: image/png');
$im = imagecreatefrompng('simnotfound.png');
imagealphablending($im, true); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
imagepng($im);
imagedestroy($im);
?>
create your image file one more time with white background to check that is read correctly, if yes - problem is your transparent background
also try with other file to eliminate problem with reading this specific file
bool imagesavealpha ( resource $image , bool $saveflag )
imagesavealpha — Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
Related
I have two graphics file.
The first image - a background image in JPG format
The second file - PNG file with the figure in the center filled with white, with a black border on a path. The main background of transparent PNG file.
Question:
How to merge two files with transparency (see image example)? Background of the first file should be placed inside the figure in the second file.
Scheme:
Images:
PNG file - profiles.in.ua/tmp/sample2.jpg
JPG file - profiles.in.ua/tmp/sample1.png
PHP code:
$mask = new Imagick(realpath('mask.png'));
$pattern = new Imagick(realpath('pattern.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$pattern->compositeImage($mask, Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/png");
echo $pattern->getImageBlob();
$mask->destroy();
$pattern->destroy();
Assuming the mask image is always made exclusively of white pixels (which should be overwritten with the pattern), black pixels (which should overwrite the pattern), and transparent pixels (which should remain transparent), you can get this effect by compositing the pattern into the non-transparent pixels in the mask, then darkening the result with the mask.
The PNG file you provided did not have a transparent background as specified; instead it was white and grey hatching. I had to edit it first to add a transparent background before this code worked.
$mask = new Imagick(realpath('sample1.png'));
$pattern = new Imagick(realpath('sample2.jpg'));
$pattern->resizeImage($mask->width, $mask->height, Imagick::FILTER_LANCZOS, 1);
$image = clone($mask);
$image->compositeImage($pattern, Imagick::COMPOSITE_IN, 0, 0);
$image->compositeImage($mask, Imagick::COMPOSITE_DARKEN, 0, 0);
header("Content-Type: image/png");
echo $image;
$image->destroy();
$mask->destroy();
$pattern->destroy();
You need to fix the end of your code. All good until the end.
$base->writeImage('output.png');
header("Content-Type: image/png");
echo $base;
Update me :)
I created an image from other image with the following code,
imagepng(imagecreatefromstring(file_get_contents(destination path)),source_path);
The image was created,But when i try to create a png image from a transparent image,The background become black color.
Why this happens?How to solve this?
If you look at the GD Documentation you'll find the alpha based functions. Taken directly from imagesavealpha() is the following code that opens a png and outputs it with transparency maintained;
// Load a png image with alpha channels
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
// (So any resizing/rotating/cropping etc)
// 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);
Don't know what i'm doing wrong.
Want to create a simple solid color png image.
$im = #imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00' );
$pdir = '/Applications/AMPPS/www/images/test.png';
imagepng($im,$pDir,9);
imagedestroy($im);
It doesn't work.
have no image in the dir even it it is ok for sure and has proper rights.
I only see some strange digits begins with PNG displayd on the page
To display your image, add the following to the very top of your script.
header("Content-type: image/png");
EDIT: Also, your variables for the output directory don't match. One is $pDir and $pdir.
EDIT 2: Here's your final code. imagepng() seems to either save the image to a file or display it, but not at the same time. So you will need two of them. Just simply don't pass a file path to the one that you want to display the image.
header("Content-type: image/png");
$im = #imagecreate(40, 40);
imagecolorallocate($im, '0xff', '0x00','0x00' );
$pdir = '/path/test.png';
imagepng($im);
imagepng($im,$pdir,9);
imagedestroy($im);
I am trying to do a watermark mark with the php code, and everything seems to work fine, until I put a transparent PNG file to a GIF. This what happens:
So instead of transparent black watermark, I get this semi solid green thing on the top.
The watermark is
I use the following php code:
...
$image = imagecreatefromgif($filepath);;
$watermark_image = imagecreatefrompng($wm_filepath);
imagealphablending($watermark_image, false);
imagesavealpha($watermark_image, true);
imagegif($image, $filepath);
imagedestroy($image);
imagecopy($image, $watermark_image, $offset['x'], $offset['y'], 0, 0, imagesx($watermark_image), imagesy($watermark_image) );
p.s. I have to mention that I tried to combine different settings using
imagealphablending()
imagesavealpha()
and got no result
UPD:
Now I am saving image as a png file. I deleted these two rows imagealphablending($watermark_image, false); imagesavealpha($watermark_image, true); and it worked. However, the transparency of PNG is overlapping GIF. imagealphablending($image, true); didn't help. What shall I do?
GIF files are limited to 256 colours and have a single colour designated as transparent. So you can't have antialiased transparency, nor can you have much antialiasing of any kind.
I suggest saving as PNG instead.
I'm already rotating an image:
$filename = 'http://gabomacias.zapto.org/flecha';
$grados = $_POST["grados"];
header('Content-type: image/png');
$source = imagecreatefrompng($filename);
$rotate = imagerotate($source, $grados, 0);
imagejpeg($rotate);
and showing it in the page,
But how can I save it so if someone rotates it again, it starts from the last rotation point and not form the original one, thanks.
From the imagejpeg documentation:
imagejpeg — Output image to browser or file
So,
imagejpeg($rotate, 'myimage.jpg');
ought to do the trick. At the top of the file, you can check to see if the myimage.jpg file exists using file_exists(), and if it does, load that to rotate it again.